Skip to content

Commit 11ea5fe

Browse files
ACPT-1544: Fix EAV Config ::attributes to work across websites
1 parent 682f51e commit 11ea5fe

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

app/code/Magento/Catalog/Model/Config.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ class Config extends \Magento\Eav\Model\Config
8989
*/
9090
protected $_eavConfig;
9191

92-
/**
93-
* @var \Magento\Store\Model\StoreManagerInterface
94-
*/
95-
protected $_storeManager;
96-
9792
/**
9893
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory
9994
*/
@@ -166,7 +161,8 @@ public function __construct(
166161
$universalFactory,
167162
$serializer,
168163
$scopeConfig,
169-
$attributesForPreload
164+
$attributesForPreload,
165+
$storeManager,
170166
);
171167
}
172168

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

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Model\AbstractModel;
1616
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
1717
use Magento\Framework\Serialize\SerializerInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
1819

1920
/**
2021
* EAV config model.
@@ -71,11 +72,8 @@ class Config implements ResetAfterRequestInterface
7172
/**
7273
* Initialized attributes
7374
*
74-
* array ($entityTypeCode =>
75-
* ($attributeCode => $object)
76-
* )
77-
*
78-
* @var AbstractAttribute[][]
75+
* [int $website][string $entityTypeCode][string $code] = AbstractAttribute $attribute
76+
* @var array<int, array<string, array<string, AbstractAttribute>>>
7977
*/
8078
private $attributes;
8179

@@ -123,6 +121,11 @@ class Config implements ResetAfterRequestInterface
123121
*/
124122
protected $_universalFactory;
125123

124+
/**
125+
* @var StoreManagerInterface
126+
*/
127+
protected $_storeManager;
128+
126129
/**
127130
* @var AbstractAttribute[]
128131
*/
@@ -168,6 +171,7 @@ class Config implements ResetAfterRequestInterface
168171
* @param SerializerInterface|null $serializer
169172
* @param ScopeConfigInterface|null $scopeConfig
170173
* @param array $attributesForPreload
174+
* @param StoreManagerInterface|null $storeManager
171175
* @codeCoverageIgnore
172176
*/
173177
public function __construct(
@@ -178,7 +182,8 @@ public function __construct(
178182
\Magento\Framework\Validator\UniversalFactory $universalFactory,
179183
SerializerInterface $serializer = null,
180184
ScopeConfigInterface $scopeConfig = null,
181-
$attributesForPreload = []
185+
$attributesForPreload = [],
186+
?StoreManagerInterface $storeManager = null,
182187
) {
183188
$this->_cache = $cache;
184189
$this->_entityTypeFactory = $entityTypeFactory;
@@ -188,6 +193,7 @@ public function __construct(
188193
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
189194
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
190195
$this->attributesForPreload = $attributesForPreload;
196+
$this->_storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
191197
}
192198

193199
/**
@@ -243,7 +249,7 @@ protected function _load($id)
243249
*/
244250
private function loadAttributes($entityTypeCode)
245251
{
246-
return $this->attributes[$entityTypeCode] ?? [];
252+
return $this->attributes[$this->getWebsiteId()][$entityTypeCode] ?? [];
247253
}
248254

249255
/**
@@ -269,7 +275,7 @@ protected function _save($obj, $id)
269275
*/
270276
private function saveAttribute(AbstractAttribute $attribute, $entityTypeCode, $attributeCode)
271277
{
272-
$this->attributes[$entityTypeCode][$attributeCode] = $attribute;
278+
$this->attributes[$this->getWebsiteId()][$entityTypeCode][$attributeCode] = $attribute;
273279
}
274280

275281
/**
@@ -476,7 +482,7 @@ protected function _initAttributes($entityType)
476482

477483
$entityTypeCode = $entityType->getEntityTypeCode();
478484
$attributes = $this->_universalFactory->create($entityType->getEntityAttributeCollection());
479-
$websiteId = $attributes instanceof Collection ? $this->getWebsiteId($attributes) : 0;
485+
$websiteId = $attributes instanceof Collection ? $this->getWebsiteIdFromAttributeCollection($attributes) : 0;
480486
$cacheKey = self::ATTRIBUTES_CACHE_ID . '-' . $entityTypeCode . '-' . $websiteId ;
481487

482488
if ($this->isCacheEnabled() && $this->initAttributesFromCache($entityType, $cacheKey)) {
@@ -537,6 +543,7 @@ public function getAttributes($entityType)
537543
*/
538544
public function getAttribute($entityType, $code)
539545
{
546+
$websiteId = $this->getWebsiteId();
540547
if ($code instanceof \Magento\Eav\Model\Entity\Attribute\AttributeInterface) {
541548
return $code;
542549
}
@@ -548,19 +555,19 @@ public function getAttribute($entityType, $code)
548555
$code = $this->_getAttributeReference($code, $entityTypeCode) ?: $code;
549556
}
550557

551-
if (isset($this->attributes[$entityTypeCode][$code])) {
558+
if (isset($this->attributes[$websiteId][$entityTypeCode][$code])) {
552559
\Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
553-
return $this->attributes[$entityTypeCode][$code];
560+
return $this->attributes[$websiteId][$entityTypeCode][$code];
554561
}
555562

556563
if (array_key_exists($entityTypeCode, $this->attributesForPreload)
557564
&& array_key_exists($code, $this->attributesForPreload[$entityTypeCode])
558565
) {
559566
$this->initSystemAttributes($entityType, $this->attributesForPreload[$entityTypeCode]);
560567
}
561-
if (isset($this->attributes[$entityTypeCode][$code])) {
568+
if (isset($this->attributes[$websiteId][$entityTypeCode][$code])) {
562569
\Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
563-
return $this->attributes[$entityTypeCode][$code];
570+
return $this->attributes[$websiteId][$entityTypeCode][$code];
564571
}
565572

566573
if ($this->scopeConfig->getValue(self::XML_PATH_CACHE_USER_DEFINED_ATTRIBUTES)) {
@@ -590,7 +597,8 @@ private function initSystemAttributes($entityType, $systemAttributes)
590597
return;
591598
}
592599
$attributeCollection = $this->_universalFactory->create($entityType->getEntityAttributeCollection());
593-
$websiteId = $attributeCollection instanceof Collection ? $this->getWebsiteId($attributeCollection) : 0;
600+
$websiteId = $attributeCollection instanceof Collection
601+
? $this->getWebsiteIdFromAttributeCollection($attributeCollection) : 0;
594602
$cacheKey = self::ATTRIBUTES_CACHE_ID . '-' . $entityTypeCode . '-' . $websiteId . '-preload';
595603
if ($this->isCacheEnabled() && ($attributes = $this->_cache->load($cacheKey))) {
596604
$attributes = $this->serializer->unserialize($attributes);
@@ -628,7 +636,7 @@ private function initSystemAttributes($entityType, $systemAttributes)
628636
$cacheKey,
629637
[
630638
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
631-
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG
639+
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG,
632640
]
633641
);
634642
}
@@ -973,14 +981,25 @@ private function initAttributesFromCache(Type $entityType, string $cacheKey)
973981
}
974982

975983
/**
976-
* Returns website id.
984+
* Returns website id from attribute collection.
977985
*
978986
* @param Collection $attributeCollection
979987
* @return int
980988
*/
981-
private function getWebsiteId(Collection $attributeCollection): int
989+
private function getWebsiteIdFromAttributeCollection(Collection $attributeCollection): int
990+
{
991+
return (int)$attributeCollection->getWebsite()?->getId();
992+
}
993+
994+
/**
995+
* Return current website scope instance
996+
*
997+
* @return int website id
998+
*/
999+
public function getWebsiteId() : int
9821000
{
983-
return $attributeCollection->getWebsite() ? (int)$attributeCollection->getWebsite()->getId() : 0;
1001+
$websiteId = $this->_storeManager->getStore()?->getWebsiteId();
1002+
return (int)$websiteId;
9841003
}
9851004

9861005
/**

0 commit comments

Comments
 (0)