6

I created custom customer attribute is_institution with boolean value

My installData.php is

<?php
namespace xxx\xxxx\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallData implements InstallDataInterface {

    private $_eavSetupFactory;
    private $_attributeRepository;

    public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,\Magento\Eav\Model\AttributeRepository $attributeRepository)
    {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_attributeRepository = $attributeRepository;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {

        $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

        // add customer_attribute to customer
        $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'is_institution');
        $eavSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY, 'is_institution', [
            'type' => 'int',
            'label' => 'Is Institution',
            'input' => 'select',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required' => false,
            'default' => '0',
            'sort_order' => 100,
            'system' => false,
            'position' => 100
                ]
        );

        // allow customer_attribute attribute to be saved in the specific areas
    $attribute = $this->_attributeRepository->get('customer', 'is_institution');
    $setup->getConnection()
    ->insertOnDuplicate(
        $setup->getTable('customer_form_attribute'),
        [
            ['form_code' => 'adminhtml_customer', 'attribute_id' => $attribute->getId()],
            ['form_code' => 'customer_account_create', 'attribute_id' => $attribute->getId()],
            ['form_code' => 'customer_account_edit', 'attribute_id' => $attribute->getId()],
        ]
    );
    }
}

It is displayed in admin customer form

enter image description here

But I want to disable it from admin form, so the admin cannot modify this attribute

I also tried 'readonly' => true, but it is not working

7
  • Did you use Ui component? Apr 19, 2018 at 12:06
  • no , i created customer attribute . is need to ui component?
    – mohan
    Apr 19, 2018 at 12:17
  • Pls add 'visible' => false in your script & run again
    – Abdul
    Apr 19, 2018 at 12:21
  • i try attribute created but i cant see admin view. I want not editable
    – mohan
    Apr 19, 2018 at 12:33
  • Did you got any solution for this?
    – Piyush
    Jun 5, 2018 at 12:44

3 Answers 3

18

You need to create Namespace/Module/view/base/ui_component/customer_form.xml and add the following code

<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="is_institution" formElement="input">
            <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="disabled" xsi:type="boolean">true</item>
            </item>
        </argument>
        </field>
    </fieldset>
</form> 

I hope this solution will solve your problem. Thanks

7
  • 2
    yes its working
    – Sourav
    Feb 15, 2019 at 8:56
  • 2
    working for me as well. any idea if there is a setting to keep the toggle on/off aspect? for me the toggle turned into an input field with the value 1 or 0. I have a boolean field.
    – Radu
    Nov 22, 2019 at 13:30
  • Can you please add your code so I can check and give the proper answer? Thanks
    – Shankar
    Nov 22, 2019 at 19:20
  • @Shankar I'm using the code you've provided in the answer. The only difference is the field name which shouldn't matter.
    – Radu
    Nov 23, 2019 at 12:43
  • Did you created custom customer attribute?
    – Shankar
    Nov 24, 2019 at 4:27
1

Or in more recent versions of Magento...

And using @Shankar example

You need to create Namespace/Module/view/base/ui_component/customer_form.xml and add the following code

<fieldset name="customer">
    <field name="is_institution">
        <settings>
            <disabled>1</disabled>
        </settings>
    </field>
</fieldset>
0

You have to remove this line from your code

 ['form_code' => 'adminhtml_customer', 'attribute_id' => $attribute->getId()],

your code should be like this

<?php
namespace xxx\xxxx\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallData implements InstallDataInterface {

    private $_eavSetupFactory;
    private $_attributeRepository;

    public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,\Magento\Eav\Model\AttributeRepository $attributeRepository)
    {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_attributeRepository = $attributeRepository;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {

        $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

        // add customer_attribute to customer
        $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'is_institution');
        $eavSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY, 'is_institution', [
            'type' => 'int',
            'label' => 'Is Institution',
            'input' => 'select',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required' => false,
            'default' => '0',
            'sort_order' => 100,
            'system' => false,
            'position' => 100
                ]
        );

        // allow customer_attribute attribute to be saved in the specific areas
    $attribute = $this->_attributeRepository->get('customer', 'is_institution');
    $setup->getConnection()
    ->insertOnDuplicate(
        $setup->getTable('customer_form_attribute'),
        [
            ['form_code' => 'customer_account_create', 'attribute_id' => $attribute->getId()],
            ['form_code' => 'customer_account_edit', 'attribute_id' => $attribute->getId()],
        ]
    );
    }
}
1
  • It hidden from customer form . but i want display values, but Admin user con't change that value, because it update form some other DB .
    – mohan
    Apr 20, 2018 at 5:28

Your Answer

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

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