Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReadSnapshotPlugin.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Eav\Model\Config as EavConfig;
13 
20 {
24  private $metadataPool;
25 
29  private $config;
30 
35  public function __construct(
36  MetadataPool $metadataPool,
37  EavConfig $config
38  ) {
39  $this->metadataPool = $metadataPool;
40  $this->config = $config;
41  }
42 
50  public function afterExecute(ReadSnapshot $subject, array $entityData, $entityType)
51  {
52  if (!in_array($entityType, [ProductInterface::class, CategoryInterface::class], true)) {
53  return $entityData;
54  }
55 
56  $metadata = $this->metadataPool->getMetadata($entityType);
57  $connection = $metadata->getEntityConnection();
58  $globalAttributes = [];
59  $attributesMap = [];
60  $eavEntityType = $metadata->getEavEntityType();
61  $attributes = null === $eavEntityType
62  ? []
63  : $this->config->getEntityAttributes($eavEntityType, new \Magento\Framework\DataObject($entityData));
64 
66  foreach ($attributes as $attribute) {
67  if (!$attribute->isStatic() && $attribute->isScopeGlobal()) {
68  $globalAttributes[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId();
69  $attributesMap[$attribute->getAttributeId()] = $attribute->getAttributeCode();
70  }
71  }
72 
73  if ($globalAttributes) {
74  $selects = [];
75  foreach ($globalAttributes as $table => $attributeIds) {
76  $select = $connection->select()
77  ->from(
78  ['t' => $table],
79  ['value' => 't.value', 'attribute_id' => 't.attribute_id']
80  )
81  ->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()])
82  ->where('attribute_id' . ' in (?)', $attributeIds)
83  ->where('store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
84  $selects[] = $select;
85  }
86  $unionSelect = new \Magento\Framework\DB\Sql\UnionExpression(
87  $selects,
88  \Magento\Framework\DB\Select::SQL_UNION_ALL
89  );
90  foreach ($connection->fetchAll($unionSelect) as $attributeValue) {
91  $entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value'];
92  }
93  }
94 
95  return $entityData;
96  }
97 }
$config
Definition: fraud_order.php:17
$attributes
Definition: matrix.phtml:13
__construct(MetadataPool $metadataPool, EavConfig $config)
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14