Skip to content

Commit 406cf19

Browse files
committed
Merge remote-tracking branch 'local/ACP2E-981' into PR_21_SEP_2022
2 parents d37bc74 + 328397c commit 406cf19

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ public function execute()
326326
return $this->returnResult('catalog/*/', [], ['error' => false]);
327327
} catch (\Exception $e) {
328328
$this->messageManager->addErrorMessage($e->getMessage());
329+
if ($attributeId === null) {
330+
unset($data['frontend_input']);
331+
}
329332
$this->_session->setAttributeData($data);
330333
return $this->returnResult(
331334
'catalog/*/edit',

app/code/Magento/Eav/Model/Entity/Attribute.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Eav\Model\Entity;
77

8+
use Magento\Eav\Model\ReservedAttributeCheckerInterface;
89
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
910
use Magento\Framework\Api\AttributeValueFactory;
1011
use Magento\Framework\App\ObjectManager;
@@ -29,17 +30,17 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
2930
* The value is defined as 60 because in the flat mode attribute code will be transformed into column name.
3031
* MySQL allows only 64 symbols in column name.
3132
*/
32-
const ATTRIBUTE_CODE_MAX_LENGTH = 60;
33+
public const ATTRIBUTE_CODE_MAX_LENGTH = 60;
3334

3435
/**
3536
* Min accepted length of an attribute code.
3637
*/
37-
const ATTRIBUTE_CODE_MIN_LENGTH = 1;
38+
public const ATTRIBUTE_CODE_MIN_LENGTH = 1;
3839

3940
/**
4041
* Tag to use for attributes caching.
4142
*/
42-
const CACHE_TAG = 'EAV_ATTRIBUTE';
43+
public const CACHE_TAG = 'EAV_ATTRIBUTE';
4344

4445
/**
4546
* Prefix of model events names
@@ -69,6 +70,9 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
6970

7071
/**
7172
* @var \Magento\Catalog\Model\Product\ReservedAttributeList
73+
*
74+
* @deprecated Incorrect direct dependency on Product attribute.
75+
* @see \Magento\Eav\Model\Entity\Attribute::$reservedAttributeChecker
7276
*/
7377
protected $reservedAttributeList;
7478

@@ -87,6 +91,11 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
8791
*/
8892
private $attributeCodeValidator;
8993

94+
/**
95+
* @var ReservedAttributeCheckerInterface|null
96+
*/
97+
private $reservedAttributeChecker;
98+
9099
/**
91100
* @param \Magento\Framework\Model\Context $context
92101
* @param \Magento\Framework\Registry $registry
@@ -108,6 +117,7 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
108117
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
109118
* @param array $data
110119
* @param AttributeCodeValidator|null $attributeCodeValidator
120+
* @param ReservedAttributeCheckerInterface|null $reservedAttributeChecker
111121
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
112122
* @codeCoverageIgnore
113123
*/
@@ -131,7 +141,8 @@ public function __construct(
131141
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
132142
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
133143
array $data = [],
134-
AttributeCodeValidator $attributeCodeValidator = null
144+
AttributeCodeValidator $attributeCodeValidator = null,
145+
?ReservedAttributeCheckerInterface $reservedAttributeChecker = null
135146
) {
136147
parent::__construct(
137148
$context,
@@ -157,6 +168,9 @@ public function __construct(
157168
$this->attributeCodeValidator = $attributeCodeValidator ?: ObjectManager::getInstance()->get(
158169
AttributeCodeValidator::class
159170
);
171+
$this->reservedAttributeChecker = $reservedAttributeChecker ?: ObjectManager::getInstance()->get(
172+
ReservedAttributeCheckerInterface::class
173+
);
160174
}
161175

162176
/**
@@ -251,7 +265,7 @@ public function beforeSave()
251265
}
252266

253267
// prevent overriding product data
254-
if (isset($this->_data['attribute_code']) && $this->reservedAttributeList->isReservedAttribute($this)) {
268+
if (isset($this->_data['attribute_code']) && $this->reservedAttributeChecker->isReservedAttribute($this)) {
255269
throw new LocalizedException(
256270
__(
257271
'The attribute code \'%1\' is reserved by system. Please try another attribute code',
@@ -535,6 +549,7 @@ public function __wakeup()
535549
$this->_localeDate = $objectManager->get(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
536550
$this->_localeResolver = $objectManager->get(\Magento\Framework\Locale\ResolverInterface::class);
537551
$this->reservedAttributeList = $objectManager->get(\Magento\Catalog\Model\Product\ReservedAttributeList::class);
552+
$this->reservedAttributeChecker = $objectManager->get(ReservedAttributeCheckerInterface::class);
538553
$this->dateTimeFormatter = $objectManager->get(DateTimeFormatterInterface::class);
539554
}
540555

app/code/Magento/Eav/Model/ReservedAttributeChecker.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class ReservedAttributeChecker implements ReservedAttributeCheckerInterface
1919
{
2020
/**
21-
* @var ReservedAttributeCheckerInterface[][]
21+
* @var ReservedAttributeCheckerInterface[]
2222
*/
2323
private $validators;
2424

@@ -37,7 +37,8 @@ public function __construct(
3737
public function isReservedAttribute(AbstractAttribute $attribute): bool
3838
{
3939
$isReserved = false;
40-
$validators = $this->validators[$attribute->getEntityType()->getEntityTypeCode()] ?? [];
40+
$entityTypeCode = $this->getAttributeEntityTypeCode($attribute);
41+
$validators = $this->validators[$entityTypeCode] ?? [];
4142
foreach ($validators as $validator) {
4243
$isReserved = $validator->isReservedAttribute($attribute);
4344
if ($isReserved === true) {
@@ -47,4 +48,21 @@ public function isReservedAttribute(AbstractAttribute $attribute): bool
4748

4849
return $isReserved;
4950
}
51+
52+
/**
53+
* Returns attribute entity type code.
54+
*
55+
* @param AbstractAttribute $attribute
56+
* @return string|null
57+
*/
58+
private function getAttributeEntityTypeCode(AbstractAttribute $attribute): ?string
59+
{
60+
try {
61+
$result = $attribute->getEntityType()->getEntityTypeCode();
62+
} catch (LocalizedException $e) {
63+
$result = null;
64+
}
65+
66+
return $result;
67+
}
5068
}

0 commit comments

Comments
 (0)