Skip to content

Commit efc03a2

Browse files
committed
Merge branch '2.2-develop' of github.com:magento/magento2ce into MAGETWO-80789
2 parents c5e76d4 + 178fa2b commit efc03a2

File tree

27 files changed

+924
-170
lines changed

27 files changed

+924
-170
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Gallery.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
namespace Magento\Catalog\Block\Adminhtml\Product\Helper\Form;
1515

16+
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\App\Request\DataPersistorInterface;
1618
use Magento\Framework\Registry;
1719
use Magento\Catalog\Model\Product;
1820
use Magento\Eav\Model\Entity\Attribute;
@@ -68,6 +70,11 @@ class Gallery extends \Magento\Framework\View\Element\AbstractBlock
6870
*/
6971
protected $registry;
7072

73+
/**
74+
* @var DataPersistorInterface
75+
*/
76+
private $dataPersistor;
77+
7178
/**
7279
* @param \Magento\Framework\View\Element\Context $context
7380
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -80,11 +87,13 @@ public function __construct(
8087
\Magento\Store\Model\StoreManagerInterface $storeManager,
8188
Registry $registry,
8289
\Magento\Framework\Data\Form $form,
83-
$data = []
90+
$data = [],
91+
DataPersistorInterface $dataPersistor = null
8492
) {
8593
$this->storeManager = $storeManager;
8694
$this->registry = $registry;
8795
$this->form = $form;
96+
$this->dataPersistor = $dataPersistor ?: ObjectManager::getInstance()->get(DataPersistorInterface::class);
8897
parent::__construct($context, $data);
8998
}
9099

@@ -104,7 +113,24 @@ public function getElementHtml()
104113
*/
105114
public function getImages()
106115
{
107-
return $this->registry->registry('current_product')->getData('media_gallery') ?: null;
116+
$images = $this->registry->registry('current_product')->getData('media_gallery') ?: null;
117+
if ($images === null) {
118+
$images = ((array)$this->dataPersistor->get('catalog_product'))['product']['media_gallery'] ?? null;
119+
}
120+
121+
return $images;
122+
}
123+
124+
/**
125+
* Get value for given type.
126+
*
127+
* @param string $type
128+
* @return string|null
129+
*/
130+
public function getImageValue(string $type)
131+
{
132+
$product = (array)$this->dataPersistor->get('catalog_product');
133+
return $product['product'][$type] ?? null;
108134
}
109135

110136
/**

app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Gallery/Content.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,11 @@ public function getImageTypes()
193193
$imageTypes = [];
194194
foreach ($this->getMediaAttributes() as $attribute) {
195195
/* @var $attribute \Magento\Eav\Model\Entity\Attribute */
196+
$value = $this->getElement()->getDataObject()->getData($attribute->getAttributeCode())
197+
?: $this->getElement()->getImageValue($attribute->getAttributeCode());
196198
$imageTypes[$attribute->getAttributeCode()] = [
197199
'code' => $attribute->getAttributeCode(),
198-
'value' => $this->getElement()->getDataObject()->getData($attribute->getAttributeCode()),
200+
'value' => $value,
199201
'label' => $attribute->getFrontend()->getLabel(),
200202
'scope' => __($this->getElement()->getScopeLabel($attribute)),
201203
'name' => $this->getElement()->getAttributeFieldName($attribute),

app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Validate.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public function execute()
110110
$options
111111
);
112112
$valueOptions = (isset($options['value']) && is_array($options['value'])) ? $options['value'] : [];
113+
foreach (array_keys($valueOptions) as $key) {
114+
if (!empty($options['delete'][$key])) {
115+
unset($valueOptions[$key]);
116+
}
117+
}
113118
$this->checkEmptyOption($response, $valueOptions);
114119
}
115120

app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Catalog\Controller\Adminhtml\Product;
88

99
use Magento\Backend\App\Action;
10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Controller\Adminhtml\Product;
1112
use Magento\Store\Model\StoreManagerInterface;
1213
use Magento\Framework\App\Request\DataPersistorInterface;
@@ -146,11 +147,13 @@ public function execute()
146147
} catch (\Magento\Framework\Exception\LocalizedException $e) {
147148
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
148149
$this->messageManager->addExceptionMessage($e);
150+
$data = $this->persistMediaData($product, $data);
149151
$this->getDataPersistor()->set('catalog_product', $data);
150152
$redirectBack = $productId ? true : 'new';
151153
} catch (\Exception $e) {
152154
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
153155
$this->messageManager->addErrorMessage($e->getMessage());
156+
$data = $this->persistMediaData($product, $data);
154157
$this->getDataPersistor()->set('catalog_product', $data);
155158
$redirectBack = $productId ? true : 'new';
156159
}
@@ -279,4 +282,36 @@ protected function getDataPersistor()
279282

