Skip to content

Commit b7e059f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-62025' into 2.1.8-develop-pr11
2 parents f4ea010 + 17b7855 commit b7e059f

File tree

1 file changed

+102
-61
lines changed

1 file changed

+102
-61
lines changed

app/code/Magento/Catalog/Setup/UpgradeData.php

Lines changed: 102 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
*/
66
namespace Magento\Catalog\Setup;
77

8-
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9-
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
108
use Magento\Framework\Setup\UpgradeDataInterface;
119
use Magento\Framework\Setup\ModuleContextInterface;
1210
use Magento\Framework\Setup\ModuleDataSetupInterface;
1311
use Magento\Eav\Setup\EavSetup;
14-
use Magento\Eav\Setup\EavSetupFactory;
1512

1613
/**
1714
* Upgrade Data script
@@ -29,20 +26,32 @@ class UpgradeData implements UpgradeDataInterface
2926
/**
3027
* EAV setup factory
3128
*
32-
* @var EavSetupFactory
29+
* @var \Magento\Eav\Setup\EavSetupFactory
3330
*/
3431
private $eavSetupFactory;
3532

33+
/**
34+
* Attributes cache management.
35+
*
36+
* @var \Magento\Eav\Model\Entity\AttributeCache
37+
*/
38+
private $attributeCache;
39+
3640
/**
3741
* Init
3842
*
3943
* @param CategorySetupFactory $categorySetupFactory
40-
* @param EavSetupFactory $eavSetupFactory
44+
* @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
45+
* @param \Magento\Eav\Model\Entity\AttributeCache $attributeCache
4146
*/
42-
public function __construct(CategorySetupFactory $categorySetupFactory, EavSetupFactory $eavSetupFactory)
43-
{
47+
public function __construct(
48+
CategorySetupFactory $categorySetupFactory,
49+
\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
50+
\Magento\Eav\Model\Entity\AttributeCache $attributeCache
51+
) {
4452
$this->categorySetupFactory = $categorySetupFactory;
4553
$this->eavSetupFactory = $eavSetupFactory;
54+
$this->attributeCache = $attributeCache;
4655
}
4756

4857
/**
@@ -87,7 +96,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
8796

8897
if (version_compare($context->getVersion(), '2.0.2') < 0) {
8998
// set new resource model paths
90-
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
99+
/** @var CategorySetup $categorySetup */
91100
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
92101
$categorySetup->updateEntityType(
93102
\Magento\Catalog\Model\Category::ENTITY,
@@ -128,151 +137,157 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
128137
}
129138

130139
if (version_compare($context->getVersion(), '2.0.3') < 0) {
131-
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
140+
/** @var CategorySetup $categorySetup */
132141
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
133142
$categorySetup->updateAttribute(3, 54, 'default_value', 1);
134143
}
135144

136145
if (version_compare($context->getVersion(), '2.0.4') < 0) {
137-
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
146+
$mediaBackendType = 'static';
147+
$mediaBackendModel = null;
148+
/** @var CategorySetup $categorySetup */
138149
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
139150
$categorySetup->updateAttribute(
140151
'catalog_product',
141152
'media_gallery',
142153
'backend_type',
143-
'static'
154+
$mediaBackendType
144155
);
145156
$categorySetup->updateAttribute(
146157
'catalog_product',
147158
'media_gallery',
148-
'backend_model'
159+
'backend_model',
160+
$mediaBackendModel
149161
);
162+
163+
$this->changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel);
150164
}
151165

