1

I have added new customer attribute 'Company' (not to customer_address) and I need to render this in customer_form ui-component at the "Account information" tab.

It's already in customer collection when I check this in magento_root/vendor/magento/module-customer/Model/Customer/DataProvider.php enter image description here

For implementing I have added to magento_root/vendor/magento/module-customer/view/base/ui_component/customer_form.xml following:

    <field name="company" formElement="input">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">customer</item>
            </item>
        </argument>
        <settings>
            <dataType>text</dataType>
        </settings>
    </field>

I also tried:

        <field name="company">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">50</item>
                <item name="dataType" xsi:type="string">varchar</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Company</item>
                <item name="default" xsi:type="number">0</item>
                <item name="source" xsi:type="string">customer</item>
            </item>
        </argument>
    </field>

But this field doesn't appear at the account information tab! What am I missing?

4

1 Answer 1

2

May be you doing wrong when you created customer attribute. No need to additional xml for that. Try following code which create mobile attribute and added account information tab automatically.


namespace Vendor\ModuleName\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use \Magento\Framework\Setup\UpgradeDataInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * Upgrade data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '2.0.1', '<')) {
            $this->createMobileAttribute($setup);
        }
    }

    public function createMobileAttribute($setup)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        // Add new customer attribute
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'mobile',
            [

                'label'                 => 'Mobile',
                'input'                 => 'text',
                'required'              => false,
                'sort_order'            => 1000,
                'position'              => 1000,
                'visible'               => true,
                'system'                => false,
                'is_used_in_grid'       => true,
                'is_visible_in_grid'    => false,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false,
                'default'               => '0'
            ]
        );

        // add attribute to form
        /** @var  $attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile');
        $attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']);
        $attribute->save();

        $setup->endSetup();
    }
}

6
  • Hi @sohel Rana in which file i have to add the above code in my custom module Commented Feb 12, 2019 at 13:03
  • Vendor\ModuleName\Setup\UpgradeData.php
    – Sohel Rana
    Commented Feb 12, 2019 at 13:09
  • Hi @sohel thanks for your reply, I would like add a custom field in myaccount dashboard frontend section not in admin : Commented Feb 12, 2019 at 13:30
  • Please refer the below link: magento.stackexchange.com/questions/261433/… Commented Feb 12, 2019 at 13:30
  • Hi @sohel now iam able to add fields account information section but the values are not saving in which table i have to check. Commented Feb 14, 2019 at 18:53

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.