Skip to content

Commit 74deee4

Browse files
committed
Move getCustomAttributeCodes from product model to resource model
1 parent 08ab509 commit 74deee4

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
117117
*/
118118
protected $_urlModel = null;
119119

120+
/**
121+
* @var ResourceModel\Product
122+
*/
123+
protected $_resource;
124+
120125
/**
121126
* @var string
122127
*/
@@ -269,6 +274,7 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
269274
protected $imageCacheFactory;
270275

271276
/**
277+
* @deprecated not used anymore, related functionality has been moved to resource model
272278
* @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
273279
*/
274280
protected $metadataService;
@@ -468,11 +474,7 @@ protected function _construct()
468474
*/
469475
protected function getCustomAttributesCodes()
470476
{
471-
if ($this->customAttributesCodes === null) {
472-
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
473-
$this->customAttributesCodes = array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES);
474-
}
475-
return $this->customAttributesCodes;
477+
return $this->_resource->getCustomAttributesCodes();
476478
}
477479

478480
/**

app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,5 @@ public function getAttributeRawValue($entityId, $attribute, $store)
577577

578578
return $attributesData === false ? false : $attributesData;
579579
}
580+
580581
}

app/code/Magento/Catalog/Model/ResourceModel/Product.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
810
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
911
use Magento\Framework\App\ObjectManager;
1012

@@ -83,6 +85,16 @@ class Product extends AbstractResource
8385
*/
8486
private $productCategoryLink;
8587

88+
/**
89+
* @var ProductAttributeRepositoryInterface
90+
*/
91+
protected $metadataService;
92+
93+
/**
94+
* @var string[]
95+
*/
96+
private $customAttributesCodes;
97+
8698
/**
8799
* @param \Magento\Eav\Model\Entity\Context $context
88100
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -94,6 +106,7 @@ class Product extends AbstractResource
94106
* @param \Magento\Eav\Model\Entity\TypeFactory $typeFactory
95107
* @param \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes
96108
* @param array $data
109+
* @param ProductAttributeRepositoryInterface|null $metadataService
97110
*
98111
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
99112
*/
@@ -107,14 +120,18 @@ public function __construct(
107120
\Magento\Eav\Model\Entity\Attribute\SetFactory $setFactory,
108121
\Magento\Eav\Model\Entity\TypeFactory $typeFactory,
109122
\Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes,
110-
$data = []
123+
$data = [],
124+
ProductAttributeRepositoryInterface $metadataService = null
111125
) {
112126
$this->_categoryCollectionFactory = $categoryCollectionFactory;
113127
$this->_catalogCategory = $catalogCategory;
114128
$this->eventManager = $eventManager;
115129
$this->setFactory = $setFactory;
116130
$this->typeFactory = $typeFactory;
117131
$this->defaultAttributes = $defaultAttributes;
132+
$this->metadataService = $metadataService ?? ObjectManager::getInstance(
133+
ProductAttributeRepositoryInterface::class
134+
);
118135
parent::__construct(
119136
$context,
120137
$storeManager,
@@ -232,6 +249,14 @@ public function getCategoryIds($product)
232249
return array_column($result, 'category_id');
233250
}
234251

252+
public function getCustomAttributesCodes()
253+
{
254+
if ($this->customAttributesCodes === null) {
255+
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
256+
$this->customAttributesCodes = array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES);
257+
}
258+
}
259+
235260
/**
236261
* Get product identifier by sku
237262
*

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
1111
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
1212
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
13+
use Magento\Framework\Api\MetadataServiceInterface;
1314
use Magento\Framework\App\Config\Element;
1415
use Magento\Framework\DB\Adapter\DuplicateException;
1516
use Magento\Framework\Exception\AlreadyExistsException;
@@ -1958,4 +1959,25 @@ protected function loadAttributesForObject($attributes, $object = null)
19581959
}
19591960
}
19601961
}
1962+
1963+
/**
1964+
* Receive a list of EAV attributes using provided metadata service.
1965+
*
1966+
* @param MetadataServiceInterface $metadataService
1967+
* @return string[]
1968+
*/
1969+
protected function getEavAttributesCodes(MetadataServiceInterface $metadataService)
1970+
{
1971+
$attributeCodes = [];
1972+
$customAttributesMetadata = $metadataService->getCustomAttributesMetadata(
1973+
$this->getEntityType()->getEntityModel()
1974+
);
1975+
if (is_array($customAttributesMetadata)) {
1976+
/** @var $attribute \Magento\Framework\Api\MetadataObjectInterface */
1977+
foreach ($customAttributesMetadata as $attribute) {
1978+
$attributeCodes[] = $attribute->getAttributeCode();
1979+
}
1980+
}
1981+
return $attributeCodes;
1982+
}
19611983
}

0 commit comments

Comments
 (0)