152166
if (version_compare($context->getVersion(), '2.0.5', '<')) {
153-
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
167+
/** @var CategorySetup $categorySetup */
154168
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
155169

156170
//Product Details tab
157171
$categorySetup->updateAttribute(
158-
ProductAttributeInterface::ENTITY_TYPE_CODE,
172+
\Magento\Catalog\Model\Product::ENTITY,
159173
'status',
160174
'frontend_label',
161175
'Enable Product',
162176
5
163177
);
164178
$categorySetup->updateAttribute(
165-
ProductAttributeInterface::ENTITY_TYPE_CODE,
179+
\Magento\Catalog\Model\Product::ENTITY,
166180
'name',
167181
'frontend_label',
168182
'Product Name'
169183
);
184+
$attributeSetId = $categorySetup->getDefaultAttributeSetId(\Magento\Catalog\Model\Product::ENTITY);
170185
$categorySetup->addAttributeToGroup(
171-
ProductAttributeInterface::ENTITY_TYPE_CODE,
172-
'Default',
186+
\Magento\Catalog\Model\Product::ENTITY,
187+
$attributeSetId,
173188
'Product Details',
174189
'visibility',
175190
80
176191
);
177192
$categorySetup->addAttributeToGroup(
178-
ProductAttributeInterface::ENTITY_TYPE_CODE,
179-
'Default',
193+
\Magento\Catalog\Model\Product::ENTITY,
194+
$attributeSetId,
180195
'Product Details',
181196
'news_from_date',
182197
90
183198
);
184199
$categorySetup->addAttributeToGroup(
185-
ProductAttributeInterface::ENTITY_TYPE_CODE,
186-
'Default',
200+
\Magento\Catalog\Model\Product::ENTITY,
201+
$attributeSetId,
187202
'Product Details',
188203
'news_to_date',
189204
100
190205
);
191206
$categorySetup->addAttributeToGroup(
192-
ProductAttributeInterface::ENTITY_TYPE_CODE,
193-
'Default',
207+
\Magento\Catalog\Model\Product::ENTITY,
208+
$attributeSetId,
194209
'Product Details',
195210
'country_of_manufacture',
196211
110
197212
);
198213

199214
//Content tab
200215
$categorySetup->addAttributeGroup(
201-
ProductAttributeInterface::ENTITY_TYPE_CODE,
202-
'Default',
216+
\Magento\Catalog\Model\Product::ENTITY,
217+
$attributeSetId,
203218
'Content',
204219
15
205220
);
206221
$categorySetup->updateAttributeGroup(
207-
ProductAttributeInterface::ENTITY_TYPE_CODE,
208-
'Default',
222+
\Magento\Catalog\Model\Product::ENTITY,
223+
$attributeSetId,
209224
'Content',
210225
'tab_group_code',
211226
'basic'
212227
);
213228
$categorySetup->addAttributeToGroup(
214-
ProductAttributeInterface::ENTITY_TYPE_CODE,
215-
'Default',
229+
\Magento\Catalog\Model\Product::ENTITY,
230+
$attributeSetId,
216231
'Content',
217232
'description'
218233
);
219234
$categorySetup->addAttributeToGroup(
220-
ProductAttributeInterface::ENTITY_TYPE_CODE,
221-
'Default',
235+
\Magento\Catalog\Model\Product::ENTITY,
236+
$attributeSetId,
222237
'Content',
223238
'short_description',
224239
100
225240
);
226241

227242
//Images tab
228243
$groupId = (int)$categorySetup->getAttributeGroupByCode(
229-
ProductAttributeInterface::ENTITY_TYPE_CODE,
230-
'Default',
244+
\Magento\Catalog\Model\Product::ENTITY,
245+
$attributeSetId,
231246
'image-management',
232247
'attribute_group_id'
233248
);
234249
$categorySetup->addAttributeToGroup(
235-
ProductAttributeInterface::ENTITY_TYPE_CODE,
236-
'Default',
250+
\Magento\Catalog\Model\Product::ENTITY,
251+
$attributeSetId,
237252
$groupId,
238253
'image',
239254
1
240255
);
241256
$categorySetup->updateAttributeGroup(
242-
ProductAttributeInterface::ENTITY_TYPE_CODE,
243-
'Default',
257+
\Magento\Catalog\Model\Product::ENTITY,
258+
$attributeSetId,
244259
$groupId,
245260
'attribute_group_name',
246261
'Images'
247262
);
248263
$categorySetup->updateAttribute(
249-
ProductAttributeInterface::ENTITY_TYPE_CODE,
264+
\Magento\Catalog\Model\Product::ENTITY,
250265
'image',
251266
'frontend_label',
252267
'Base'
253268
);
254269
$categorySetup->updateAttribute(
255-
ProductAttributeInterface::ENTITY_TYPE_CODE,
270+
\Magento\Catalog\Model\Product::ENTITY,
256271
'small_image',
257272
'frontend_label',
258273
'Small'
259274
);
260275
$categorySetup->updateAttribute(
261-
ProductAttributeInterface::ENTITY_TYPE_CODE,
276+
\Magento\Catalog\Model\Product::ENTITY,
262277
'image',
263278
'frontend_input_renderer',
264279
null
265280
);
266281

267282
//Design tab
268283
$categorySetup->updateAttribute(
269-
ProductAttributeInterface::ENTITY_TYPE_CODE,
284+
\Magento\Catalog\Model\Product::ENTITY,
270285
'page_layout',
271286
'frontend_label',
272287
'Layout'
273288
);
274289
$categorySetup->updateAttribute(
275-
ProductAttributeInterface::ENTITY_TYPE_CODE,
290+
\Magento\Catalog\Model\Product::ENTITY,
276291
'custom_layout_update',
277292
'frontend_label',
278293
'Layout Update XML',
@@ -281,70 +296,70 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
281296

282297
//Schedule Design Update tab
283298
$categorySetup->addAttributeGroup(
284-
ProductAttributeInterface::ENTITY_TYPE_CODE,
285-
'Default',
299+
\Magento\Catalog\Model\Product::ENTITY,
300+
$attributeSetId,
286301
'Schedule Design Update',
287302
55
288303
);
289304
$categorySetup->updateAttributeGroup(
290-
ProductAttributeInterface::ENTITY_TYPE_CODE,
291-
'Default',
305+
\Magento\Catalog\Model\Product::ENTITY,
306+
$attributeSetId,
292307
'Schedule Design Update',
293308
'tab_group_code',
294309
'advanced'
295310
);
296311
$categorySetup->addAttributeToGroup(
297-
ProductAttributeInterface::ENTITY_TYPE_CODE,
298-
'Default',
312+
\Magento\Catalog\Model\Product::ENTITY,
313+
$attributeSetId,
299314
'Schedule Design Update',
300315
'custom_design_from',
301316
20
302317
);
303318
$categorySetup->addAttributeToGroup(
304-
ProductAttributeInterface::ENTITY_TYPE_CODE,
305-
'Default',
319+
\Magento\Catalog\Model\Product::ENTITY,
320+
$attributeSetId,
306321
'Schedule Design Update',
307322
'custom_design_to',
308323
30
309324
);
310325
$categorySetup->updateAttribute(
311-
ProductAttributeInterface::ENTITY_TYPE_CODE,
326+
\Magento\Catalog\Model\Product::ENTITY,
312327
'custom_design',
313328
'frontend_label',
314329
'New Theme',
315330
40
316331
);
317332
$categorySetup->addAttributeToGroup(
318-
ProductAttributeInterface::ENTITY_TYPE_CODE,
319-
'Default',
333+
\Magento\Catalog\Model\Product::ENTITY,
334+
$attributeSetId,
320335
'Schedule Design Update',
321336
'custom_design'
322337
);
323338
$categorySetup->addAttribute(
324-
ProductAttributeInterface::ENTITY_TYPE_CODE,
339+
\Magento\Catalog\Model\Product::ENTITY,
325340
'custom_layout',
326341
[
327342
'type' => 'varchar',
328343
'label' => 'New Layout',
329344
'input' => 'select',
330-
'source' => 'Magento\Catalog\Model\Product\Attribute\Source\Layout',
345+
'source' => \Magento\Catalog\Model\Product\Attribute\Source\Layout::class,
331346
'required' => false,
332347
'sort_order' => 50,
333-
'global' => ScopedAttributeInterface::SCOPE_STORE,
348+
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
334349
'group' => 'Schedule Design Update',
335350
'is_used_in_grid' => true,
336351
'is_visible_in_grid' => false,
337352
'is_filterable_in_grid' => false
338353
]
339354
);
340355
}
341-
356+
342357
if (version_compare($context->getVersion(), '2.0.7') < 0) {
343358
/** @var EavSetup $eavSetup */
344-
$eavSetup= $this->eavSetupFactory->create(['setup' => $setup]);
359+
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
345360

346361
$eavSetup->updateAttribute(
347-
ProductAttributeInterface::ENTITY_TYPE_CODE,
362+
\Magento\Catalog\Model\Product::ENTITY,
348363
'meta_description',
349364
[
350365
'note' => 'Maximum 255 chars. Meta Description should optimally be between 150-160 characters'
@@ -353,7 +368,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
353368
}
354369

355370
if (version_compare($context->getVersion(), '2.1.3') < 0) {
356-
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
371+
/** @var CategorySetup $categorySetup */
357372
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
358373
$this->changePriceAttributeDefaultScope($categorySetup);
359374
}
@@ -362,7 +377,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
362377
}
363378

364379
/**
365-
* @param \Magento\Catalog\Setup\CategorySetup $categorySetup
380+
* @param CategorySetup $categorySetup
366381
* @return void
367382
*/
368383
private function changePriceAttributeDefaultScope($categorySetup)
@@ -379,4 +394,30 @@ private function changePriceAttributeDefaultScope($categorySetup)
379394

380395
}
381396
}
397+
398+
/**
399+
* Change media_gallery attribute metadata in cache.
400+
*
401+
* @param string $mediaBackendType
402+
* @param string $mediaBackendModel
403+
* @return void
404+
*/
405+
private function changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel)
406+
{
407+
// need to do, because media_gallery has backend model in cache.
408+
$catalogProductAttributes = $this->attributeCache->getAttributes(
409+
\Magento\Catalog\Model\Product::ENTITY,
410+
'0-0'
411+
);
412+
413+
if (is_array($catalogProductAttributes)) {
414+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $catalogProductAttribute */
415+
foreach ($catalogProductAttributes as $catalogProductAttribute) {
416+
if ($catalogProductAttribute->getAttributeCode() == 'media_gallery') {
417+
$catalogProductAttribute->setBackendModel($mediaBackendModel);
418+
$catalogProductAttribute->setBackendType($mediaBackendType);
419+
}
420+
}
421+
}
422+
}
382423
}

0 commit comments

Comments
 (0)