Skip to content

Commit dd5610d

Browse files
committed
MAGETWO-49618: [GitHub] Product Save & Duplicate Unexpected Behavior for Multiple Store Views #3426
1 parent 5409bf2 commit dd5610d

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ private function getCategoryLinkManagement()
250250

251251
/**
252252
* @return StoreManagerInterface
253+
* @deprecated
253254
*/
254255
private function getStoreManager()
255256
{
@@ -264,6 +265,7 @@ private function getStoreManager()
264265
* Retrieve data persistor
265266
*
266267
* @return DataPersistorInterface|mixed
268+
* @deprecated
267269
*/
268270
protected function getDataPersistor()
269271
{

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Backend\App\Action;
1010
use Magento\Catalog\Controller\Adminhtml\Product;
1111
use Magento\Framework\App\ObjectManager;
12+
use Magento\Store\Model\StoreManagerInterface;
1213

1314
/**
1415
* Product validate
@@ -47,6 +48,11 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
4748
*/
4849
protected $initializationHelper;
4950

51+
/**
52+
* @var StoreManagerInterface
53+
*/
54+
private $storeManager;
55+
5056
/**
5157
* @param Action\Context $context
5258
* @param Builder $productBuilder
@@ -91,10 +97,12 @@ public function execute()
9197
if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) {
9298
$productData['stock_data']['use_config_manage_stock'] = 0;
9399
}
100+
$storeId = $this->getRequest()->getParam('store', 0);
101+
$store = $this->getStoreManager()->getStore($storeId);
102+
$this->getStoreManager()->setCurrentStore($store->getCode());
94103
/* @var $product \Magento\Catalog\Model\Product */
95104
$product = $this->productFactory->create();
96105
$product->setData('_edit_mode', true);
97-
$storeId = $this->getRequest()->getParam('store');
98106
if ($storeId) {
99107
$product->setStoreId($storeId);
100108
}
@@ -137,6 +145,19 @@ public function execute()
137145
return $this->resultJsonFactory->create()->setData($response);
138146
}
139147

148+
/**
149+
* @return StoreManagerInterface
150+
* @deprecated
151+
*/
152+
private function getStoreManager()
153+
{
154+
if (null === $this->storeManager) {
155+
$this->storeManager = \Magento\Framework\App\ObjectManager::getInstance()
156+
->get('Magento\Store\Model\StoreManagerInterface');
157+
}
158+
return $this->storeManager;
159+
}
160+
140161
/**
141162
* @return Initialization\Helper
142163
* @deprecated

app/code/Magento/Catalog/Plugin/Model/Attribute/Backend/AttributeValidation.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,47 @@
55
*/
66
namespace Magento\Catalog\Plugin\Model\Attribute\Backend;
77

8+
use Magento\Store\Model\Store;
9+
810
class AttributeValidation
911
{
12+
/** @var \Magento\Store\Model\StoreManagerInterface */
13+
private $storeManager;
14+
15+
/**
16+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
17+
*/
18+
public function __construct(
19+
\Magento\Store\Model\StoreManagerInterface $storeManager,
20+
$allowedEntityTypes = []
21+
) {
22+
$this->storeManager = $storeManager;
23+
$this->allowedEntityTypes = $allowedEntityTypes;
24+
}
25+
1026
/**
1127
* @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject
1228
* @param \Closure $proceed
13-
* @param \Magento\Framework\DataObject $product
29+
* @param \Magento\Framework\DataObject $entity
1430
* @return bool
1531
*/
1632
public function aroundValidate(
1733
\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject,
1834
\Closure $proceed,
19-
\Magento\Framework\DataObject $product
35+
\Magento\Framework\DataObject $entity
2036
) {
21-
$attrCode = $subject->getAttribute()->getAttributeCode();
22-
// Null is meaning "no value" which should be overridden by value from default scope
23-
if (array_key_exists($attrCode, $product->getData()) && $product->getData($attrCode) === null) {
24-
return true;
37+
$isAllowedType = !empty(array_filter(array_map(function($allowedEntity) use ($entity) {
38+
return $entity instanceof $allowedEntity;
39+
}, $this->allowedEntityTypes)));
40+
41+
if ($isAllowedType && $this->storeManager->getStore()->getId() !== Store::DEFAULT_STORE_ID) {
42+
$attrCode = $subject->getAttribute()->getAttributeCode();
43+
// Null is meaning "no value" which should be overridden by value from default scope
44+
if (array_key_exists($attrCode, $entity->getData()) && $entity->getData($attrCode) === null) {
45+
return true;
46+
}
2547
}
26-
return $proceed($product);
48+
49+
return $proceed($entity);
2750
}
2851
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,14 @@
473473
<type name="Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend">
474474
<plugin name="attributeValidation" type="Magento\Catalog\Plugin\Model\Attribute\Backend\AttributeValidation"/>
475475
</type>
476+
<type name="Magento\Catalog\Plugin\Model\Attribute\Backend\AttributeValidation">
477+
<arguments>
478+
<argument name="allowedEntityTypes" xsi:type="array">
479+
<item name="product" xsi:type="string">Magento\Catalog\Api\Data\ProductInterface</item>
480+
<item name="category" xsi:type="string">Magento\Catalog\Api\Data\CategoryInterface</item>
481+
</argument>
482+
</arguments>
483+
</type>
476484
<type name="Magento\Catalog\Model\CategoryRepository">
477485
<arguments>
478486
<argument name="categoryResource" xsi:type="object">Magento\Catalog\Model\ResourceModel\Category\Proxy</argument>

0 commit comments

Comments
 (0)