|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Foo\Bar\Setup\Patch\Data; |
| 4 | + |
| 5 | +use Magento\Eav\Setup\EavSetup; |
| 6 | +use Magento\Eav\Setup\EavSetupFactory; |
| 7 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 8 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 9 | + |
| 10 | +class AddAppliedToAttribute implements DataPatchInterface |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * @var ModuleDataSetupInterface |
| 15 | + */ |
| 16 | + private $moduleDataSetup; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var EavSetupFactory |
| 20 | + */ |
| 21 | + private $eavSetupFactory; |
| 22 | + |
| 23 | + /** |
| 24 | + * AddRecommendedAttribute constructor. |
| 25 | + * |
| 26 | + * @param ModuleDataSetupInterface $moduleDataSetup |
| 27 | + * @param EavSetupFactory $eavSetupFactory |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + ModuleDataSetupInterface $moduleDataSetup, |
| 31 | + EavSetupFactory $eavSetupFactory |
| 32 | + ) |
| 33 | + { |
| 34 | + $this->moduleDataSetup = $moduleDataSetup; |
| 35 | + $this->eavSetupFactory = $eavSetupFactory; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Get array of patches that have to be executed prior to this. |
| 40 | + * |
| 41 | + * Example of implementation: |
| 42 | + * |
| 43 | + * [ |
| 44 | + * \Vendor_Name\Module_Name\Setup\Patch\Patch1::class, |
| 45 | + * \Vendor_Name\Module_Name\Setup\Patch\Patch2::class |
| 46 | + * ] |
| 47 | + * |
| 48 | + * @return string[] |
| 49 | + */ |
| 50 | + public static function getDependencies() |
| 51 | + { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get aliases (previous names) for the patch. |
| 57 | + * |
| 58 | + * @return string[] |
| 59 | + */ |
| 60 | + public function getAliases() |
| 61 | + { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Run code inside patch |
| 67 | + * If code fails, patch must be reverted, in case when we are speaking about schema - then under revert |
| 68 | + * means run PatchInterface::revert() |
| 69 | + * |
| 70 | + * If we speak about data, under revert means: $transaction->rollback() |
| 71 | + * |
| 72 | + * @return $this |
| 73 | + */ |
| 74 | + public function apply() |
| 75 | + { |
| 76 | + /** @var EavSetup $eavSetup */ |
| 77 | + $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); |
| 78 | + |
| 79 | + $eavSetup->addAttribute( |
| 80 | + \Magento\Catalog\Model\Product::ENTITY, |
| 81 | + 'applied_to_attribute', |
| 82 | + [ |
| 83 | + 'is_visible_in_grid' => false, |
| 84 | + 'is_html_allowed_on_front' => false, |
| 85 | + 'visible_on_front' => false, |
| 86 | + 'visible' => true, |
| 87 | + 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, |
| 88 | + 'label' => 'Test Label', |
| 89 | + 'source' => null, |
| 90 | + 'type' => 'static', |
| 91 | + 'is_used_in_grid' => false, |
| 92 | + 'required' => false, |
| 93 | + 'input' => 'text', |
| 94 | + 'is_filterable_in_grid' => false, |
| 95 | + 'apply_to' => 'configurable,simple', |
| 96 | + 'sort_order' => 10, |
| 97 | + 'group' => 'General', |
| 98 | + ] |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
0 commit comments