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();
}
  • Can you update your installer code? – Sukumar Gorai Aug 30 '18 at 9:28
  • @SukumarGorai I added the code – Daniele Rovatti Aug 30 '18 at 10:13
  • which magento version are you using ? – Pradeep Sanku Aug 30 '18 at 10:25
  • @PradeepSanku I'm using Magento 2.2.3 – Daniele Rovatti Aug 30 '18 at 10:30
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!

0

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, privacy policy and cookie policy

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