I have looked over a few tutorials on how to add a customer attribute to the customer edit page within admin.
- http://www.extensions.sashas.org/blog/magento-2-make-customer-attribute.html
- Magento 2: How to create a customer custom attribute?
- How to add dropdown attribute to custom module in magento 2.0
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.