Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Generic.php
Go to the documentation of this file.
1 <?php
7 
13 
17 class Generic implements LayoutInterface
18 {
19  const CONFIG_JS_COMPONENT = 'component';
20  const CONFIG_COMPONENT_NAME = 'componentName';
21  const CONFIG_PANEL_COMPONENT = 'panelComponentName';
22 
26  protected $component;
27 
31  protected $namespace;
32 
37 
41  protected $data;
42 
48  {
49  $this->uiComponentFactory = $uiComponentFactory;
50  $this->data = $data;
51  }
52 
60  {
61  $this->component = $component;
62  $this->namespace = $component->getContext()->getNamespace();
63 
64  $this->component->getContext()->addComponentDefinition(
65  $this->getConfig(self::CONFIG_COMPONENT_NAME),
66  [
67  'component' => $this->getConfig(self::CONFIG_JS_COMPONENT),
68  'extends' => $this->namespace
69  ]
70  );
71 
72  $children = [];
73  $context = $component->getContext();
74  $this->addChildren($children, $component, $component->getName());
75  $dataSources = $component->getContext()->getDataSourceData($component);
76  $configuration = [
77  'types' => $context->getComponentsDefinitions(),
78  'components' => [
79  $context->getNamespace() => [
80  'children' => array_merge($children, $dataSources)
81  ]
82  ]
83  ];
84  return $configuration;
85  }
86 
95  protected function addChildren(
96  array &$topNode,
98  $componentType
99  ) {
100  $childrenNode = [];
101  $childComponents = $component->getChildComponents();
102  if (!empty($childComponents)) {
104  foreach ($childComponents as $child) {
105  if ($child instanceof DataSourceInterface) {
106  continue;
107  }
108  if ($child->getData('wrapper')) {
109  $this->addWrappedBlock($child, $childrenNode);
110  continue;
111  }
112  self::addChildren($childrenNode, $child, $child->getComponentName());
113  }
114  }
115 
116  $config = $component->getConfiguration();
117  if (is_string($config)) {
118  $topNode[$config] = $config;
119  } else {
120  $nodeData = [
121  'type' => $componentType,
122  'name' => $component->getName(),
123  ];
124  if (!empty($childrenNode)) {
125  $nodeData['children'] = $childrenNode;
126  }
127  if (isset($config['dataScope'])) {
128  $nodeData['dataScope'] = $config['dataScope'];
129  unset($config['dataScope']);
130  }
131  if (!empty($config)) {
132  $nodeData['config'] = $config;
133  }
134  $topNode[$component->getName()] = $nodeData;
135  }
136  }
137 
145  protected function addWrappedBlock(BlockWrapperInterface $childComponent, array &$childrenNode)
146  {
147  if (!($childComponent->getData('wrapper/canShow') && $childComponent->getData('wrapper/componentType'))) {
148  return $this;
149  }
150 
151  $name = $childComponent->getName();
152  $panelComponent = $this->createChildFormComponent($childComponent, $name);
153  $childrenNode[$name] = [
154  'type' => $panelComponent->getComponentName(),
155  'dataScope' => $name,
156  'config' => $childComponent->getConfiguration(),
157  'children' => [
158  $name => [
159  'type' => $childComponent->getComponentName(),
160  'dataScope' => $name,
161  'config' => [
162  'content' => $childComponent->render()
163  ],
164  ]
165  ],
166  ];
167 
168  return $this;
169  }
170 
179  protected function createChildFormComponent(UiComponentInterface $childComponent, $name)
180  {
181  $panelComponent = $this->uiComponentFactory->create(
182  $name,
183  $childComponent->getData('wrapper/componentType'),
184  [
185  'context' => $this->component->getContext(),
186  'components' => [$childComponent->getName() => $childComponent]
187  ]
188  );
189  $panelComponent->prepare();
190  $this->component->addComponent($name, $panelComponent);
191 
192  return $panelComponent;
193  }
194 
201  protected function getConfig($name)
202  {
203  return $this->data['config'][$name] ?? null;
204  }
205 }
$configuration
Definition: index.php:33
$config
Definition: fraud_order.php:17
createChildFormComponent(UiComponentInterface $childComponent, $name)
Definition: Generic.php:179
build(UiComponentInterface $component)
Definition: Generic.php:59
addWrappedBlock(BlockWrapperInterface $childComponent, array &$childrenNode)
Definition: Generic.php:145
$children
Definition: actions.phtml:11
__construct(UiComponentFactory $uiComponentFactory, $data=[])
Definition: Generic.php:47
if(!isset($_GET['name'])) $name
Definition: log.php:14