Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UiComponent.php
Go to the documentation of this file.
1 <?php
7 
13 use Magento\Framework\Config\DataInterfaceFactory;
14 
18 class UiComponent implements ReaderInterface
19 {
23  const TYPE_UI_COMPONENT = 'uiComponent';
24 
30  protected $attributes = ['group', 'component', 'aclResource'];
31 
35  protected $layoutHelper;
36 
40  private $conditionReader;
41 
45  private $uiConfigFactory;
46 
50  private $readerPool;
51 
60  public function __construct(
62  Condition $conditionReader,
63  DataInterfaceFactory $uiConfigFactory,
64  ReaderPool $readerPool
65  ) {
66  $this->layoutHelper = $helper;
67  $this->conditionReader = $conditionReader;
68  $this->uiConfigFactory = $uiConfigFactory;
69  $this->readerPool = $readerPool;
70  }
71 
75  public function getSupportedNodes()
76  {
77  return [self::TYPE_UI_COMPONENT];
78  }
79 
83  public function interpret(Context $readerContext, Element $currentElement)
84  {
85  $attributes = $this->getAttributes($currentElement);
86  $scheduledStructure = $readerContext->getScheduledStructure();
87  $referenceName = $this->layoutHelper->scheduleStructure(
88  $scheduledStructure,
89  $currentElement,
90  $currentElement->getParent(),
91  ['attributes' => $attributes]
92  );
93  $attributes = array_merge(
95  ['visibilityConditions' => $this->conditionReader->parseConditions($currentElement)]
96  );
97  $scheduledStructure->setStructureElementData($referenceName, ['attributes' => $attributes]);
98 
99  $elements = [];
100  $config = $this->uiConfigFactory->create(['componentName' => $referenceName])->get($referenceName);
101  $this->getLayoutElementsFromUiConfiguration([$referenceName => $config], $elements);
102  foreach ($elements as $layoutElement) {
103  $layoutElement = simplexml_load_string(
104  $layoutElement,
105  Element::class
106  );
107  $this->readerPool->interpret($readerContext, $layoutElement);
108  }
109 
110  return $this;
111  }
112 
120  private function getLayoutElementsFromUiConfiguration(array $config, array &$elements = [])
121  {
122  foreach ($config as $data) {
123  if (isset($data['arguments']['block']['layout'])) {
124  $elements[] = $data['arguments']['block']['layout'];
125  }
126  if (isset($data['children']) && !empty($data['children'])) {
127  $this->getLayoutElementsFromUiConfiguration($data['children'], $elements);
128  }
129  }
130  }
131 
138  protected function getAttributes(Element $element)
139  {
140  $attributes = [];
141  foreach ($this->attributes as $attributeName) {
142  $attributes[$attributeName] = (string)$element->getAttribute($attributeName);
143  }
144 
145  return $attributes;
146  }
147 }
$helper
Definition: iframe.phtml:13
$config
Definition: fraud_order.php:17
__construct(Helper $helper, Condition $conditionReader, DataInterfaceFactory $uiConfigFactory, ReaderPool $readerPool)
Definition: UiComponent.php:60
interpret(Context $readerContext, Element $currentElement)
Definition: UiComponent.php:83
$element
Definition: element.phtml:12