1

I have looked over a few tutorials on how to add a customer attribute to the customer edit page within admin.

This however does not seem to be working completely for me. I added the below to my InstallData.php file:

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, 'cp_permissions', [
        'type' => 'text',
        'label' => 'Account Type',
        '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 => 'Master',
                        1 => 'Branch'
                    ),
            ),
    ]);

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

    $attribute->save();

    }
}

And run setup:upgrade to install the plugin.

I compared my attribute to the gender attribute. Main issues seems to be that, the source model wasn't being set and need to work out how to make sure this is set. When i set the source manually it shows up fine however.

1 Answer 1

0

Setting the Source Model Correctly

Studying the /vendor/magento/module-catalog/Setup/customerSetup.php file from the core i could see that source was being set differently to other answers on stack exchange:

 'source' => \Magento\Customer\Model\Customer\Attribute\Source\Website::class,
 'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Website::class,

I changed the setup data script to match and this added the source to the database however attribute still didn't show. Comparing the gender attribute again i could see that user_defined was set to 0 rather than 1. Setting this seemed to be the final step to kick the attribute into action.

Final addAttribute Method:

$customerSetup->addAttribute(Customer::ENTITY, 'cp_permissions', [
        'type' => 'static',
        'label' => 'Account Type',
        'input' => 'select',
        'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class,
        'required' => false,
        'visible' => true,
        'user_defined' => false,
        'sort_order' => 101,
        'position' => 101,
        'system' => 0,
        'adminhtml_only' => 1,
        'option' =>
            array (
                'values' =>
                    array (
                        0 => 'Master',
                        1 => 'Branch'
                    ),
            ),
    ]);

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.