6

Note: The question was referring to and the code for setup scripts is not the same anymore in Magento 2.x.

I'm playing with Magento 2 and I'm stuck in adding attribute for customer entity. I got attribute properly installed using data installer, but I can't get it displayed in forms.

My data-install-2.0.0.php setup script looks as follows:

/* @var $this \Magento\Customer\Model\Resource\Setup */
$installer = $this;

$installer->startSetup();

// Add nickname attribute
$installer->addAttribute(
    'customer',
    'nickname',
    [
        'type' => 'varchar',
        'label' => 'Nickname',
        'input' => 'text',
        'required' => true,
        'sort_order' => 75,
        'visible' => true,
        'system' => true,
        'unique' => true,
        'position' => 75,
    ]
);


$nicknameAttribute = $installer->getEavConfig()->getAttribute('customer', 'nickname');
$nicknameAttribute->setData(
    'used_in_forms',
    ['customer_account_create', 'customer_account_edit', 'checkout_register', 'adminhtml_customer']
);
$nicknameAttribute->save();

$installer->endSetup();

This part works fine, after running setup/index.php update my attribute is visible in database tables (eav_attribute, customer_eav_attribute, customer_form_attribute). However it's not visible in forms.

I added following in etc/fieldset.xml in my custom module:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Object/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="customer_account">
            <field name="nickname">
                <aspect name="create" />
                <aspect name="update" />
                <aspect name="name" />
            </field>
        </fieldset>
    </scope>
</config>

It looks it does nothing.

Then I added following in data_source/customer.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../../app/code/Magento/Ui/etc/data_source.xsd">
    <dataSource
            name="account" label="Account Information"
            dataSet="Magento\Customer\Model\Resource\Customer\Collection">
        <fields entityType="customer">
            <field name="nickname" source="eav" dataType="text"/>
        </fields>
    </dataSource>
</config>

This actually does something, it breaks backend customer form with exception:

More than one node matching the query: /config/dataSource[@name='account']/fields/field

I guess adding to these two xml files from core module in custom module should be done in some other way.

At the end I did one more thing just for testing. I removed etc/fieldset.xml and data_source/customer.xml from my module and added that content in Magento_Customer core module. This resulted in having my field displayed in backend (hooray!), but saving still doesn't work and throws error message:

"Nickname" is a required value.

So my questions are:

  1. What is a valid way to alter etc/fieldset.xml and data_source/customer.xml in custom module?

  2. What else should be done to have custom attribute being saved?

  3. How can I do this for frontend registration form? Adding field in form/register.phtml doesn't do the magic.

  • Did you figure this out yet Wojtek? – James Anelay - TheExtensionLab Apr 15 '15 at 19:49
  • @JamesAnelay no, I haven't figured that out and haven't checked that with latest betas. – Wojtek Naruniec Apr 24 '15 at 15:32
  • Did you create git repository? Can you show it? – zhartaunik May 12 '15 at 7:34
3

Note: This answer was referring to and the code for setup scripts is not the same anymore in Magento 2.x.

Finally I can provide answer to my question. Adding customer attribute and having it displayed in backend form doesn't require to change anything in XML files. It could be achieved by doing following in install/upgrade script:

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'nickname',
        [
            'label'            => 'Nickname',
            'required'         => 0,
            'system'           => 0,
            'position'         => 100
        ]
    );

    $customerSetup->getEavConfig()->getAttribute('customer', 'nickname')
        ->setData('used_in_forms', ['adminhtml_customer'])
        ->save();

system property set to 0 is crucial, in other case attribute value won't be saved.

It works well on current develop branch.

  • What about showing and saving in frontend, have you figured that out? – stevensagaar Jan 8 '16 at 23:59
  • I am using your code in my function but is give me error as below:- unexpected '$setup' veriable Please suggest – Purushotam Sharma Dec 25 '17 at 8:39
1

Creation of Customer Attribute through InstallData

    /** @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, 'work_profile', [
        'type' => 'varchar',
        'label' => 'Work Profile',
        'input' => 'text',
        'class' => '',
        'source' => '',                            
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                                /*Scope of your attribute */
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'position' =>999,
        'system' => 0,
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'approved_attribute')
    ->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer'],
    ]);

    $attribute->save();

You can use other used_in_forms values ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']

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.