I added an attribute to customer entity
My goal is to have this attribute as Select (with data from another db table), but i didn't find any tuto about the source/model that i should create !!
Here is the Files :
InstallData Vendor/Module/Setup/InstallData.php
<?php
namespace Vendor\Module\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
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @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, 'testfield', [
'type' => 'varchar',
'label' => 'Test Field',
'input' => 'select',
'required' => false,
'visible' => true,
'user_defined' => true,
'visible_on_front' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'source' => \Vendor\Module\Model\Customer\Attribute\Source\Test,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'testfield')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'],
]);
$attribute->save();
}
}
Source Model (Needed Code) : Vendor\Module\Model\Customer\Attribute\Source\Test
<?php
namespace Vendor\Module\Model\Customer\Attribute\Source;
class Test extends \Magento\Eav\Model\Entity\Attribute\Source\Table
{
public function __construct(..........)
{
......................
}
/**
* @return array
*/
public function getAllOptions()
{
...................
return $this->_options;
}
}
Could any one help me how to create this source model.
Best Answer
Install Data
Source Model
Helper
Data.php
Note: Tags is for example. You can load your collection and return to
getAllOptions
function.If your install data script successfully installed your custom attribute, now you just need to override addition information phtml file and set your custom attribute in that.
Create
Vendor/Module/view/frontend/layout/customer_account_create.xml
Create
Vendor/Module/view/frontend/templates/attribute.phtml