Skip to content

Commit 3fb4c82

Browse files
committed
Fix re saving product attribute
1 parent cb93fe5 commit 3fb4c82

File tree

2 files changed

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

2 files changed

+83
-58
lines changed

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

Lines changed: 81 additions & 56 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

10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Backend\Model\View\Result\Redirect;
12+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
13+
use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
14+
use Magento\Catalog\Helper\Product;
15+
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
16+
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
17+
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator;
18+
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
19+
use Magento\Eav\Model\Entity\Attribute\Set;
20+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
21+
use Magento\Framework\Cache\FrontendInterface;
22+
use Magento\Framework\Controller\Result\Json;
1223
use Magento\Framework\Controller\ResultFactory;
1324
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,30 +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();
116132
} catch (AlreadyExistsException $alreadyExists) {
117-
$this->messageManager->addError(__('An attribute set named \'%1\' already exists.', $name));
133+
$this->messageManager->addErrorMessage(__('An attribute set named \'%1\' already exists.', $name));
118134
$this->_session->setAttributeData($data);
119135
return $this->returnResult('catalog/*/edit', ['_current' => true], ['error' => true]);
120-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
121-
$this->messageManager->addError($e->getMessage());
136+
} catch (LocalizedException $e) {
137+
$this->messageManager->addErrorMessage($e->getMessage());
122138
} catch (\Exception $e) {
123-
$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+
);
124143
}
125144
}
126145

127146
$attributeId = $this->getRequest()->getParam('attribute_id');
128-
$attributeCode = $this->getRequest()->getParam('attribute_code')
129-
?: $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]);
130157
if (strlen($attributeCode) > 0) {
131-
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u']);
158+
$validatorAttrCode = new \Zend_Validate_Regex(
159+
['pattern' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u']
160+
);
132161
if (!$validatorAttrCode->isValid($attributeCode)) {
133-
$this->messageManager->addError(
162+
$this->messageManager->addErrorMessage(
134163
__(
135164
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
136165
'numbers (0-9) or underscore(_) in this field, first character should be a letter.',
@@ -148,11 +177,11 @@ public function execute()
148177

149178
//validate frontend_input
150179
if (isset($data['frontend_input'])) {
151-
/** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator */
180+
/** @var $inputType Validator */
152181
$inputType = $this->validatorFactory->create();
153182
if (!$inputType->isValid($data['frontend_input'])) {
154183
foreach ($inputType->getMessages() as $message) {
155-
$this->messageManager->addError($message);
184+
$this->messageManager->addErrorMessage($message);
156185
}
157186
return $this->returnResult(
158187
'catalog/*/edit',
@@ -162,18 +191,14 @@ public function execute()
162191
}
163192
}
164193

165-
/* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
166-
$model = $this->attributeFactory->create();
167-
168194
if ($attributeId) {
169-
$model->load($attributeId);
170195
if (!$model->getId()) {
171-
$this->messageManager->addError(__('This attribute no longer exists.'));
196+
$this->messageManager->addErrorMessage(__('This attribute no longer exists.'));
172197
return $this->returnResult('catalog/*/', [], ['error' => true]);
173198
}
174199
// entity type check
175200
if ($model->getEntityTypeId() != $this->_entityTypeId) {
176-
$this->messageManager->addError(__('We can\'t update the attribute.'));
201+
$this->messageManager->addErrorMessage(__('We can\'t update the attribute.'));
177202
$this->_session->setAttributeData($data);
178203
return $this->returnResult('catalog/*/', [], ['error' => true]);
179204
}
@@ -193,9 +218,9 @@ public function execute()
193218
);
194219
}
195220

196-
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []];
221+
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0];
197222

198-
if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
223+
if ($model->getIsUserDefined() === null || $model->getIsUserDefined() != 0) {
199224
$data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
200225
}
201226

@@ -241,7 +266,7 @@ public function execute()
241266

242267
try {
243268
$model->save();
244-
$this->messageManager->addSuccess(__('You saved the product attribute.'));
269+
$this->messageManager->addSuccessMessage(__('You saved the product attribute.'));
245270

246271
$this->_attributeLabelCache->clean();
247272
$this->_session->setAttributeData(false);
@@ -252,7 +277,7 @@ public function execute()
252277
'_current' => true,
253278
'product_tab' => $this->getRequest()->getParam('product_tab'),
254279
];
255-
if (!is_null($attributeSet)) {
280+
if ($attributeSet !== null) {
256281
$requestParams['new_attribute_set_id'] = $attributeSet->getId();
257282
}
258283
return $this->returnResult('catalog/product/addAttribute', $requestParams, ['error' => false]);
@@ -265,7 +290,7 @@ public function execute()
265290
}
266291
return $this->returnResult('catalog/*/', [], ['error' => false]);
267292
} catch (\Exception $e) {
268-
$this->messageManager->addError($e->getMessage());
293+
$this->messageManager->addErrorMessage($e->getMessage());
269294
$this->_session->setAttributeData($data);
270295
return $this->returnResult(
271296
'catalog/*/edit',
@@ -281,7 +306,7 @@ public function execute()
281306
* @param string $path
282307
* @param array $params
283308
* @param array $response
284-
* @return \Magento\Framework\Controller\Result\Json|\Magento\Backend\Model\View\Result\Redirect
309+
* @return Json|Redirect
285310
*/
286311
private function returnResult($path = '', array $params = [], array $response = [])
287312
{

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)