Tabs.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Layout;
  7. use Magento\Framework\View\Element\UiComponent\BlockWrapperInterface;
  8. use Magento\Framework\View\Element\UiComponent\DataSourceInterface;
  9. use Magento\Framework\View\Element\UiComponentFactory;
  10. use Magento\Framework\View\Element\UiComponentInterface;
  11. use Magento\Framework\View\Element\ComponentVisibilityInterface;
  12. use Magento\Ui\Component\Layout\Tabs\TabInterface;
  13. /**
  14. * Class Tabs
  15. */
  16. class Tabs extends \Magento\Framework\View\Layout\Generic
  17. {
  18. /**
  19. * @var string
  20. */
  21. protected $navContainerName;
  22. /**
  23. * @var array
  24. */
  25. protected $structure = [];
  26. /**
  27. * @var int
  28. */
  29. protected $sortIncrement = 10;
  30. /**
  31. * Constructor
  32. *
  33. * @param UiComponentFactory $uiComponentFactory
  34. * @param null|string $navContainerName
  35. * @param array $data
  36. */
  37. public function __construct(UiComponentFactory $uiComponentFactory, $navContainerName = null, $data = [])
  38. {
  39. $this->navContainerName = $navContainerName;
  40. parent::__construct($uiComponentFactory, $data);
  41. }
  42. /**
  43. * Build
  44. *
  45. * @param UiComponentInterface $component
  46. * @return array
  47. */
  48. public function build(UiComponentInterface $component)
  49. {
  50. $this->component = $component;
  51. $this->namespace = $component->getContext()->getNamespace();
  52. $this->addNavigationBlock();
  53. // Initialization of structure components
  54. $this->initSections();
  55. $this->initAreas();
  56. return parent::build($component);
  57. }
  58. /**
  59. * Add children data
  60. *
  61. * @param array $topNode
  62. * @param UiComponentInterface $component
  63. * @param string $componentType
  64. * @return void
  65. * @throws \Magento\Framework\Exception\LocalizedException
  66. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  67. */
  68. protected function addChildren(array &$topNode, UiComponentInterface $component, $componentType)
  69. {
  70. $childrenAreas = [];
  71. $collectedComponents = [];
  72. foreach ($component->getChildComponents() as $childComponent) {
  73. if ($childComponent instanceof DataSourceInterface) {
  74. continue;
  75. }
  76. if ($childComponent instanceof BlockWrapperInterface) {
  77. $this->addWrappedBlock($childComponent, $childrenAreas);
  78. continue;
  79. }
  80. if ($childComponent instanceof ComponentVisibilityInterface && !$childComponent->isComponentVisible()) {
  81. continue;
  82. }
  83. $name = $childComponent->getName();
  84. $config = $childComponent->getData('config');
  85. $collectedComponents[$name] = true;
  86. [$childComponent, $structure] = $this->buildChildComponentStructure($config, $childComponent);
  87. $tabComponent = $this->createTabComponent($childComponent, $name);
  88. if (isset($structure[$name]['dataScope']) && $structure[$name]['dataScope']) {
  89. $dataScope = $structure[$name]['dataScope'];
  90. unset($structure[$name]['dataScope']);
  91. } else {
  92. $dataScope = 'data.' . $name;
  93. }
  94. $childrenAreas[$name] = [
  95. 'type' => $tabComponent->getComponentName(),
  96. 'dataScope' => $dataScope,
  97. 'config' => $config,
  98. 'insertTo' => [
  99. $this->namespace . '.sections' => [
  100. 'position' => $this->getNextSortIncrement()
  101. ]
  102. ],
  103. 'children' => $structure,
  104. ];
  105. }
  106. $this->structure[static::AREAS_KEY]['children'] = $childrenAreas;
  107. $topNode = $this->structure;
  108. }
  109. /**
  110. * Build child components structure of the tab
  111. *
  112. * @param array $config
  113. * @param UiComponentInterface $childComponent
  114. * @return array
  115. */
  116. private function buildChildComponentStructure(array $config, $childComponent): array
  117. {
  118. $name = $childComponent->getName();
  119. if (isset($config['is_collection']) && $config['is_collection'] === true) {
  120. $label = $childComponent->getData('config/label');
  121. $this->component->getContext()->addComponentDefinition(
  122. 'collection',
  123. [
  124. 'component' => 'Magento_Ui/js/form/components/collection',
  125. 'extends' => $this->namespace
  126. ]
  127. );
  128. /**
  129. * @var UiComponentInterface $childComponent
  130. * @var array $structure
  131. */
  132. [$childComponent, $structure] = $this->prepareChildComponents($childComponent, $name);
  133. $childrenStructure = $structure[$name]['children'];
  134. $structure[$name]['children'] = [
  135. $name . '_collection' => [
  136. 'type' => 'collection',
  137. 'config' => [
  138. 'active' => 1,
  139. 'removeLabel' => __('Remove %1', $label),
  140. 'addLabel' => __('Add New %1', $label),
  141. 'removeMessage' => $childComponent->getData('config/removeMessage'),
  142. 'itemTemplate' => 'item_template',
  143. ],
  144. 'children' => [
  145. 'item_template' => ['type' => $this->namespace,
  146. 'isTemplate' => true,
  147. 'component' => 'Magento_Ui/js/form/components/collection/item',
  148. 'childType' => 'group',
  149. 'config' => [
  150. 'label' => __('New %1', $label),
  151. ],
  152. 'children' => $childrenStructure
  153. ]
  154. ]
  155. ]
  156. ];
  157. } else {
  158. /**
  159. * @var UiComponentInterface $childComponent
  160. * @var array $structure
  161. */
  162. [$childComponent, $structure] = $this->prepareChildComponents($childComponent, $name);
  163. }
  164. return [$childComponent, $structure];
  165. }
  166. /**
  167. * Add wrapped layout block
  168. *
  169. * @param BlockWrapperInterface $childComponent
  170. * @param array $areas
  171. * @return void
  172. */
  173. protected function addWrappedBlock(BlockWrapperInterface $childComponent, array &$areas)
  174. {
  175. $name = $childComponent->getName();
  176. /** @var TabInterface $block */
  177. $block = $childComponent->getBlock();
  178. if (!$block->canShowTab()) {
  179. return;
  180. }
  181. if (!$block instanceof TabInterface) {
  182. parent::addWrappedBlock($childComponent, $areas);
  183. }
  184. $block->setData('target_form', $this->namespace);
  185. $config = [];
  186. if ($block->isAjaxLoaded()) {
  187. $config['url'] = $block->getTabUrl();
  188. } else {
  189. $config['content'] = $childComponent->getData('config/content') ?: $block->toHtml();
  190. }
  191. $tabComponent = $this->createTabComponent($childComponent, $name);
  192. $areas[$name] = [
  193. 'type' => $tabComponent->getComponentName(),
  194. 'dataScope' => $name,
  195. 'insertTo' => [
  196. $this->namespace . '.sections' => [
  197. 'position' => $block->hasSortOrder() ? $block->getSortOrder() : $this->getNextSortIncrement()
  198. ]
  199. ],
  200. 'config' => [
  201. 'label' => $block->getTabTitle()
  202. ],
  203. 'children' => [
  204. $name => [
  205. 'type' => 'html_content',
  206. 'dataScope' => $name,
  207. 'config' => $config,
  208. ]
  209. ],
  210. ];
  211. }
  212. /**
  213. * Create tab component
  214. *
  215. * @param UiComponentInterface $childComponent
  216. * @param string $name
  217. * @return UiComponentInterface
  218. * @throws \Magento\Framework\Exception\LocalizedException
  219. */
  220. protected function createTabComponent(UiComponentInterface $childComponent, $name)
  221. {
  222. $tabComponent = $this->uiComponentFactory->create(
  223. $name,
  224. 'tab',
  225. [
  226. 'context' => $this->component->getContext(),
  227. 'components' => [$childComponent->getName() => $childComponent]
  228. ]
  229. );
  230. $tabComponent->prepare();
  231. $this->component->addComponent($name, $tabComponent);
  232. return $tabComponent;
  233. }
  234. /**
  235. * To prepare the structure of child components
  236. *
  237. * @param UiComponentInterface $component
  238. * @param string $parentName
  239. * @return array
  240. */
  241. protected function prepareChildComponents(UiComponentInterface $component, $parentName)
  242. {
  243. $name = $component->getName();
  244. $childComponents = $component->getChildComponents();
  245. $childrenStructure = [];
  246. foreach ($childComponents as $childName => $child) {
  247. $isVisible = $child->getData('config/visible');
  248. if ($isVisible !== null && $isVisible == 0) {
  249. continue;
  250. }
  251. /**
  252. * @var UiComponentInterface $childComponent
  253. * @var array $childStructure
  254. */
  255. list($childComponent, $childStructure) = $this->prepareChildComponents($child, $component->getName());
  256. $childrenStructure = array_merge($childrenStructure, $childStructure);
  257. $component->addComponent($childName, $childComponent);
  258. }
  259. $structure = [
  260. $name => [
  261. 'type' => $component->getComponentName(),
  262. 'name' => $component->getName(),
  263. 'children' => $childrenStructure
  264. ]
  265. ];
  266. list($config, $dataScope) = $this->prepareConfig((array) $component->getConfiguration(), $name, $parentName);
  267. if ($dataScope !== false) {
  268. $structure[$name]['dataScope'] = $dataScope;
  269. }
  270. $structure[$name]['config'] = $config;
  271. return [$component, $structure];
  272. }
  273. /**
  274. * Prepare config
  275. *
  276. * @param array $config
  277. * @param string $name
  278. * @param string $parentName
  279. * @return array
  280. */
  281. protected function prepareConfig(array $config, $name, $parentName)
  282. {
  283. $dataScope = false;
  284. if (!isset($config['displayArea'])) {
  285. $config['displayArea'] = 'body';
  286. }
  287. if (isset($config['dataScope'])) {
  288. $dataScope = $config['dataScope'];
  289. unset($config['dataScope']);
  290. } elseif ($name !== $parentName) {
  291. $dataScope = $name;
  292. }
  293. return [$config, $dataScope];
  294. }
  295. /**
  296. * Prepare initial structure for sections
  297. *
  298. * @return void
  299. */
  300. protected function initSections()
  301. {
  302. $this->structure[static::SECTIONS_KEY] = [
  303. 'type' => 'nav',
  304. 'config' => [
  305. 'label' => $this->component->getData('label'),
  306. ],
  307. 'children' => [],
  308. ];
  309. }
  310. /**
  311. * Prepare initial structure for areas
  312. *
  313. * @return void
  314. */
  315. protected function initAreas()
  316. {
  317. $this->structure[static::AREAS_KEY] = [
  318. 'type' => $this->namespace,
  319. 'config' => [
  320. 'namespace' => $this->namespace,
  321. ],
  322. 'children' => [],
  323. ];
  324. }
  325. /**
  326. * Add navigation block
  327. *
  328. * @return void
  329. */
  330. protected function addNavigationBlock()
  331. {
  332. $pageLayout = $this->component->getContext()->getPageLayout();
  333. $navName = 'tabs_nav';
  334. if ($pageLayout->hasElement($navName)) {
  335. $navName = $this->component->getName() . '_tabs_nav';
  336. }
  337. /** @var \Magento\Ui\Component\Layout\Tabs\Nav $navBlock */
  338. if (isset($this->navContainerName)) {
  339. $navBlock = $pageLayout->addBlock(
  340. \Magento\Ui\Component\Layout\Tabs\Nav::class,
  341. $navName,
  342. $this->navContainerName
  343. );
  344. } else {
  345. $navBlock = $pageLayout->addBlock(\Magento\Ui\Component\Layout\Tabs\Nav::class, $navName, 'content');
  346. }
  347. $navBlock->setTemplate('Magento_Ui::layout/tabs/nav/default.phtml');
  348. $navBlock->setData('data_scope', $this->namespace);
  349. $this->component->getContext()->addComponentDefinition(
  350. 'nav',
  351. [
  352. 'component' => 'Magento_Ui/js/form/components/tab_group',
  353. 'config' => [
  354. 'template' => 'ui/tab'
  355. ],
  356. 'extends' => $this->namespace
  357. ]
  358. );
  359. }
  360. /**
  361. * Get next sort increment
  362. *
  363. * @return int
  364. */
  365. protected function getNextSortIncrement()
  366. {
  367. $this->sortIncrement += 10;
  368. return $this->sortIncrement;
  369. }
  370. }