Skip to content

Commit 28f2d03

Browse files
committed
Merge pull request #461 from magento-troll/MAGETWO-49154-updated
[Troll] EAV and Store usage changes
2 parents 0cd6b3a + db5a937 commit 28f2d03

File tree

61 files changed

+1445
-574
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1445
-574
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public function __construct(
5252
public function execute()
5353
{
5454
$storeId = (int)$this->getRequest()->getParam('store');
55+
$store = $this->storeManager->getStore($storeId);
56+
$this->storeManager->setCurrentStore($store->getCode());
57+
5558
$categoryId = (int)$this->getRequest()->getParam('id');
5659

5760
if (!$categoryId) {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Catalog\Controller\Adminhtml\Category;
77

8+
use Magento\Store\Model\StoreManagerInterface;
9+
810
/**
911
* Class Save
1012
*
@@ -43,24 +45,32 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
4345
]
4446
];
4547

48+
/**
49+
* @var StoreManagerInterface
50+
*/
51+
private $storeManager;
52+
4653
/**
4754
* Constructor
4855
*
4956
* @param \Magento\Backend\App\Action\Context $context
5057
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
5158
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
5259
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
60+
* @param StoreManagerInterface $storeManager
5361
*/
5462
public function __construct(
5563
\Magento\Backend\App\Action\Context $context,
5664
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
5765
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
58-
\Magento\Framework\View\LayoutFactory $layoutFactory
66+
\Magento\Framework\View\LayoutFactory $layoutFactory,
67+
StoreManagerInterface $storeManager
5968
) {
6069
parent::__construct($context);
6170
$this->resultRawFactory = $resultRawFactory;
6271
$this->resultJsonFactory = $resultJsonFactory;
6372
$this->layoutFactory = $layoutFactory;
73+
$this->storeManager = $storeManager;
6474
}
6575

6676
/**
@@ -104,6 +114,8 @@ public function execute()
104114
$data = $this->stringToBoolConverting($data);
105115
$data = $this->imagePreprocessing($data);
106116
$storeId = isset($data['general']['store_id']) ? $data['general']['store_id'] : null;
117+
$store = $this->storeManager->getStore($storeId);
118+
$this->storeManager->setCurrentStore($store->getCode());
107119
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
108120
if ($data['general']) {
109121
$category->addData($this->_filterCategoryPostData($data['general']));
@@ -163,6 +175,7 @@ public function execute()
163175
if ($category->hasCustomDesignTo()) {
164176
$categoryResource->getAttribute('custom_design_from')->setMaxValue($category->getCustomDesignTo());
165177
}
178+
166179
$validate = $category->validate();
167180
if ($validate !== true) {
168181
foreach ($validate as $code => $error) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function __construct(
4141
*/
4242
public function execute()
4343
{
44+
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
45+
$storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface');
46+
$storeId = (int) $this->getRequest()->getParam('store', 0);
47+
$store = $storeManager->getStore($storeId);
48+
$storeManager->setCurrentStore($store->getCode());
4449
$productId = (int) $this->getRequest()->getParam('id');
4550
$product = $this->productBuilder->build($this->getRequest());
4651

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function initialize(\Magento\Catalog\Model\Product $product)
156156
$product->lockAttribute('media');
157157
}
158158

159-
if ($this->storeManager->hasSingleStore()) {
159+
if ($this->storeManager->hasSingleStore() && empty($product->getWebsiteIds())) {
160160
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
161161
}
162162

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Backend\App\Action;
1010
use Magento\Catalog\Controller\Adminhtml\Product;
11+
use Magento\Store\Model\StoreManagerInterface;
1112

1213
class Save extends \Magento\Catalog\Controller\Adminhtml\Product
1314
{
@@ -37,13 +38,21 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Product
3738
protected $productRepository;
3839

3940
/**
41+
* @var StoreManagerInterface
42+
*/
43+
private $storeManager;
44+
45+
/**
46+
* Save constructor.
47+
*
4048
* @param Action\Context $context
4149
* @param Builder $productBuilder
4250
* @param Initialization\Helper $initializationHelper
4351
* @param \Magento\Catalog\Model\Product\Copier $productCopier
4452
* @param \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager
4553
* @param \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement
4654
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
55+
* @param StoreManagerInterface $storeManager
4756
*/
4857
public function __construct(
4958
\Magento\Backend\App\Action\Context $context,
@@ -52,13 +61,15 @@ public function __construct(
5261
\Magento\Catalog\Model\Product\Copier $productCopier,
5362
\Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager,
5463
\Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement,
55-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
64+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
65+
StoreManagerInterface $storeManager
5666
) {
5767
$this->initializationHelper = $initializationHelper;
5868
$this->productCopier = $productCopier;
5969
$this->productTypeManager = $productTypeManager;
6070
$this->categoryLinkManagement = $categoryLinkManagement;
6171
$this->productRepository = $productRepository;
72+
$this->storeManager = $storeManager;
6273
parent::__construct($context, $productBuilder);
6374
}
6475

@@ -71,7 +82,9 @@ public function __construct(
7182
*/
7283
public function execute()
7384
{
74-
$storeId = $this->getRequest()->getParam('store');
85+
$storeId = $this->getRequest()->getParam('store', 0);
86+
$store = $this->storeManager->getStore($storeId);
87+
$this->storeManager->setCurrentStore($store->getCode());
7588
$redirectBack = $this->getRequest()->getParam('back', false);
7689
$productId = $this->getRequest()->getParam('id');
7790
$resultRedirect = $this->resultRedirectFactory->create();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ public function afterLoad($object)
116116
if ($attributeCode == 'available_sort_by') {
117117
$data = $object->getData($attributeCode);
118118
if ($data) {
119-
$object->setData($attributeCode, explode(',', $data));
119+
if (!is_array($data)) {
120+
$object->setData($attributeCode, explode(',', $data));
121+
} else {
122+
$object->setData($attributeCode, $data);
123+
}
124+
120125
}
121126
}
122127
return $this;

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

Lines changed: 19 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public function __construct(
9797
\Magento\Framework\Event\ManagerInterface $eventManager,
9898
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
9999
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
100-
EntityManager $entityManager,
101100
Category\AggregateCount $aggregateCount,
101+
EntityManager $entityManager,
102102
$data = []
103103
) {
104104
parent::__construct(
@@ -990,96 +990,19 @@ public function countVisible()
990990
/**
991991
* Reset firstly loaded attributes
992992
*
993-
* @param \Magento\Framework\Model\AbstractModel $object
993+
* @param \Magento\Framework\DataObject $object
994994
* @param integer $entityId
995995
* @param array|null $attributes
996996
* @return $this
997997
*/
998998
public function load($object, $entityId, $attributes = [])
999999
{
1000-
\Magento\Framework\Profiler::start('EAV:load_entity');
1001-
/**
1002-
* Load object base row data
1003-
*/
1004-
$this->entityManager->load(CategoryInterface::class, $object, $entityId);
1005-
1000+
$this->_attributes = [];
1001+
$this->loadAttributesMetadata($attributes);
1002+
$object = $this->entityManager->load(CategoryInterface::class, $object, $entityId);
10061003
if (!$this->entityManager->has(\Magento\Catalog\Api\Data\CategoryInterface::class, $entityId)) {
10071004
$object->isObjectNew(true);
10081005
}
1009-
1010-
$this->loadAttributesMetadata($attributes);
1011-
1012-
$this->_loadModelAttributes($object);
1013-
1014-
$object->setOrigData();
1015-
1016-
$this->_afterLoad($object);
1017-
1018-
\Magento\Framework\Profiler::stop('EAV:load_entity');
1019-
return $this;
1020-
}
1021-
1022-
/**
1023-
* Save object collected data
1024-
*
1025-
* @param array $saveData array('newObject', 'entityRow', 'insert', 'update', 'delete')
1026-
* @return $this
1027-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1028-
* @SuppressWarnings(PHPMD.NPathComplexity)
1029-
*/
1030-
protected function _processSaveData($saveData)
1031-
{
1032-
extract($saveData, EXTR_SKIP);
1033-
/**
1034-
* Import variables into the current symbol table from save data array
1035-
*
1036-
* @see \Magento\Eav\Model\Entity\AbstractEntity::_collectSaveData()
1037-
*
1038-
* @var array $entityRow
1039-
* @var \Magento\Framework\Model\AbstractModel $newObject
1040-
* @var array $insert
1041-
* @var array $update
1042-
* @var array $delete
1043-
*/
1044-
1045-
/**
1046-
* Process base row
1047-
*/
1048-
$this->entityManager->save(CategoryInterface::class, $newObject);
1049-
1050-
/**
1051-
* insert attribute values
1052-
*/
1053-
if (!empty($insert)) {
1054-
foreach ($insert as $attributeId => $value) {
1055-
$attribute = $this->getAttribute($attributeId);
1056-
$this->_insertAttribute($newObject, $attribute, $value);
1057-
}
1058-
}
1059-
1060-
/**
1061-
* update attribute values
1062-
*/
1063-
if (!empty($update)) {
1064-
foreach ($update as $attributeId => $v) {
1065-
$attribute = $this->getAttribute($attributeId);
1066-
$this->_updateAttribute($newObject, $attribute, $v['value_id'], $v['value']);
1067-
}
1068-
}
1069-
1070-
/**
1071-
* delete empty attribute values
1072-
*/
1073-
if (!empty($delete)) {
1074-
foreach ($delete as $table => $values) {
1075-
$this->_deleteAttributes($newObject, $table, $values);
1076-
}
1077-
}
1078-
1079-
$this->_processAttributeValues();
1080-
1081-
$newObject->isObjectNew(false);
1082-
10831006
return $this;
10841007
}
10851008

@@ -1088,33 +1011,24 @@ protected function _processSaveData($saveData)
10881011
*/
10891012
public function delete($object)
10901013
{
1091-
try {
1092-
$this->transactionManager->start($this->getConnection());
1093-
if (is_numeric($object)) {
1094-
} elseif ($object instanceof \Magento\Framework\Model\AbstractModel) {
1095-
$object->beforeDelete();
1096-
}
1097-
$this->_beforeDelete($object);
1098-
$this->entityManager->delete(\Magento\Catalog\Api\Data\CategoryInterface::class, $object);
1099-
1100-
$this->_afterDelete($object);
1101-
1102-
if ($object instanceof \Magento\Framework\Model\AbstractModel) {
1103-
$object->isDeleted(true);
1104-
$object->afterDelete();
1105-
}
1106-
$this->transactionManager->commit();
1107-
if ($object instanceof \Magento\Framework\Model\AbstractModel) {
1108-
$object->afterDeleteCommit();
1109-
}
1110-
} catch (\Exception $e) {
1111-
$this->transactionManager->rollBack();
1112-
throw $e;
1113-
}
1014+
$this->entityManager->delete(CategoryInterface::class, $object);
11141015
$this->_eventManager->dispatch(
11151016
'catalog_category_delete_after_done',
11161017
['product' => $object]
11171018
);
11181019
return $this;
11191020
}
1021+
1022+
/**
1023+
* Save entity's attributes into the object's resource
1024+
*
1025+
* @param \Magento\Framework\Model\AbstractModel $object
1026+
* @return $this
1027+
* @throws \Exception
1028+
*/
1029+
public function save(\Magento\Framework\Model\AbstractModel $object)
1030+
{
1031+
$this->entityManager->save(CategoryInterface::class, $object);
1032+
return $this;
1033+
}
11201034
}

0 commit comments

Comments
 (0)