Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeLoader.php
Go to the documentation of this file.
1 <?php
7 
13 
18 {
24  private $defaultAttributes = [];
25 
29  protected $objectManager;
30 
34  private $config;
35 
42  public function __construct(
43  Config $config,
45  ) {
46  $this->config = $config;
47  $this->objectManager = $objectManager;
48  }
49 
58  public function loadAllAttributes(AbstractEntity $resource, DataObject $object = null)
59  {
60  $attributes = $this->config->getEntityAttributes($resource->getEntityType(), $object);
61  $attributeCodes = array_keys($attributes);
65  $defaultAttributesCodes = array_diff($resource->getDefaultAttributes(), $attributeCodes);
66 
67  $resource->unsetAttributes();
68 
69  foreach ($defaultAttributesCodes as $attributeCode) {
70  $resource->addAttribute($this->_getDefaultAttribute($resource, $attributeCode), $object);
71  }
72  foreach ($attributes as $attributeCode => $attribute) {
73  $resource->addAttribute($attribute, $object);
74  }
75  return $resource;
76  }
77 
86  {
87  $entityTypeId = $resource->getEntityType()->getId();
88  if (!isset($this->defaultAttributes[$entityTypeId][$attributeCode])) {
89  $attribute = $this->objectManager->create($resource->getEntityType()->getAttributeModel())
90  ->setAttributeCode($attributeCode)
91  ->setBackendType(AbstractAttribute::TYPE_STATIC)
92  ->setIsGlobal(1)
93  ->setEntityType($resource->getEntityType())
94  ->setEntityTypeId($resource->getEntityType()->getId());
95  $this->defaultAttributes[$entityTypeId][$attributeCode] = $attribute;
96  }
97  return $this->defaultAttributes[$entityTypeId][$attributeCode];
98  }
99 }
__construct(Config $config, ObjectManagerInterface $objectManager)
$config
Definition: fraud_order.php:17
loadAllAttributes(AbstractEntity $resource, DataObject $object=null)
$resource
Definition: bulk.php:12
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13
_getDefaultAttribute(AbstractEntity $resource, $attributeCode)