|
7 | 7 |
|
8 | 8 | use Magento\Catalog\Api\Data\ProductAttributeInterface;
|
9 | 9 | use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
|
| 10 | +use Magento\Eav\Model\Entity\AttributeCache; |
10 | 11 | use Magento\Framework\Setup\UpgradeDataInterface;
|
11 | 12 | use Magento\Framework\Setup\ModuleContextInterface;
|
12 | 13 | use Magento\Framework\Setup\ModuleDataSetupInterface;
|
@@ -34,16 +35,26 @@ class UpgradeData implements UpgradeDataInterface
|
34 | 35 | */
|
35 | 36 | private $eavSetupFactory;
|
36 | 37 |
|
| 38 | + /** |
| 39 | + * @var AttributeCache |
| 40 | + */ |
| 41 | + private $attributeCache; |
| 42 | + |
37 | 43 | /**
|
38 | 44 | * Init
|
39 | 45 | *
|
40 | 46 | * @param CategorySetupFactory $categorySetupFactory
|
41 | 47 | * @param EavSetupFactory $eavSetupFactory
|
| 48 | + * @param AttributeCache $attributeCache |
42 | 49 | */
|
43 |
| - public function __construct(CategorySetupFactory $categorySetupFactory, EavSetupFactory $eavSetupFactory) |
44 |
| - { |
| 50 | + public function __construct( |
| 51 | + CategorySetupFactory $categorySetupFactory, |
| 52 | + EavSetupFactory $eavSetupFactory, |
| 53 | + AttributeCache $attributeCache |
| 54 | + ) { |
45 | 55 | $this->categorySetupFactory = $categorySetupFactory;
|
46 | 56 | $this->eavSetupFactory = $eavSetupFactory;
|
| 57 | + $this->attributeCache = $attributeCache; |
47 | 58 | }
|
48 | 59 |
|
49 | 60 | /**
|
@@ -135,19 +146,24 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
|
135 | 146 | }
|
136 | 147 |
|
137 | 148 | if (version_compare($context->getVersion(), '2.0.4') < 0) {
|
| 149 | + $mediaBackendType = 'static'; |
| 150 | + $mediaBackendModel = null; |
138 | 151 | /** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
|
139 | 152 | $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
|
140 | 153 | $categorySetup->updateAttribute(
|
141 | 154 | 'catalog_product',
|
142 | 155 | 'media_gallery',
|
143 | 156 | 'backend_type',
|
144 |
| - 'static' |
| 157 | + $mediaBackendType |
145 | 158 | );
|
146 | 159 | $categorySetup->updateAttribute(
|
147 | 160 | 'catalog_product',
|
148 | 161 | 'media_gallery',
|
149 |
| - 'backend_model' |
| 162 | + 'backend_model', |
| 163 | + $mediaBackendModel |
150 | 164 | );
|
| 165 | + |
| 166 | + $this->changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel); |
151 | 167 | }
|
152 | 168 |
|
153 | 169 | if (version_compare($context->getVersion(), '2.0.5', '<')) {
|
@@ -340,10 +356,10 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
|
340 | 356 | ]
|
341 | 357 | );
|
342 | 358 | }
|
343 |
| - |
| 359 | + |
344 | 360 | if (version_compare($context->getVersion(), '2.0.7') < 0) {
|
345 | 361 | /** @var EavSetup $eavSetup */
|
346 |
| - $eavSetup= $this->eavSetupFactory->create(['setup' => $setup]); |
| 362 | + $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); |
347 | 363 |
|
348 | 364 | $eavSetup->updateAttribute(
|
349 | 365 | ProductAttributeInterface::ENTITY_TYPE_CODE,
|
@@ -381,4 +397,25 @@ private function changePriceAttributeDefaultScope($categorySetup)
|
381 | 397 |
|
382 | 398 | }
|
383 | 399 | }
|
| 400 | + |
| 401 | + /** |
| 402 | + * @param string $mediaBackendType |
| 403 | + * @param string $mediaBackendModel |
| 404 | + * @return void |
| 405 | + */ |
| 406 | + private function changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel) |
| 407 | + { |
| 408 | + // need to do, because media_gallery has backend model in cache. |
| 409 | + $catalogProductAttributes = $this->attributeCache->getAttributes('catalog_product', '0-0'); |
| 410 | + |
| 411 | + if (is_array($catalogProductAttributes)) { |
| 412 | + /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $catalogProductAttribute */ |
| 413 | + foreach ($catalogProductAttributes as $catalogProductAttribute) { |
| 414 | + if ($catalogProductAttribute->getAttributeCode() == 'media_gallery') { |
| 415 | + $catalogProductAttribute->setBackendModel($mediaBackendModel); |
| 416 | + $catalogProductAttribute->setBackendType($mediaBackendType); |
| 417 | + } |
| 418 | + } |
| 419 | + } |
| 420 | + } |
384 | 421 | }
|
0 commit comments