Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @var MagentoEavApiDataAttributeInterface $attribute */
- $attribute->setData('used_in_forms', ['adminhtml_customer']);
- $attributeResource->save($attribute);
- public function save(MagentoEavApiDataAttributeInterface $attribute)
- {
- try {
- /** @var MagentoEavModelResourceModelEntityAttribute */
- $this->eavResource->save($attribute);
- } catch (Exception $e) {
- ...
- }
- use MagentoCustomerApiAddressMetadataInterface;
- use MagentoEavApiAttributeRepositoryInterface;
- use MagentoEavModelConfig as EavConfig;
- use MagentoEavSetupEavSetupFactory;
- use MagentoFrameworkSetupModuleDataSetupInterface;
- use MagentoFrameworkSetupPatchDataPatchInterface;
- ....
- /**
- * @param ModuleDataSetupInterface $moduleDataSetup
- * @param EavSetupFactory $eavSetupFactory
- * @param EavConfig $eavConfig
- * @param AttributeRepositoryInterface $attributeRepository
- */
- public function __construct(
- ModuleDataSetupInterface $moduleDataSetup,
- EavSetupFactory $eavSetupFactory,
- EavConfig $eavConfig,
- AttributeRepositoryInterface $attributeRepository
- ) {
- $this->eavSetupFactory = $eavSetupFactory;
- $this->moduleDataSetup = $moduleDataSetup;
- $this->eavConfig = $eavConfig;
- $this->attributeRepository = $attributeRepository;
- }
- /**
- * @inheritdoc
- */
- public function apply()
- {
- /** @var MagentoEavSetupEavSetup $eavSetup */
- $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
- $attributeCode = 'test_attribute';
- $entityTypeId = AddressMetadataInterface::ENTITY_TYPE_ADDRESS;
- $setId = AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS;
- $eavSetup->addAttribute($entityTypeId, $attributeCode, [
- 'type' => 'int',
- 'input' => 'boolean',
- 'label' => 'Test attribute',
- 'required' => 0,
- 'user_defined' => 1,
- 'default' => 0,
- 'system' => 0,
- 'position' => 60,
- ]);
- $eavSetup->addAttributeToSet($entityTypeId, $setId, null, $attributeCode);
- $attribute = $this->eavConfig->getAttribute($entityTypeId, $attributeCode);
- $attribute->setData('used_in_forms', [
- 'adminhtml_customer_address',
- 'customer_address_edit',
- 'customer_register_address'
- ]);
- // works. deprecated, I remember
- // $attribute->getResource()->save($attribute);
- // the attribute is saved, but the information on the forms in which the attribute is added is not saved
- $this->attributeRepository->save($attribute);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement