Created
July 4, 2021 16:18
-
-
Save drpayyne/b58619197b6a7fcffced71a6da2cd5f0 to your computer and use it in GitHub Desktop.
Magento 2 Customer Attribute - Simple Text Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace Manick\Customer\Setup\Patch\Data; | |
use Exception; | |
use Psr\Log\LoggerInterface; | |
use Magento\Customer\Api\CustomerMetadataInterface; | |
use Magento\Customer\Model\Customer; | |
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResource; | |
use Magento\Customer\Setup\CustomerSetup; | |
use Magento\Customer\Setup\CustomerSetupFactory; | |
use Magento\Framework\Setup\ModuleDataSetupInterface; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
/** | |
* Creates a customer attribute for managing a customer's external system ID | |
*/ | |
class ExternalId implements DataPatchInterface | |
{ | |
/** | |
* Attribute constants | |
*/ | |
const ATTRIBUTE_CODE = 'manick_external_id'; | |
const ATTRIBUTE_LABEL = 'External ID'; | |
/** | |
* @var ModuleDataSetupInterface | |
*/ | |
private ModuleDataSetupInterface $moduleDataSetup; | |
/** | |
* @var CustomerSetup | |
*/ | |
private CustomerSetup $customerSetup; | |
/** | |
* @var AttributeResource | |
*/ | |
private AttributeResource $attributeResource; | |
/** | |
* @var LoggerInterface | |
*/ | |
private LoggerInterface $logger; | |
/** | |
* Constructor | |
* | |
* @param ModuleDataSetupInterface $moduleDataSetup | |
* @param CustomerSetupFactory $customerSetupFactory | |
* @param AttributeResource $attributeResource | |
* @param LoggerInterface $logger | |
*/ | |
public function __construct( | |
ModuleDataSetupInterface $moduleDataSetup, | |
CustomerSetupFactory $customerSetupFactory, | |
AttributeResource $attributeResource, | |
LoggerInterface $logger | |
) { | |
$this->moduleDataSetup = $moduleDataSetup; | |
$this->customerSetup = $customerSetupFactory->create(['setup' => $moduleDataSetup]); | |
$this->attributeResource = $attributeResource; | |
$this->logger = $logger; | |
} | |
/** | |
* Get array of patches that have to be executed prior to this. | |
* | |
* Example of implementation: | |
* | |
* [ | |
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class, | |
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class | |
* ] | |
* | |
* @return string[] | |
*/ | |
public static function getDependencies(): array | |
{ | |
return []; | |
} | |
/** | |
* Get aliases (previous names) for the patch. | |
* | |
* @return string[] | |
*/ | |
public function getAliases(): array | |
{ | |
return []; | |
} | |
/** | |
* Run code inside patch | |
*/ | |
public function apply() | |
{ | |
// Start setup | |
$this->moduleDataSetup->getConnection()->startSetup(); | |
try { | |
// Add customer attribute with settings | |
$this->customerSetup->addAttribute( | |
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, | |
self::ATTRIBUTE_CODE, | |
[ | |
'label' => self::ATTRIBUTE_LABEL, | |
'required' => 0, | |
'position' => 100, | |
'system' => 0, | |
'user_defined' => 1, | |
'is_used_in_grid' => 1, | |
'is_visible_in_grid' => 1, | |
'is_filterable_in_grid' => 1, | |
'is_searchable_in_grid' => 1, | |
] | |
); | |
// Add attribute to default attribute set and group | |
$this->customerSetup->addAttributeToSet( | |
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, | |
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, | |
null, | |
self::ATTRIBUTE_CODE | |
); | |
// Get the newly created attribute's model | |
$attribute = $this->customerSetup->getEavConfig() | |
->getAttribute(Customer::ENTITY, self::ATTRIBUTE_CODE); | |
// Make attribute visible in Admin customer form | |
$attribute->setData('used_in_forms', [ | |
'adminhtml_customer' | |
]); | |
// Save attribute using its resource model | |
$this->attributeResource->save($attribute); | |
} catch (Exception $e) { | |
$this->logger->err($e->getMessage()); | |
} | |
// End setup | |
$this->moduleDataSetup->getConnection()->endSetup(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment