1
- <?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento \Catalog \Plugin \Model \Attribute \Backend ;use Magento \Store \Model \Store ;class AttributeValidation { /** * @var \Magento\Store\Model\StoreManagerInterface */ private $ storeManager ; /** * @var array */ private $ allowedEntityTypes ; /** * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param array $allowedEntityTypes */ public function __construct ( \Magento \Store \Model \StoreManagerInterface $ storeManager , $ allowedEntityTypes = [] ) { $ this ->storeManager = $ storeManager ; $ this ->allowedEntityTypes = $ allowedEntityTypes ; } /** * @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject * @param \Closure $proceed * @param \Magento\Framework\DataObject $entity * @throws \Magento\Framework\Exception\NoSuchEntityException * @return bool */ public function aroundValidate ( \Magento \Eav \Model \Entity \Attribute \Backend \AbstractBackend $ subject , \Closure $ proceed , \Magento \Framework \DataObject $ entity ) { $ isAllowedType = !empty (array_filter (array_map (function ($ allowedEntity ) use ($ entity ) { return $ entity instanceof $ allowedEntity ; }, $ this ->allowedEntityTypes ))); if ($ isAllowedType && (int ) $ this ->storeManager ->getStore ()->getId () !== Store::DEFAULT_STORE_ID ) { $ attrCode = $ subject ->getAttribute ()->getAttributeCode (); // Null is meaning "no value" which should be overridden by value from default scope if (array_key_exists ($ attrCode , $ entity ->getData ()) && $ entity ->getData ($ attrCode ) === null ) { return true ; } } return $ proceed ($ entity ); } }
1
+ <?php
2
+ /**
3
+ * Copyright © Magento, Inc. All rights reserved.
4
+ * See COPYING.txt for license details.
5
+ */
6
+ namespace Magento \Catalog \Plugin \Model \Attribute \Backend ;
7
+
8
+ use Magento \Store \Model \Store ;
9
+
10
+ class AttributeValidation
11
+ {
12
+ /**
13
+ * @var \Magento\Store\Model\StoreManagerInterface
14
+ */
15
+ private $ storeManager ;
16
+
17
+ /**
18
+ * @var array
19
+ */
20
+ private $ allowedEntityTypes ;
21
+
22
+ /**
23
+ * @param \Magento\Store\Model\StoreManagerInterface $storeManager
24
+ * @param array $allowedEntityTypes
25
+ */
26
+ public function __construct (
27
+ \Magento \Store \Model \StoreManagerInterface $ storeManager ,
28
+ $ allowedEntityTypes = []
29
+ ) {
30
+ $ this ->storeManager = $ storeManager ;
31
+ $ this ->allowedEntityTypes = $ allowedEntityTypes ;
32
+ }
33
+
34
+ /**
35
+ * @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject
36
+ * @param \Closure $proceed
37
+ * @param \Magento\Framework\DataObject $entity
38
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
39
+ * @return bool
40
+ */
41
+ public function aroundValidate (
42
+ \Magento \Eav \Model \Entity \Attribute \Backend \AbstractBackend $ subject ,
43
+ \Closure $ proceed ,
44
+ \Magento \Framework \DataObject $ entity
45
+ ) {
46
+ $ isAllowedType = !empty (array_filter (array_map (function ($ allowedEntity ) use ($ entity ) {
47
+ return $ entity instanceof $ allowedEntity ;
48
+ }, $ this ->allowedEntityTypes )));
49
+
50
+ if ($ isAllowedType && (int ) $ this ->storeManager ->getStore ()->getId () !== Store::DEFAULT_STORE_ID ) {
51
+ $ attrCode = $ subject ->getAttribute ()->getAttributeCode ();
52
+ // Null is meaning "no value" which should be overridden by value from default scope
53
+ if (array_key_exists ($ attrCode , $ entity ->getData ()) && $ entity ->getData ($ attrCode ) === null ) {
54
+ return true ;
55
+ }
56
+ }
57
+
58
+ return $ proceed ($ entity );
59
+ }
60
+ }
0 commit comments