30
loading...
This website collects cookies to deliver better user experience
2.4.2-p1
and I'll try my best to make the code as clean as possible following the Magento best practices. :)\Manick\Customer\Setup\Patch\Data
implementing the \Magento\Framework\Setup\Patch\DataPatchInterface
. For now, implement the interface stubs as empty methods. This class must be placed under the module's Setup\Patch\Data
namespace to be auto picked up by Magento when we run bin/magento setup:upgrade
. Note - data patches are applied only once.
class ExternalId implements DataPatchInterface {}
public function __construct(
\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
\Magento\Customer\Model\ResourceModel\Attribute $attributeResource,
\Psr\Log\LoggerInterface $logger
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetup = $customerSetupFactory->create(['setup' => $moduleDataSetup]);
$this->attributeResource = $attributeResource;
$this->logger = $logger;
}
apply
method. This performs a couple of prerequisites in MySQL, like turning off foreign key checks and modifying the SQL mode.
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
... insert patch code from the below steps here ...
$this->moduleDataSetup->getConnection()->endSetup();
}
\Magento\Customer\Api\CustomerMetadataInterface
class, so make sure to import that.$this->customerSetup->addAttribute(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
'manick_external_id',
[
'label' => 'External ID',
'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,
]
);
addAttribute
are - entity type ID, attribute code, and attribute settings.text
HTML input for the browser and as a varchar
database value type. Since we want a simple text field, we do not need to specify the already default value. However, we need to provide the following settings:setting | meaning | value |
---|---|---|
label | user facing label | string |
required | is the attribute a required field? |
0 or 1
|
position | sort order for the form it appears in | number |
system | is the attribute system-defined? this should always be 0 for custom attributes |
0 or 1
|
user_defined | is the attribute user-defined? this should always be 1 for custom attributes |
0 or 1
|
is_used_in_grid | is the attribute used in the customer grid? this needs to be 1 for any of the below values to take effect |
0 or 1
|
is_visible_in_grid | is the attribute visible in the customer grid as a column? |
0 or 1
|
is_filterable_in_grid | is the attribute filterable in the customer grid under the filter settings? |
0 or 1
|
is_searchable_in_grid | is the attribute searchable in the customer grid using the search box? |
0 or 1
|
$this->customerSetup->addAttributeToSet(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
null,
'manick_external_id'
);
addAttributeToSet
are - entity type ID, attribute set ID, attribute group ID, and attribute code.$attribute = $this->customerSetup->getEavConfig()->getAttribute(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
'manick_external_id'
);
$attribute->setData('used_in_forms', [
'adminhtml_customer'
]);
$this->attributeResource->save($attribute);
public static function getDependencies(): array
{
return [];
}
public function getAliases(): array
{
return [];
}
getDependencies
method should return a list of patches which should be run before this patch, if any dependencies exist. The getAliases
method should return the list of previous patch names in case this was renamed so that Magento knows not to run this again since patches are run only once.bin/magento setup:upgrade
and see our new customer attribute in the Magento Admin.