280283
return $this->dataPersistor;
281284
}
285+
286+
/**
287+
* Persist media gallery on error, in order to show already saved images on next run.
288+
*
289+
* @param ProductInterface $product
290+
* @param array $data
291+
* @return array
292+
*/
293+
private function persistMediaData(ProductInterface $product, array $data)
294+
{
295+
$mediaGallery = $product->getData('media_gallery');
296+
if (!empty($mediaGallery['images'])) {
297+
foreach ($mediaGallery['images'] as $key => $image) {
298+
if (!isset($image['new_file'])) {
299+
//Remove duplicates.
300+
unset($mediaGallery['images'][$key]);
301+
}
302+
}
303+
$data['product']['media_gallery'] = $mediaGallery;
304+
$fields = [
305+
'image',
306+
'small_image',
307+
'thumbnail',
308+
'swatch_image',
309+
];
310+
foreach ($fields as $field) {
311+
$data['product'][$field] = $product->getData($field);
312+
}
313+
}
314+
315+
return $data;
316+
}
282317
}

app/code/Magento/Catalog/Model/Category/Attribute.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ class Attribute extends \Magento\Catalog\Model\Entity\Attribute implements
2929
*/
3030
public function getApplyTo()
3131
{
32-
if ($this->getData(self::APPLY_TO)) {
33-
if (is_array($this->getData(self::APPLY_TO))) {
34-
return $this->getData(self::APPLY_TO);
32+
$applyTo = $this->_getData(self::APPLY_TO);
33+
if ($applyTo) {
34+
if (is_array($applyTo)) {
35+
return $applyTo;
3536
}
36-
return explode(',', $this->getData(self::APPLY_TO));
37-
} else {
38-
return [];
37+
return explode(',', $applyTo);
3938
}
39+
40+
return [];
4041
}
4142

4243
/**
@@ -59,7 +60,7 @@ public function setApplyTo($applyTo)
5960
*/
6061
public function getIsWysiwygEnabled()
6162
{
62-
return $this->getData(self::IS_WYSIWYG_ENABLED);
63+
return $this->_getData(self::IS_WYSIWYG_ENABLED);
6364
}
6465

6566
/**
@@ -70,15 +71,15 @@ public function getIsWysiwygEnabled()
7071
*/
7172
public function setIsWysiwygEnabled($isWysiwygEnabled)
7273
{
73-
return $this->getData(self::IS_WYSIWYG_ENABLED, $isWysiwygEnabled);
74+
return $this->setData(self::IS_WYSIWYG_ENABLED, $isWysiwygEnabled);
7475
}
7576

7677
/**
7778
* {@inheritdoc}
7879
*/
7980
public function getIsHtmlAllowedOnFront()
8081
{
81-
return $this->getData(self::IS_HTML_ALLOWED_ON_FRONT);
82+
return $this->_getData(self::IS_HTML_ALLOWED_ON_FRONT);
8283
}
8384

8485
/**
@@ -97,7 +98,7 @@ public function setIsHtmlAllowedOnFront($isHtmlAllowedOnFront)
9798
*/
9899
public function getUsedForSortBy()
99100
{
100-
return $this->getData(self::USED_FOR_SORT_BY);
101+
return $this->_getData(self::USED_FOR_SORT_BY);
101102
}
102103

103104
/**
@@ -116,7 +117,7 @@ public function setUsedForSortBy($usedForSortBy)
116117
*/
117118
public function getIsFilterable()
118119
{
119-
return $this->getData(self::IS_FILTERABLE);
120+
return $this->_getData(self::IS_FILTERABLE);
120121
}
121122

122123
/**
@@ -135,31 +136,31 @@ public function setIsFilterable($isFilterable)
135136
*/
136137
public function getIsFilterableInSearch()
137138
{
138-
return $this->getData(self::IS_FILTERABLE_IN_SEARCH);
139+
return $this->_getData(self::IS_FILTERABLE_IN_SEARCH);
139140
}
140141

141142
/**
142143
* {@inheritdoc}
143144
*/
144145
public function getIsUsedInGrid()
145146
{
146-
return (bool)$this->getData(self::IS_USED_IN_GRID);
147+
return (bool)$this->_getData(self::IS_USED_IN_GRID);
147148
}
148149

149150
/**
150151
* {@inheritdoc}
151152
*/
152153
public function getIsVisibleInGrid()
153154
{
154-
return (bool)$this->getData(self::IS_VISIBLE_IN_GRID);
155+
return (bool)$this->_getData(self::IS_VISIBLE_IN_GRID);
155156
}
156157

157158
/**
158159
* {@inheritdoc}
159160
*/
160161
public function getIsFilterableInGrid()
161162
{
162-
return (bool)$this->getData(self::IS_FILTERABLE_IN_GRID);
163+
return (bool)$this->_getData(self::IS_FILTERABLE_IN_GRID);
163164
}
164165

165166
/**
@@ -170,15 +171,15 @@ public function getIsFilterableInGrid()
170171
*/
171172
public function setIsFilterableInSearch($isFilterableInSearch)
172173
{
173-
return $this->getData(self::IS_FILTERABLE_IN_SEARCH, $isFilterableInSearch);
174+
return $this->setData(self::IS_FILTERABLE_IN_SEARCH, $isFilterableInSearch);
174175
}
175176

176177
/**
177178
* {@inheritdoc}
178179
*/
179180
public function getPosition()
180181
{
181-
return $this->getData(self::POSITION);
182+
return $this->_getData(self::POSITION);
182183
}
183184

184185
/**
@@ -197,7 +198,7 @@ public function setPosition($position)
197198
*/
198199
public function getIsSearchable()
199200
{
200-
return $this->getData(self::IS_SEARCHABLE);
201+
return $this->_getData(self::IS_SEARCHABLE);
201202
}
202203

203204
/**
@@ -216,7 +217,7 @@ public function setIsSearchable($isSearchable)
216217
*/
217218
public function getIsVisibleInAdvancedSearch()
218219
{
219-
return $this->getData(self::IS_VISIBLE_IN_ADVANCED_SEARCH);
220+
return $this->_getData(self::IS_VISIBLE_IN_ADVANCED_SEARCH);
220221
}
221222

222223
/**
@@ -235,7 +236,7 @@ public function setIsVisibleInAdvancedSearch($isVisibleInAdvancedSearch)
235236
*/
236237
public function getIsComparable()
237238
{
238-
return $this->getData(self::IS_COMPARABLE);
239+
return $this->_getData(self::IS_COMPARABLE);
239240
}
240241

241242
/**
@@ -254,7 +255,7 @@ public function setIsComparable($isComparable)
254255
*/
255256
public function getIsUsedForPromoRules()
256257
{
257-
return $this->getData(self::IS_USED_FOR_PROMO_RULES);
258+
return $this->_getData(self::IS_USED_FOR_PROMO_RULES);
258259
}
259260

260261
/**
@@ -273,7 +274,7 @@ public function setIsUsedForPromoRules($isUsedForPromoRules)
273274
*/
274275
public function getIsVisibleOnFront()
275276
{
276-
return $this->getData(self::IS_VISIBLE_ON_FRONT);
277+
return $this->_getData(self::IS_VISIBLE_ON_FRONT);
277278
}
278279

279280
/**
@@ -292,7 +293,7 @@ public function setIsVisibleOnFront($isVisibleOnFront)
292293
*/
293294
public function getUsedInProductListing()
294295
{
295-
return $this->getData(self::USED_IN_PRODUCT_LISTING);
296+
return $this->_getData(self::USED_IN_PRODUCT_LISTING);
296297
}
297298

298299
/**
@@ -311,7 +312,7 @@ public function setUsedInProductListing($usedInProductListing)
311312
*/
312313
public function getIsVisible()
313314
{
314-
return $this->getData(self::IS_VISIBLE);
315+
return $this->_getData(self::IS_VISIBLE);
315316
}
316317

317318
/**
@@ -332,7 +333,7 @@ public function setIsVisible($isVisible)
332333
*/
333334
public function getScope()
334335
{
335-
$scope = $this->getData(self::KEY_IS_GLOBAL);
336+
$scope = $this->_getData(self::KEY_IS_GLOBAL);
336337
if ($scope == self::SCOPE_GLOBAL) {
337338
return self::SCOPE_GLOBAL_TEXT;
338339
} elseif ($scope == self::SCOPE_WEBSITE) {

0 commit comments

Comments
 (0)