1

I have created some custom customer attribute that I need to show only in admin customer form. For these attributes i used in my InstallData 'used_in_forms' => ['adminhtml_customer']

In customer_form_attribute my attribute code matches the used_in_form code but I'm able to show it in admin only if I add 'used_in_forms' => ['customer_account_edit'], but this way it is shown also in the frontend.

Is there a way to show it only on the admin side?

EDIT:

Here is upgrade code

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    if (version_compare($context->getVersion(), '1.0.2', '<')) {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'my_attribute', [
            'type' => 'int',
            'label' => 'My attribute label',
            'input' => 'boolean',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
            'default' => 1,
        ]);
        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'my_attribute')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],
            ]);

        $attribute->save();
    }

    $setup->endSetup();
}
4
  • Can you update your installer code? Aug 30, 2018 at 9:28
  • @SukumarGorai I added the code Aug 30, 2018 at 10:13
  • which magento version are you using ? Aug 30, 2018 at 10:25
  • @PradeepSanku I'm using Magento 2.2.3 Aug 30, 2018 at 10:30

2 Answers 2

1

I have managed to work this by following code:

$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'my_attribute');
$attribute->setData('used_in_forms', $usedInForms);
$attribute->save();

I have changed the addData to setData and not passing anything else.

Hope this helps!

1

Try Below code

<?php
    $customerSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY,
        'my_attribute',
        [
            'label' => 'My attribute label',
            'system' => 0,
            'position' => 999,
            'sort_order' =>999,
            'visible' =>  true,
            'note' => '',
            'type' => 'int',
            'input' => 'boolean',
        ]
    );

    $customerSetup->getEavConfig()
                ->getAttribute('customer', 'my_attribute')
                ->setData('is_user_defined',1)
                ->setData('is_required',0)
                ->setData('default_value','')
                ->setData('used_in_forms', ['adminhtml_customer'])
                ->save();
?>

Even after above code if the field is not visible in backend try adding customer_form.xml at view/base/ui_component/ of your module with following code

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <field name="my_attribute" formElement="checkbox">
            <settings>
                <visible>true</visible>
            </settings>
        </field>
    </fieldset>
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.