Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReadHandler.php
Go to the documentation of this file.
1 <?php
7 
13 use Psr\Log\LoggerInterface;
14 
16 {
20  protected $metadataPool;
21 
25  protected $scopeResolver;
26 
30  private $logger;
31 
35  private $config;
36 
45  public function __construct(
48  LoggerInterface $logger,
49  \Magento\Eav\Model\Config $config
50  ) {
51  $this->metadataPool = $metadataPool;
52  $this->scopeResolver = $scopeResolver;
53  $this->logger = $logger;
54  $this->config = $config;
55  }
56 
66  protected function getAttributes($entityType)
67  {
68  $metadata = $this->metadataPool->getMetadata($entityType);
69  $eavEntityType = $metadata->getEavEntityType();
70  return null === $eavEntityType ? [] : $this->config->getEntityAttributes($eavEntityType);
71  }
72 
81  private function getEntityAttributes(string $entityType, DataObject $entity): array
82  {
83  $metadata = $this->metadataPool->getMetadata($entityType);
84  $eavEntityType = $metadata->getEavEntityType();
85  return null === $eavEntityType ? [] : $this->config->getEntityAttributes($eavEntityType, $entity);
86  }
87 
92  protected function getContextVariables(ScopeInterface $scope)
93  {
94  $data[] = $scope->getValue();
95  if ($scope->getFallback()) {
96  $data = array_merge($data, $this->getContextVariables($scope->getFallback()));
97  }
98  return $data;
99  }
100 
111  public function execute($entityType, $entityData, $arguments = [])
112  {
113  $metadata = $this->metadataPool->getMetadata($entityType);
114  if (!$metadata->getEavEntityType()) {//todo hasCustomAttributes
115  return $entityData;
116  }
117  $context = $this->scopeResolver->getEntityContext($entityType, $entityData);
118  $connection = $metadata->getEntityConnection();
119 
120  $attributeTables = [];
121  $attributesMap = [];
122  $selects = [];
123 
125  foreach ($this->getEntityAttributes($entityType, new DataObject($entityData)) as $attribute) {
126  if (!$attribute->isStatic()) {
127  $attributeTables[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId();
128  $attributesMap[$attribute->getAttributeId()] = $attribute->getAttributeCode();
129  }
130  }
131  if (count($attributeTables)) {
132  $attributeTables = array_keys($attributeTables);
133  foreach ($attributeTables as $attributeTable) {
134  $select = $connection->select()
135  ->from(
136  ['t' => $attributeTable],
137  ['value' => 't.value', 'attribute_id' => 't.attribute_id']
138  )
139  ->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()]);
140  foreach ($context as $scope) {
141  //TODO: if (in table exists context field)
142  $select->where(
143  $metadata->getEntityConnection()->quoteIdentifier($scope->getIdentifier()) . ' IN (?)',
144  $this->getContextVariables($scope)
145  )->order('t.' . $scope->getIdentifier() . ' DESC');
146  }
147  $selects[] = $select;
148  }
149  $unionSelect = new \Magento\Framework\DB\Sql\UnionExpression(
150  $selects,
151  \Magento\Framework\DB\Select::SQL_UNION_ALL
152  );
153  foreach ($connection->fetchAll($unionSelect) as $attributeValue) {
154  if (isset($attributesMap[$attributeValue['attribute_id']])) {
155  $entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value'];
156  } else {
157  $this->logger->warning(
158  "Attempt to load value of nonexistent EAV attribute '{$attributeValue['attribute_id']}'
159  for entity type '$entityType'."
160  );
161  }
162  }
163  }
164  return $entityData;
165  }
166 }
$config
Definition: fraud_order.php:17
$logger
execute($entityType, $entityData, $arguments=[])
const SQL_UNION_ALL
Definition: Select.php:69
__construct(MetadataPool $metadataPool, ScopeResolver $scopeResolver, LoggerInterface $logger, \Magento\Eav\Model\Config $config)
Definition: ReadHandler.php:45
$entity
Definition: element.phtml:22
getContextVariables(ScopeInterface $scope)
Definition: ReadHandler.php:92
$arguments
$connection
Definition: bulk.php:13