Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Catalog\Model;
8 
10 
17 {
18  const XML_PATH_LIST_DEFAULT_SORT_BY = 'catalog/frontend/default_sort_by';
19 
24 
29 
34 
39 
43  protected $_productTypesById;
44 
51 
58 
64  protected $_usedForSortBy;
65 
69  protected $_storeId = null;
70 
76  protected $_scopeConfig;
77 
83  protected $_eavConfig;
84 
90  protected $_storeManager;
91 
98 
105 
112 
118  protected $_configFactory;
119 
139  public function __construct(
140  \Magento\Framework\App\CacheInterface $cache,
141  \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory,
142  \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory,
143  \Magento\Framework\App\Cache\StateInterface $cacheState,
144  \Magento\Framework\Validator\UniversalFactory $universalFactory,
145  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
146  \Magento\Catalog\Model\ResourceModel\ConfigFactory $configFactory,
147  \Magento\Catalog\Model\Product\TypeFactory $productTypeFactory,
148  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
149  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory,
151  \Magento\Eav\Model\Config $eavConfig,
152  SerializerInterface $serializer = null
153  ) {
154  $this->_scopeConfig = $scopeConfig;
155  $this->_configFactory = $configFactory;
156  $this->_productTypeFactory = $productTypeFactory;
157  $this->_groupCollectionFactory = $groupCollectionFactory;
158  $this->_setCollectionFactory = $setCollectionFactory;
159  $this->_storeManager = $storeManager;
160  $this->_eavConfig = $eavConfig;
161 
162  parent::__construct(
163  $cache,
164  $entityTypeFactory,
166  $cacheState,
167  $universalFactory,
169  );
170  }
171 
177  protected function _construct()
178  {
179  $this->_init(\Magento\Catalog\Model\ResourceModel\Config::class);
180  }
181 
188  public function setStoreId($storeId)
189  {
190  $this->_storeId = $storeId;
191  return $this;
192  }
193 
199  public function getStoreId()
200  {
201  if ($this->_storeId === null) {
202  return $this->_storeManager->getStore()->getId();
203  }
204  return $this->_storeId;
205  }
206 
210  public function loadAttributeSets()
211  {
212  if ($this->_attributeSetsById) {
213  return $this;
214  }
215 
216  $attributeSetCollection = $this->_setCollectionFactory->create()->load();
217 
218  $this->_attributeSetsById = [];
219  $this->_attributeSetsByName = [];
220  foreach ($attributeSetCollection as $id => $attributeSet) {
221  $entityTypeId = $attributeSet->getEntityTypeId();
222  $name = $attributeSet->getAttributeSetName();
223  $this->_attributeSetsById[$entityTypeId][$id] = $name;
224  $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id;
225  }
226  return $this;
227  }
228 
235  {
236  if (!is_numeric($id)) {
237  return $id;
238  }
239  $this->loadAttributeSets();
240 
241  if (!is_numeric($entityTypeId)) {
242  $entityTypeId = $this->getEntityType($entityTypeId)->getId();
243  }
244  return isset(
245  $this->_attributeSetsById[$entityTypeId][$id]
246  ) ? $this->_attributeSetsById[$entityTypeId][$id] : false;
247  }
248 
254  public function getAttributeSetId($entityTypeId, $name = null)
255  {
256  if (is_numeric($name)) {
257  return $name;
258  }
259  $this->loadAttributeSets();
260 
261  if (!is_numeric($entityTypeId)) {
262  $entityTypeId = $this->getEntityType($entityTypeId)->getId();
263  }
264  $name = strtolower($name);
265  return isset(
266  $this->_attributeSetsByName[$entityTypeId][$name]
267  ) ? $this->_attributeSetsByName[$entityTypeId][$name] : false;
268  }
269 
273  public function loadAttributeGroups()
274  {
275  if ($this->_attributeGroupsById) {
276  return $this;
277  }
278 
279  $attributeSetCollection = $this->_groupCollectionFactory->create()->load();
280 
281  $this->_attributeGroupsById = [];
282  $this->_attributeGroupsByName = [];
284  $attributeSetId = $attributeGroup->getAttributeSetId();
285  $name = $attributeGroup->getAttributeGroupName();
286  $this->_attributeGroupsById[$attributeSetId][$id] = $name;
287  $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
288  }
289  return $this;
290  }
291 
298  {
299  if (!is_numeric($id)) {
300  return $id;
301  }
302 
303  $this->loadAttributeGroups();
304 
305  if (!is_numeric($attributeSetId)) {
307  }
308  return isset(
309  $this->_attributeGroupsById[$attributeSetId][$id]
310  ) ? $this->_attributeGroupsById[$attributeSetId][$id] : false;
311  }
312 
319  {
320  if (is_numeric($name)) {
321  return $name;
322  }
323 
324  $this->loadAttributeGroups();
325 
326  if (!is_numeric($attributeSetId)) {
328  }
329  $name = strtolower($name);
330  return isset(
331  $this->_attributeGroupsByName[$attributeSetId][$name]
332  ) ? $this->_attributeGroupsByName[$attributeSetId][$name] : false;
333  }
334 
338  public function loadProductTypes()
339  {
340  if ($this->_productTypesById) {
341  return $this;
342  }
343 
344  $productTypeCollection = $this->_productTypeFactory->create()->getOptionArray();
345 
346  $this->_productTypesById = [];
347  $this->_productTypesByName = [];
348  foreach ($productTypeCollection as $id => $type) {
349  $name = $type;
350  $this->_productTypesById[$id] = $name;
351  $this->_productTypesByName[strtolower($name)] = $id;
352  }
353  return $this;
354  }
355 
360  public function getProductTypeId($name)
361  {
362  if (is_numeric($name)) {
363  return $name;
364  }
365 
366  $this->loadProductTypes();
367 
368  $name = strtolower($name);
369  return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
370  }
371 
376  public function getProductTypeName($id)
377  {
378  if (!is_numeric($id)) {
379  return $id;
380  }
381 
382  $this->loadProductTypes();
383 
384  return $this->_productTypesById[$id] ?? false;
385  }
386 
392  public function getSourceOptionId($source, $value)
393  {
394  foreach ($source->getAllOptions() as $option) {
395  if (strcasecmp($option['label'], $value) == 0 || $option['value'] == $value) {
396  return $option['value'];
397  }
398  }
399  return null;
400  }
401 
407  public function getProductAttributes()
408  {
409  if ($this->_productAttributes === null) {
410  $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing());
411  }
413  }
414 
420  protected function _getResource()
421  {
422  return $this->_configFactory->create();
423  }
424 
431  {
432  if ($this->_usedInProductListing === null) {
433  $this->_usedInProductListing = [];
435  $attributesData = $this->_getResource()->setStoreId($this->getStoreId())->getAttributesUsedInListing();
436  $this->_eavConfig->importAttributesData($entityType, $attributesData);
437  foreach ($attributesData as $attributeData) {
438  $attributeCode = $attributeData['attribute_code'];
439  $this->_usedInProductListing[$attributeCode] = $this->_eavConfig->getAttribute(
440  $entityType,
442  );
443  }
444  }
446  }
447 
453  public function getAttributesUsedForSortBy()
454  {
455  if ($this->_usedForSortBy === null) {
456  $this->_usedForSortBy = [];
458  $attributesData = $this->_getResource()->getAttributesUsedForSortBy();
459  $this->_eavConfig->importAttributesData($entityType, $attributesData);
460  foreach ($attributesData as $attributeData) {
461  $attributeCode = $attributeData['attribute_code'];
462  $this->_usedForSortBy[$attributeCode] = $this->_eavConfig->getAttribute($entityType, $attributeCode);
463  }
464  }
465  return $this->_usedForSortBy;
466  }
467 
475  {
476  $options = ['position' => __('Position')];
477  foreach ($this->getAttributesUsedForSortBy() as $attribute) {
478  /* @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
479  $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
480  }
481 
482  return $options;
483  }
484 
491  public function getProductListDefaultSortBy($store = null)
492  {
493  return $this->_scopeConfig->getValue(
494  self::XML_PATH_LIST_DEFAULT_SORT_BY,
495  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
496  $store
497  );
498  }
499 }
getAttributeSetName($entityTypeId, $id)
Definition: Config.php:234
$id
Definition: fieldset.phtml:14
$source
Definition: source.php:23
getAttributeGroupId($attributeSetId, $name)
Definition: Config.php:318
$storeManager
__()
Definition: __.php:13
$type
Definition: item.phtml:13
getAttributeGroupName($attributeSetId, $id)
Definition: Config.php:297
$attributesData
$value
Definition: gender.phtml:16
getAttributeSetId($entityTypeId, $name=null)
Definition: Config.php:254
$attributeCode
Definition: extend.phtml:12
$configFactory
Definition: config_data.php:43
getProductListDefaultSortBy($store=null)
Definition: Config.php:491
getSourceOptionId($source, $value)
Definition: Config.php:392
if(!isset($_GET['name'])) $name
Definition: log.php:14
__construct(\Magento\Framework\App\CacheInterface $cache, \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory, \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory, \Magento\Framework\App\Cache\StateInterface $cacheState, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\ResourceModel\ConfigFactory $configFactory, \Magento\Catalog\Model\Product\TypeFactory $productTypeFactory, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Eav\Model\Config $eavConfig, SerializerInterface $serializer=null)
Definition: Config.php:139