3

Can someone point me in the right direction here? For a new magento 2 install I want to be able to add some custom attributes for the customer in the backend.

  1. One of the attributes must be only visible in the backend
  2. One of the attributes needs to be a selectlist be predefined variables.

Like: Insurance company 1 Insurance company 2

Update: In the end I fixed it with the help of: Basic module: http://www.extensions.sashas.org/blog/magento-2-1-3-how-to-make-customer-attribute-update.html

Select list: Adding customer multiselect attribute

2
5

In the answer of James Cooper. The backend type for the model provided is text. This should be int for a select list. For a select list the ID of the select is saved in the database. Text will work but its not the Magento standard for lists and could result in slower database performance.

the correct array should be:

'type' => 'int',
    'label' => 'Insurance Companies',
    'input' => 'select',
    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
    'required' => false,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 101,
    'position' => 101,
    'system' => 0,
    'option' =>
        array (
            'values' =>
                array (
                    0 => 'Insurance Company 1',
                    1 => 'Insurance Company 2',
                    2 => 'Insurance Company 3',
                ),
        ),

On a sidenote: There are also some attributes in the array that have default M2 values and could be removed. Like: 'required' => false, 'visible' => true,
See: vendor/magento/module-customer/Model/ResourceModel/Setup/PropertyMapper.php AND the DB model of eav_attribute

2
  • The reference to module-customer/Model/ResourceModel/Setup/PropertyMapper.php was a huge help for me. Additionally, most any file with the name PropertyMapper in the Magento source is worth looking at for default values. Another example: vendor/magento/module-eav/Model/Entity/Setup/PropertyMapper.php.
    – RNanoware
    Apr 13 '18 at 13:13
  • Here as value it set from last id. but I want here to set it value as 0,1
    – Ravi Soni
    Sep 26 '18 at 14:14
2

I would create an InstallData.php file in your module as follows:

<?php

namespace YourFolder\YourModule\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {
protected $customerSetupFactory;
private $attributeSetFactory;

public function __construct(CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}

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

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

    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(Customer::ENTITY, 'insurance_companies', [
        'type' => 'text',
        'label' => 'Insurance Companies',
        'input' => 'select',
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'sort_order' => 101,
        'position' => 101,
        'system' => 0,
        'option' =>
            array (
                'values' =>
                    array (
                        0 => 'Insurance Company 1',
                        1 => 'Insurance Company 2',
                        2 => 'Insurance Company 3',
                    ),
            ),
    ]);

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

    $attribute->save();

    }
}

When you run the setup:upgrade this will insert the relevant data to your Magento database. The relevant sections are:
1) the used in forms => as 'adminhtml_customer'
2) 'source' for the dropdown options as Magento\Eav\Model\Entity\Attribute\Source\Table, and the values specified inserted into the 'eav_attribute_option_value' table.

8
  • Thank you for this. I have tried. The problem is that I do not see it in the backend (customer/index/new/). It is enabled in (/system_config/edit/section/advanced) so Magento recognises the plugin. My module structure is: /developper/ /developper/AddCustomerInsurance/ /developper/AddCustomerInsurance/registration.php /developper/AddCustomerInsurance/etc/module.xml /developper/AddCustomerInsurance/Setup/InstallData.php Al seems to work (during install and activation) but I do not see it in admin customer view. Feb 21 '17 at 10:29
  • @AntondeGoede OK, just check the database tables to ensure the values have been inserted. 'select * from eav_attribute where entity_type_id = 1' should give you all the customer attributes 'select * from customer_form_attribute where form_code = 'adminhtml_customer'' should give you the attributes which will appear on the admin form. 'select is_visible from customer_eav_attribute' should also tell you if the attribute is visible from the front end. Feb 21 '17 at 11:59
  • I tried the first query (select * from eav_attribute where entity_type_id = 1') but the new attribute isn't there. Any idea what to do next? I could share a zip with the module. Feb 21 '17 at 21:28
  • @AntondeGoede Check if the attribute is there at all by 'select * from eav_attribute' and see if your field is visible. If not, then the install data hasn't run correctly. Remember you need to run the magento command magento setup:upgrade to insert the data. 'select * from setup_module' to check if the module is listed. If it is, delete the row and run the update again looking for any error messages. If the row isn't present, just run your magento setup:upgrade from the command line. Feb 22 '17 at 13:22
  • I have done magento setup:upgrade. When I check 'select * from eav_attribute' I do not see the attribute. When i check 'select * from setup_module' I do see the module. After deleting the row and the again magento setup:upgrade i get the same problem. Visible in 'select * from setup_module' but not 'select * from eav_attribute'. There are no errors during magento setup:upgrade. Feb 22 '17 at 14:29

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.