Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. /** @var MagentoEavApiDataAttributeInterface $attribute */
  2. $attribute->setData('used_in_forms', ['adminhtml_customer']);
  3. $attributeResource->save($attribute);
  4.  
  5. public function save(MagentoEavApiDataAttributeInterface $attribute)
  6. {
  7. try {
  8. /** @var MagentoEavModelResourceModelEntityAttribute */
  9. $this->eavResource->save($attribute);
  10. } catch (Exception $e) {
  11. ...
  12. }
  13.  
  14. use MagentoCustomerApiAddressMetadataInterface;
  15. use MagentoEavApiAttributeRepositoryInterface;
  16. use MagentoEavModelConfig as EavConfig;
  17. use MagentoEavSetupEavSetupFactory;
  18. use MagentoFrameworkSetupModuleDataSetupInterface;
  19. use MagentoFrameworkSetupPatchDataPatchInterface;
  20.  
  21. ....
  22. /**
  23. * @param ModuleDataSetupInterface $moduleDataSetup
  24. * @param EavSetupFactory $eavSetupFactory
  25. * @param EavConfig $eavConfig
  26. * @param AttributeRepositoryInterface $attributeRepository
  27. */
  28. public function __construct(
  29. ModuleDataSetupInterface $moduleDataSetup,
  30. EavSetupFactory $eavSetupFactory,
  31. EavConfig $eavConfig,
  32. AttributeRepositoryInterface $attributeRepository
  33. ) {
  34. $this->eavSetupFactory = $eavSetupFactory;
  35. $this->moduleDataSetup = $moduleDataSetup;
  36. $this->eavConfig = $eavConfig;
  37. $this->attributeRepository = $attributeRepository;
  38. }
  39.  
  40. /**
  41. * @inheritdoc
  42. */
  43. public function apply()
  44. {
  45. /** @var MagentoEavSetupEavSetup $eavSetup */
  46. $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
  47.  
  48. $attributeCode = 'test_attribute';
  49.  
  50. $entityTypeId = AddressMetadataInterface::ENTITY_TYPE_ADDRESS;
  51. $setId = AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS;
  52. $eavSetup->addAttribute($entityTypeId, $attributeCode, [
  53. 'type' => 'int',
  54. 'input' => 'boolean',
  55. 'label' => 'Test attribute',
  56. 'required' => 0,
  57. 'user_defined' => 1,
  58. 'default' => 0,
  59. 'system' => 0,
  60. 'position' => 60,
  61. ]);
  62.  
  63. $eavSetup->addAttributeToSet($entityTypeId, $setId, null, $attributeCode);
  64.  
  65. $attribute = $this->eavConfig->getAttribute($entityTypeId, $attributeCode);
  66. $attribute->setData('used_in_forms', [
  67. 'adminhtml_customer_address',
  68. 'customer_address_edit',
  69. 'customer_register_address'
  70. ]);
  71.  
  72. // works. deprecated, I remember
  73. // $attribute->getResource()->save($attribute);
  74.  
  75. // the attribute is saved, but the information on the forms in which the attribute is added is not saved
  76. $this->attributeRepository->save($attribute);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement