Created
March 31, 2026 11:38
-
-
Save LouisdeLooze/c692c1a0977179320944ba866b565017 to your computer and use it in GitHub Desktop.
Magento2 issue #40634
This file contains hidden or 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 Vendor\Test\Setup\Patch\Data; | |
| use Magento\Customer\Api\CustomerMetadataInterface; | |
| use Magento\Customer\Model\Customer; | |
| use Magento\Customer\Model\ResourceModel\Attribute; | |
| use Magento\Eav\Model\Config; | |
| use Magento\Eav\Setup\EavSetup; | |
| use Magento\Eav\Setup\EavSetupFactory; | |
| use Magento\Framework\Setup\ModuleDataSetupInterface; | |
| use Magento\Framework\Setup\Patch\DataPatchInterface; | |
| class AddTestAttributeCustomerAttribute implements DataPatchInterface | |
| { | |
| /** | |
| * @var ModuleDataSetupInterface | |
| */ | |
| private ModuleDataSetupInterface $moduleDataSetup; | |
| /** | |
| * @var EavSetupFactory | |
| */ | |
| private EavSetupFactory $eavSetupFactory; | |
| /** | |
| * @var Config | |
| */ | |
| private Config $eavConfig; | |
| /** | |
| * @var Attribute | |
| */ | |
| private Attribute $attributeResource; | |
| /** | |
| * @param ModuleDataSetupInterface $moduleDataSetup | |
| * @param EavSetupFactory $eavSetupFactory | |
| * @param Config $eavConfig | |
| * @param Attribute $attributeResource | |
| */ | |
| public function __construct( | |
| ModuleDataSetupInterface $moduleDataSetup, | |
| EavSetupFactory $eavSetupFactory, | |
| Config $eavConfig, | |
| Attribute $attributeResource | |
| ) | |
| { | |
| $this->moduleDataSetup = $moduleDataSetup; | |
| $this->eavSetupFactory = $eavSetupFactory; | |
| $this->eavConfig = $eavConfig; | |
| $this->attributeResource = $attributeResource; | |
| } | |
| /** | |
| * Run code inside patch | |
| * If code fails, patch must be reverted, in case when we are speaking about schema - then under revert | |
| * means run PatchInterface::revert() | |
| * | |
| * If we speak about data, under revert means: $transaction->rollback() | |
| * | |
| * @return $this | |
| */ | |
| public function apply() | |
| { | |
| /** @var EavSetup $eavSetup */ | |
| $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); | |
| $eavSetup->addAttribute( | |
| Customer::ENTITY, | |
| 'test_attribute', | |
| [ | |
| 'is_visible_in_grid' => false, | |
| 'visible' => true, | |
| 'label' => 'Test Attribute', | |
| 'type' => 'int', | |
| 'is_used_in_grid' => false, | |
| 'required' => false, | |
| 'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class, | |
| 'input' => 'select', | |
| 'user_defined' => true, | |
| 'is_filterable_in_grid' => false, | |
| 'system' => false, | |
| 'position' => 10, | |
| 'option' => [ | |
| 'values' => ['0', '1', '2'], | |
| ] | |
| ] | |
| ); | |
| $eavSetup->addAttributeToSet( | |
| CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, | |
| CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, | |
| 'Default', | |
| 'test_attribute' | |
| ); | |
| $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'test_attribute'); | |
| $optionId = null; | |
| foreach ($attribute->getSource()->getAllOptions() as $option) { | |
| if ($option['label'] === '0') { | |
| $optionId = $option['value']; | |
| break; | |
| } | |
| } | |
| $attribute->setDefaultValue($optionId); | |
| $attribute->setData( | |
| 'used_in_forms', | |
| ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'], | |
| ); | |
| $this->attributeResource->save($attribute); | |
| return $this; | |
| } | |
| /** | |
| * 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() | |
| { | |
| return []; | |
| } | |
| /** | |
| * Get aliases (previous names) for the patch. | |
| * | |
| * @return string[] | |
| */ | |
| public function getAliases() | |
| { | |
| return []; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment