Skip to content

Commit d906888

Browse files
author
Oleksii Korshenko
authored
MAGETWO-85696: Re saving product attribute [backport 2.1] #11618
2 parents ca8ee57 + 17450ff commit d906888

File tree

2 files changed

+83
-60
lines changed
  • app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute
  • dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product

2 files changed

+83
-60
lines changed

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

Lines changed: 81 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,95 @@
55
* See COPYING.txt for license details.
66
*/
77

8-
// @codingStandardsIgnoreFile
9-
108
namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;
119

12-
use Magento\Framework\Exception\AlreadyExistsException;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Backend\Model\View\Result\Redirect;
12+
use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
13+
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
14+
use Magento\Catalog\Helper\Product;
15+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
16+
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
17+
use Magento\Eav\Model\Entity\Attribute\Set;
18+
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator;
19+
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
20+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
21+
use Magento\Framework\Cache\FrontendInterface;
1322
use Magento\Framework\Controller\ResultFactory;
23+
use Magento\Framework\Controller\Result\Json;
24+
use Magento\Framework\Exception\AlreadyExistsException;
25+
use Magento\Framework\Exception\LocalizedException;
26+
use Magento\Framework\Filter\FilterManager;
27+
use Magento\Framework\Registry;
28+
use Magento\Framework\View\LayoutFactory;
29+
use Magento\Framework\View\Result\PageFactory;
1430

1531
/**
1632
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1733
*/
18-
class Save extends \Magento\Catalog\Controller\Adminhtml\Product\Attribute
34+
class Save extends Attribute
1935
{
2036
/**
21-
* @var \Magento\Catalog\Model\Product\AttributeSet\BuildFactory
37+
* @var BuildFactory
2238
*/
2339
protected $buildFactory;
2440

2541
/**
26-
* @var \Magento\Framework\Filter\FilterManager
42+
* @var FilterManager
2743
*/
2844
protected $filterManager;
2945

3046
/**
31-
* @var \Magento\Catalog\Helper\Product
47+
* @var Product
3248
*/
3349
protected $productHelper;
3450

3551
/**
36-
* @var \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory
52+
* @var AttributeFactory
3753
*/
3854
protected $attributeFactory;
3955

4056
/**
41-
* @var \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory
57+
* @var ValidatorFactory
4258
*/
4359
protected $validatorFactory;
4460

4561
/**
46-
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
62+
* @var CollectionFactory
4763
*/
4864
protected $groupCollectionFactory;
4965

5066
/**
51-
* @var \Magento\Framework\View\LayoutFactory
67+
* @var LayoutFactory
5268
*/
5369
private $layoutFactory;
5470

5571
/**
56-
* @param \Magento\Backend\App\Action\Context $context
57-
* @param \Magento\Framework\Cache\FrontendInterface $attributeLabelCache
58-
* @param \Magento\Framework\Registry $coreRegistry
59-
* @param \Magento\Catalog\Model\Product\AttributeSet\BuildFactory $buildFactory
60-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
61-
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
62-
* @param \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory
63-
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory
64-
* @param \Magento\Framework\Filter\FilterManager $filterManager
65-
* @param \Magento\Catalog\Helper\Product $productHelper
66-
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
72+
* @param Context $context
73+
* @param FrontendInterface $attributeLabelCache
74+
* @param Registry $coreRegistry
75+
* @param BuildFactory $buildFactory
76+
* @param PageFactory $resultPageFactory
77+
* @param AttributeFactory $attributeFactory
78+
* @param ValidatorFactory $validatorFactory
79+
* @param CollectionFactory $groupCollectionFactory
80+
* @param FilterManager $filterManager
81+
* @param Product $productHelper
82+
* @param LayoutFactory $layoutFactory
6783
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6884
*/
6985
public function __construct(
70-
\Magento\Backend\App\Action\Context $context,
71-
\Magento\Framework\Cache\FrontendInterface $attributeLabelCache,
72-
\Magento\Framework\Registry $coreRegistry,
73-
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
74-
\Magento\Catalog\Model\Product\AttributeSet\BuildFactory $buildFactory,
75-
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory,
76-
\Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory,
77-
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
78-
\Magento\Framework\Filter\FilterManager $filterManager,
79-
\Magento\Catalog\Helper\Product $productHelper,
80-
\Magento\Framework\View\LayoutFactory $layoutFactory
86+
Context $context,
87+
FrontendInterface $attributeLabelCache,
88+
Registry $coreRegistry,
89+
PageFactory $resultPageFactory,
90+
BuildFactory $buildFactory,
91+
AttributeFactory $attributeFactory,
92+
ValidatorFactory $validatorFactory,
93+
CollectionFactory $groupCollectionFactory,
94+
FilterManager $filterManager,
95+
Product $productHelper,
96+
LayoutFactory $layoutFactory
8197
) {
8298
parent::__construct($context, $attributeLabelCache, $coreRegistry, $resultPageFactory);
8399
$this->buildFactory = $buildFactory;
@@ -90,7 +106,7 @@ public function __construct(
90106
}
91107

92108
/**
93-
* @return \Magento\Backend\Model\View\Result\Redirect
109+
* @return Redirect
94110
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
95111
* @SuppressWarnings(PHPMD.NPathComplexity)
96112
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -107,31 +123,43 @@ public function execute()
107123
$name = trim($name);
108124

109125
try {
110-
/** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
126+
/** @var $attributeSet Set */
111127
$attributeSet = $this->buildFactory->create()
112128
->setEntityTypeId($this->_entityTypeId)
113129
->setSkeletonId($setId)
114130
->setName($name)
115131
->getAttributeSet();
116-
117132
} catch (AlreadyExistsException $alreadyExists) {
118-
$this->messageManager->addError(__('An attribute set named \'%1\' already exists.', $name));
133+
$this->messageManager->addErrorMessage(__('An attribute set named \'%1\' already exists.', $name));
119134
$this->_session->setAttributeData($data);
120135
return $this->returnResult('catalog/*/edit', ['_current' => true], ['error' => true]);
121-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
122-
$this->messageManager->addError($e->getMessage());
136+
} catch (LocalizedException $e) {
137+
$this->messageManager->addErrorMessage($e->getMessage());
123138
} catch (\Exception $e) {
124-
$this->messageManager->addException($e, __('Something went wrong while saving the attribute.'));
139+
$this->messageManager->addExceptionMessage(
140+
$e,
141+
__('Something went wrong while saving the attribute.')
142+
);
125143
}
126144
}
127145

128146
$attributeId = $this->getRequest()->getParam('attribute_id');
129-
$attributeCode = $this->getRequest()->getParam('attribute_code')
130-
?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
147+
148+
/** @var $model ProductAttributeInterface */
149+
$model = $this->attributeFactory->create();
150+
if ($attributeId) {
151+
$model->load($attributeId);
152+
}
153+
$attributeCode = $model && $model->getId()
154+
? $model->getAttributeCode()
155+
: $this->getRequest()->getParam('attribute_code');
156+
$attributeCode = $attributeCode ?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
131157
if (strlen($attributeCode) > 0) {
132-
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']);
158+
$validatorAttrCode = new \Zend_Validate_Regex(
159+
['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']
160+
);
133161
if (!$validatorAttrCode->isValid($attributeCode)) {
134-
$this->messageManager->addError(
162+
$this->messageManager->addErrorMessage(
135163
__(
136164
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
137165
'numbers (0-9) or underscore(_) in this field, first character should be a letter.',
@@ -149,11 +177,11 @@ public function execute()
149177

150178
//validate frontend_input
151179
if (isset($data['frontend_input'])) {
152-
/** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator */
180+
/** @var $inputType Validator */
153181
$inputType = $this->validatorFactory->create();
154182
if (!$inputType->isValid($data['frontend_input'])) {
155183
foreach ($inputType->getMessages() as $message) {
156-
$this->messageManager->addError($message);
184+
$this->messageManager->addErrorMessage($message);
157185
}
158186
return $this->returnResult(
159187
'catalog/*/edit',
@@ -163,18 +191,14 @@ public function execute()
163191
}
164192
}
165193

166-
/* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
167-
$model = $this->attributeFactory->create();
168-
169194
if ($attributeId) {
170-
$model->load($attributeId);
171195
if (!$model->getId()) {
172-
$this->messageManager->addError(__('This attribute no longer exists.'));
196+
$this->messageManager->addErrorMessage(__('This attribute no longer exists.'));
173197
return $this->returnResult('catalog/*/', [], ['error' => true]);
174198
}
175199
// entity type check
176200
if ($model->getEntityTypeId() != $this->_entityTypeId) {
177-
$this->messageManager->addError(__('We can\'t update the attribute.'));
201+
$this->messageManager->addErrorMessage(__('We can\'t update the attribute.'));
178202
$this->_session->setAttributeData($data);
179203
return $this->returnResult('catalog/*/', [], ['error' => true]);
180204
}
@@ -196,7 +220,7 @@ public function execute()
196220

197221
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []];
198222

199-
if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
223+
if ($model->getIsUserDefined() === null || $model->getIsUserDefined() != 0) {
200224
$data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
201225
}
202226

@@ -242,7 +266,7 @@ public function execute()
242266

243267
try {
244268
$model->save();
245-
$this->messageManager->addSuccess(__('You saved the product attribute.'));
269+
$this->messageManager->addSuccessMessage(__('You saved the product attribute.'));
246270

247271
$this->_attributeLabelCache->clean();
248272
$this->_session->setAttributeData(false);
@@ -253,7 +277,7 @@ public function execute()
253277
'_current' => true,
254278
'product_tab' => $this->getRequest()->getParam('product_tab'),
255279
];
256-
if (!is_null($attributeSet)) {
280+
if ($attributeSet !== null) {
257281
$requestParams['new_attribute_set_id'] = $attributeSet->getId();
258282
}
259283
return $this->returnResult('catalog/product/addAttribute', $requestParams, ['error' => false]);
@@ -266,7 +290,7 @@ public function execute()
266290
}
267291
return $this->returnResult('catalog/*/', [], ['error' => false]);
268292
} catch (\Exception $e) {
269-
$this->messageManager->addError($e->getMessage());
293+
$this->messageManager->addErrorMessage($e->getMessage());
270294
$this->_session->setAttributeData($data);
271295
return $this->returnResult(
272296
'catalog/*/edit',
@@ -282,7 +306,7 @@ public function execute()
282306
* @param string $path
283307
* @param array $params
284308
* @param array $response
285-
* @return \Magento\Framework\Controller\Result\Json|\Magento\Backend\Model\View\Result\Redirect
309+
* @return Json|Redirect
286310
*/
287311
private function returnResult($path = '', array $params = [], array $response = [])
288312
{
@@ -295,7 +319,6 @@ private function returnResult($path = '', array $params = [], array $response =
295319
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($response);
296320
}
297321
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath($path, $params);
298-
299322
}
300323

301324
/**

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/AttributeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ public function testAttributeWithoutId()
128128
*/
129129
public function testWrongAttributeCode()
130130
{
131-
$postData = $this->_getAttributeData() + ['attribute_id' => '2', 'attribute_code' => '_()&&&?'];
131+
$postData = $this->_getAttributeData() + ['attribute_code' => '_()&&&?'];
132132
$this->getRequest()->setPostValue($postData);
133133
$this->dispatch('backend/catalog/product_attribute/save');
134134
$this->assertEquals(302, $this->getResponse()->getHttpResponseCode());
135135
$this->assertContains(
136-
'catalog/product_attribute/edit/attribute_id/2',
136+
'catalog/product_attribute/edit',
137137
$this->getResponse()->getHeader('Location')->getFieldValue()
138138
);
139139
/** @var \Magento\Framework\Message\Collection $messages */

0 commit comments

Comments
 (0)