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

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

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

| improve this answer | |
  • 2
    yes its working – Sourav Feb 15 '19 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 '19 at 13:30
  • Can you please add your code so I can check and give the proper answer? Thanks – Shankar Nov 22 '19 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 '19 at 12:43
  • Did you created custom customer attribute? – Shankar Nov 24 '19 at 4:27
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()],
        ]
    );
    }
}
| improve this answer | |
  • 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 '18 at 5:28

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.