Generic.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Layout;
  7. use Magento\Framework\View\Element\UiComponent\DataSourceInterface;
  8. use Magento\Framework\View\Element\UiComponent\BlockWrapperInterface;
  9. use Magento\Framework\View\Element\UiComponent\LayoutInterface;
  10. use Magento\Framework\View\Element\UiComponentInterface;
  11. use Magento\Framework\View\Element\UiComponentFactory;
  12. /**
  13. * Class Generic
  14. */
  15. class Generic implements LayoutInterface
  16. {
  17. const CONFIG_JS_COMPONENT = 'component';
  18. const CONFIG_COMPONENT_NAME = 'componentName';
  19. const CONFIG_PANEL_COMPONENT = 'panelComponentName';
  20. /**
  21. * @var UiComponentInterface
  22. */
  23. protected $component;
  24. /**
  25. * @var string
  26. */
  27. protected $namespace;
  28. /**
  29. * @var UiComponentFactory
  30. */
  31. protected $uiComponentFactory;
  32. /**
  33. * @var array
  34. */
  35. protected $data;
  36. /**
  37. * @param UiComponentFactory $uiComponentFactory
  38. * @param array $data
  39. */
  40. public function __construct(UiComponentFactory $uiComponentFactory, $data = [])
  41. {
  42. $this->uiComponentFactory = $uiComponentFactory;
  43. $this->data = $data;
  44. }
  45. /**
  46. * Generate Java Script configuration element
  47. *
  48. * @param UiComponentInterface $component
  49. * @return array
  50. */
  51. public function build(UiComponentInterface $component)
  52. {
  53. $this->component = $component;
  54. $this->namespace = $component->getContext()->getNamespace();
  55. $this->component->getContext()->addComponentDefinition(
  56. $this->getConfig(self::CONFIG_COMPONENT_NAME),
  57. [
  58. 'component' => $this->getConfig(self::CONFIG_JS_COMPONENT),
  59. 'extends' => $this->namespace
  60. ]
  61. );
  62. $children = [];
  63. $context = $component->getContext();
  64. $this->addChildren($children, $component, $component->getName());
  65. $dataSources = $component->getContext()->getDataSourceData($component);
  66. $configuration = [
  67. 'types' => $context->getComponentsDefinitions(),
  68. 'components' => [
  69. $context->getNamespace() => [
  70. 'children' => array_merge($children, $dataSources)
  71. ]
  72. ]
  73. ];
  74. return $configuration;
  75. }
  76. /**
  77. * Add children data
  78. *
  79. * @param array $topNode
  80. * @param UiComponentInterface $component
  81. * @param string $componentType
  82. * @return void
  83. */
  84. protected function addChildren(
  85. array &$topNode,
  86. UiComponentInterface $component,
  87. $componentType
  88. ) {
  89. $childrenNode = [];
  90. $childComponents = $component->getChildComponents();
  91. if (!empty($childComponents)) {
  92. /** @var UiComponentInterface $child */
  93. foreach ($childComponents as $child) {
  94. if ($child instanceof DataSourceInterface) {
  95. continue;
  96. }
  97. if ($child->getData('wrapper')) {
  98. $this->addWrappedBlock($child, $childrenNode);
  99. continue;
  100. }
  101. self::addChildren($childrenNode, $child, $child->getComponentName());
  102. }
  103. }
  104. $config = $component->getConfiguration();
  105. if (is_string($config)) {
  106. $topNode[$config] = $config;
  107. } else {
  108. $nodeData = [
  109. 'type' => $componentType,
  110. 'name' => $component->getName(),
  111. ];
  112. if (!empty($childrenNode)) {
  113. $nodeData['children'] = $childrenNode;
  114. }
  115. if (isset($config['dataScope'])) {
  116. $nodeData['dataScope'] = $config['dataScope'];
  117. unset($config['dataScope']);
  118. }
  119. if (!empty($config)) {
  120. $nodeData['config'] = $config;
  121. }
  122. $topNode[$component->getName()] = $nodeData;
  123. }
  124. }
  125. /**
  126. * Add wrapped layout block
  127. *
  128. * @param BlockWrapperInterface $childComponent
  129. * @param array $childrenNode
  130. * @return $this
  131. */
  132. protected function addWrappedBlock(BlockWrapperInterface $childComponent, array &$childrenNode)
  133. {
  134. if (!($childComponent->getData('wrapper/canShow') && $childComponent->getData('wrapper/componentType'))) {
  135. return $this;
  136. }
  137. $name = $childComponent->getName();
  138. $panelComponent = $this->createChildFormComponent($childComponent, $name);
  139. $childrenNode[$name] = [
  140. 'type' => $panelComponent->getComponentName(),
  141. 'dataScope' => $name,
  142. 'config' => $childComponent->getConfiguration(),
  143. 'children' => [
  144. $name => [
  145. 'type' => $childComponent->getComponentName(),
  146. 'dataScope' => $name,
  147. 'config' => [
  148. 'content' => $childComponent->render()
  149. ],
  150. ]
  151. ],
  152. ];
  153. return $this;
  154. }
  155. /**
  156. * Create child of form
  157. *
  158. * @param UiComponentInterface $childComponent
  159. * @param string $name
  160. * @return UiComponentInterface
  161. * @throws \Magento\Framework\Exception\LocalizedException
  162. */
  163. protected function createChildFormComponent(UiComponentInterface $childComponent, $name)
  164. {
  165. $panelComponent = $this->uiComponentFactory->create(
  166. $name,
  167. $childComponent->getData('wrapper/componentType'),
  168. [
  169. 'context' => $this->component->getContext(),
  170. 'components' => [$childComponent->getName() => $childComponent]
  171. ]
  172. );
  173. $panelComponent->prepare();
  174. $this->component->addComponent($name, $panelComponent);
  175. return $panelComponent;
  176. }
  177. /**
  178. * Get config by name
  179. *
  180. * @param string $name
  181. * @return mixed
  182. */
  183. protected function getConfig($name)
  184. {
  185. return $this->data['config'][$name] ?? null;
  186. }
  187. }