6
6
7
7
namespace Magento \Eav \Setup ;
8
8
9
+ use Magento \Eav \Model \AttributeFactory ;
10
+ use Magento \Eav \Model \Config ;
11
+ use Magento \Eav \Model \Entity \Attribute ;
9
12
use Magento \Eav \Model \Entity \Setup \Context ;
10
13
use Magento \Eav \Model \Entity \Setup \PropertyMapperInterface ;
14
+ use Magento \Eav \Model \ReservedAttributeChecker ;
11
15
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \CollectionFactory ;
12
16
use Magento \Eav \Model \Validator \Attribute \Code ;
13
17
use Magento \Framework \App \CacheInterface ;
27
31
class EavSetup
28
32
{
29
33
/**
30
- * Cache
31
- *
32
34
* @var CacheInterface
33
35
*/
34
36
private $ cache ;
35
37
36
38
/**
37
- * Attribute group collection factory
38
- *
39
39
* @var CollectionFactory
40
40
*/
41
41
private $ attrGroupCollectionFactory ;
42
42
43
43
/**
44
- * Attribute mapper
45
- *
46
44
* @var PropertyMapperInterface
47
45
*/
48
46
private $ attributeMapper ;
49
47
50
48
/**
51
- * Setup model
52
- *
53
49
* @var ModuleDataSetupInterface
54
50
*/
55
51
private $ setup ;
56
52
57
53
/**
58
- * General Attribute Group Name
59
- *
60
54
* @var string
61
55
*/
62
56
private $ _generalGroupName = 'General ' ;
63
57
64
58
/**
65
- * Default attribute group name to id pairs
66
- *
67
59
* @var array
68
60
*/
69
61
private $ defaultGroupIdAssociations = ['general ' => 1 ];
70
62
71
63
/**
72
- * Default attribute group name
73
- *
74
64
* @var string
75
65
*/
76
66
private $ _defaultGroupName = 'Default ' ;
77
67
78
68
/**
79
- * Default attribute set name
80
- *
81
69
* @var string
82
70
*/
83
71
private $ _defaultAttributeSetName = 'Default ' ;
@@ -92,6 +80,21 @@ class EavSetup
92
80
*/
93
81
private $ attributeCodeValidator ;
94
82
83
+ /**
84
+ * @var ReservedAttributeChecker
85
+ */
86
+ private $ reservedAttributeChecker ;
87
+
88
+ /**
89
+ * @var AttributeFactory
90
+ */
91
+ private $ attributeFactory ;
92
+
93
+ /**
94
+ * @var Config|null
95
+ */
96
+ private $ eavConfig ;
97
+
95
98
/**
96
99
* Init
97
100
*
@@ -101,6 +104,9 @@ class EavSetup
101
104
* @param CollectionFactory $attrGroupCollectionFactory
102
105
* @param Code|null $attributeCodeValidator
103
106
* @param AddOptionToAttribute|null $addAttributeOption
107
+ * @param ReservedAttributeChecker|null $reservedAttributeChecker
108
+ * @param AttributeFactory|null $attributeFactory
109
+ * @param Config|null $eavConfig
104
110
* @SuppressWarnings(PHPMD.LongVariable)
105
111
*/
106
112
public function __construct (
@@ -109,17 +115,22 @@ public function __construct(
109
115
CacheInterface $ cache ,
110
116
CollectionFactory $ attrGroupCollectionFactory ,
111
117
Code $ attributeCodeValidator = null ,
112
- AddOptionToAttribute $ addAttributeOption = null
118
+ AddOptionToAttribute $ addAttributeOption = null ,
119
+ ReservedAttributeChecker $ reservedAttributeChecker = null ,
120
+ AttributeFactory $ attributeFactory = null ,
121
+ Config $ eavConfig = null
113
122
) {
114
123
$ this ->cache = $ cache ;
115
124
$ this ->attrGroupCollectionFactory = $ attrGroupCollectionFactory ;
116
125
$ this ->attributeMapper = $ context ->getAttributeMapper ();
117
126
$ this ->setup = $ setup ;
118
127
$ this ->addAttributeOption = $ addAttributeOption
119
128
?? ObjectManager::getInstance ()->get (AddOptionToAttribute::class);
120
- $ this ->attributeCodeValidator = $ attributeCodeValidator ?: ObjectManager::getInstance ()->get (
121
- Code::class
122
- );
129
+ $ this ->attributeCodeValidator = $ attributeCodeValidator ?? ObjectManager::getInstance ()->get (Code::class);
130
+ $ this ->reservedAttributeChecker = $ reservedAttributeChecker
131
+ ?? ObjectManager::getInstance ()->get (ReservedAttributeChecker::class);
132
+ $ this ->attributeFactory = $ attributeFactory ?? ObjectManager::getInstance ()->get (AttributeFactory::class);
133
+ $ this ->eavConfig = $ eavConfig ?? ObjectManager::getInstance ()->get (Config::class);
123
134
}
124
135
125
136
/**
@@ -1364,7 +1375,7 @@ public function installEntities($entities = null)
1364
1375
1365
1376
foreach ($ entities as $ entityName => $ entity ) {
1366
1377
$ this ->addEntityType ($ entityName , $ entity );
1367
-
1378
+ $ this -> eavConfig -> clear ();
1368
1379
$ frontendPrefix = isset ($ entity ['frontend_prefix ' ]) ? $ entity ['frontend_prefix ' ] : '' ;
1369
1380
$ backendPrefix = isset ($ entity ['backend_prefix ' ]) ? $ entity ['backend_prefix ' ] : '' ;
1370
1381
$ sourcePrefix = isset ($ entity ['source_prefix ' ]) ? $ entity ['source_prefix ' ] : '' ;
@@ -1503,5 +1514,16 @@ private function validateAttributeCode(array $data): void
1503
1514
1504
1515
throw new LocalizedException (__ ($ errorMessage ));
1505
1516
}
1517
+
1518
+ /* Actual attribute is created from data array for compatibility with reserved attribute validator logic */
1519
+ $ attribute = $ this ->attributeFactory ->createAttribute (Attribute::class, $ data );
1520
+ if ($ this ->reservedAttributeChecker ->isReservedAttribute ($ attribute )) {
1521
+ throw new LocalizedException (
1522
+ __ (
1523
+ 'The attribute code \'%1 \' is reserved by system. Please try another attribute code ' ,
1524
+ $ attributeCode
1525
+ )
1526
+ );
1527
+ }
1506
1528
}
1507
1529
}
0 commit comments