CustomerSetupFactory and Attribute SetFactory classes should be used to create the customer attribute using setup script
<?php
namespace ExamNotes\Chapter10\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\ResourceModel\Attribute;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var Attribute
*/
private $attributeResource;
/**
* InstallData constructor.
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
* @param Attribute $attributeResource
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
Attribute $attributeResource
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->attributeResource = $attributeResource;
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(\Magento\Customer\Model\Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'favourite_quote');
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'favourite_quote',
[
'type' => 'varchar',
'label' => 'Favourite Quote',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 900,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
]);
$favouriteQuote = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,
'favourite_quote');
$favouriteQuote->setData('attribute_set_id', $attributeSetId);
$favouriteQuote->setData('attribute_group_id', $attributeGroupId);
$favouriteQuote->setData('used_in_forms', ['adminhtml_customer','customer_account_edit']);
$this->attributeResource->save($favouriteQuote);
$setup->endSetup();
}
}
Following code can be used to create Yes or No option field and show it in grid
Magento\Eav\Model\Entity\Attribute\Source\Boolean;
$customerSetup->removeAttribute(Customer::ENTITY, 'is_otp_validated');
$customerSetup->addAttribute(Customer::ENTITY, 'is_otp_validated', [
'type' => 'int',
'label' => 'Otp Validated',
'input' => 'boolean',
'required' => false,
'visible' => true,
'source' => Boolean::class,
'backend' => '',
'user_defined' => false,
'is_user_defined' => false,
'sort_order' => 999,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
'position' => 999,
'default' => 0,
'system' => 0,
]);
$otpValidated = $customerSetup->getEavConfig()->getAttribute('customer', 'is_otp_validated');
if ($otpValidated->getId()) {
$otpValidated->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeDefaultGroupId,
'used_in_forms' => ['adminhtml_customer']
]);
$otpValidated->save();
}
Posted by vasan to vasan's deck (2019-05-03 10:37)