Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tabs.php
Go to the documentation of this file.
1 <?php
7 
15 
19 class Tabs extends \Magento\Framework\View\Layout\Generic implements LayoutInterface
20 {
24  protected $navContainerName;
25 
29  protected $structure = [];
30 
34  protected $sortIncrement = 10;
35 
44  {
45  $this->navContainerName = $navContainerName;
46  parent::__construct($uiComponentFactory, $data);
47  }
48 
56  {
57  $this->component = $component;
58  $this->namespace = $component->getContext()->getNamespace();
59 
60  $this->addNavigationBlock();
61 
62  // Initialization of structure components
63  $this->initSections();
64  $this->initAreas();
65 
66  return parent::build($component);
67  }
68 
79  protected function addChildren(array &$topNode, UiComponentInterface $component, $componentType)
80  {
81  $childrenAreas = [];
82  $collectedComponents = [];
83 
84  foreach ($component->getChildComponents() as $childComponent) {
85  if ($childComponent instanceof DataSourceInterface) {
86  continue;
87  }
88  if ($childComponent instanceof BlockWrapperInterface) {
89  $this->addWrappedBlock($childComponent, $childrenAreas);
90  continue;
91  }
92 
93  $name = $childComponent->getName();
94  $config = $childComponent->getData('config');
95  $collectedComponents[$name] = true;
96  if (isset($config['is_collection']) && $config['is_collection'] === true) {
97  $label = $childComponent->getData('config/label');
98  $this->component->getContext()->addComponentDefinition(
99  'collection',
100  [
101  'component' => 'Magento_Ui/js/form/components/collection',
102  'extends' => $this->namespace
103  ]
104  );
105 
110  list($childComponent, $structure) = $this->prepareChildComponents($childComponent, $name);
111 
112  $childrenStructure = $structure[$name]['children'];
113 
114  $structure[$name]['children'] = [
115  $name . '_collection' => [
116  'type' => 'collection',
117  'config' => [
118  'active' => 1,
119  'removeLabel' => __('Remove %1', $label),
120  'addLabel' => __('Add New %1', $label),
121  'removeMessage' => $childComponent->getData('config/removeMessage'),
122  'itemTemplate' => 'item_template',
123  ],
124  'children' => [
125  'item_template' => ['type' => $this->namespace,
126  'isTemplate' => true,
127  'component' => 'Magento_Ui/js/form/components/collection/item',
128  'childType' => 'group',
129  'config' => [
130  'label' => __('New %1', $label),
131  ],
132  'children' => $childrenStructure
133  ]
134  ]
135  ]
136  ];
137  } else {
142  list($childComponent, $structure) = $this->prepareChildComponents($childComponent, $name);
143  }
144 
145  $tabComponent = $this->createTabComponent($childComponent, $name);
146 
147  if (isset($structure[$name]['dataScope']) && $structure[$name]['dataScope']) {
148  $dataScope = $structure[$name]['dataScope'];
149  unset($structure[$name]['dataScope']);
150  } else {
151  $dataScope = 'data.' . $name;
152  }
153 
154  $childrenAreas[$name] = [
155  'type' => $tabComponent->getComponentName(),
156  'dataScope' => $dataScope,
157  'config' => $config,
158  'insertTo' => [
159  $this->namespace . '.sections' => [
160  'position' => $this->getNextSortIncrement()
161  ]
162  ],
163  'children' => $structure,
164  ];
165  }
166 
167  $this->structure[static::AREAS_KEY]['children'] = $childrenAreas;
168  $topNode = $this->structure;
169  }
170 
178  protected function addWrappedBlock(BlockWrapperInterface $childComponent, array &$areas)
179  {
180  $name = $childComponent->getName();
182  $block = $childComponent->getBlock();
183  if (!$block->canShowTab()) {
184  return;
185  }
186  if (!$block instanceof TabInterface) {
187  parent::addWrappedBlock($childComponent, $areas);
188  }
189  $block->setData('target_form', $this->namespace);
190 
191  $config = [];
192  if ($block->isAjaxLoaded()) {
193  $config['url'] = $block->getTabUrl();
194  } else {
195  $config['content'] = $childComponent->getData('config/content') ?: $block->toHtml();
196  }
197 
198  $tabComponent = $this->createTabComponent($childComponent, $name);
199  $areas[$name] = [
200  'type' => $tabComponent->getComponentName(),
201  'dataScope' => $name,
202  'insertTo' => [
203  $this->namespace . '.sections' => [
204  'position' => $block->hasSortOrder() ? $block->getSortOrder() : $this->getNextSortIncrement()
205  ]
206  ],
207  'config' => [
208  'label' => $block->getTabTitle()
209  ],
210  'children' => [
211  $name => [
212  'type' => 'html_content',
213  'dataScope' => $name,
214  'config' => $config,
215  ]
216  ],
217  ];
218  }
219 
228  protected function createTabComponent(UiComponentInterface $childComponent, $name)
229  {
230  $tabComponent = $this->uiComponentFactory->create(
231  $name,
232  'tab',
233  [
234  'context' => $this->component->getContext(),
235  'components' => [$childComponent->getName() => $childComponent]
236  ]
237  );
238  $tabComponent->prepare();
239  $this->component->addComponent($name, $tabComponent);
240 
241  return $tabComponent;
242  }
243 
251  protected function prepareChildComponents(UiComponentInterface $component, $parentName)
252  {
253  $name = $component->getName();
254  $childComponents = $component->getChildComponents();
255 
256  $childrenStructure = [];
257  foreach ($childComponents as $childName => $child) {
258  $isVisible = $child->getData('config/visible');
259  if ($isVisible !== null && $isVisible == 0) {
260  continue;
261  }
266  list($childComponent, $childStructure) = $this->prepareChildComponents($child, $component->getName());
267  $childrenStructure = array_merge($childrenStructure, $childStructure);
268  $component->addComponent($childName, $childComponent);
269  }
270 
271  $structure = [
272  $name => [
273  'type' => $component->getComponentName(),
274  'name' => $component->getName(),
275  'children' => $childrenStructure
276  ]
277  ];
278 
279  list($config, $dataScope) = $this->prepareConfig((array) $component->getConfiguration(), $name, $parentName);
280 
281  if ($dataScope !== false) {
282  $structure[$name]['dataScope'] = $dataScope;
283  }
284  $structure[$name]['config'] = $config;
285 
286  return [$component, $structure];
287  }
288 
297  protected function prepareConfig(array $config, $name, $parentName)
298  {
299  $dataScope = false;
300  if (!isset($config['displayArea'])) {
301  $config['displayArea'] = 'body';
302  }
303  if (isset($config['dataScope'])) {
304  $dataScope = $config['dataScope'];
305  unset($config['dataScope']);
306  } elseif ($name !== $parentName) {
307  $dataScope = $name;
308  }
309 
310  return [$config, $dataScope];
311  }
312 
318  protected function initSections()
319  {
320  $this->structure[static::SECTIONS_KEY] = [
321  'type' => 'nav',
322  'config' => [
323  'label' => $this->component->getData('label'),
324  ],
325  'children' => [],
326  ];
327  }
328 
334  protected function initAreas()
335  {
336  $this->structure[static::AREAS_KEY] = [
337  'type' => $this->namespace,
338  'config' => [
339  'namespace' => $this->namespace,
340  ],
341  'children' => [],
342  ];
343  }
344 
350  protected function addNavigationBlock()
351  {
352  $pageLayout = $this->component->getContext()->getPageLayout();
353 
354  $navName = 'tabs_nav';
355  if ($pageLayout->hasElement($navName)) {
356  $navName = $this->component->getName() . '_tabs_nav';
357  }
358 
360  if (isset($this->navContainerName)) {
361  $navBlock = $pageLayout->addBlock(
362  \Magento\Ui\Component\Layout\Tabs\Nav::class,
363  $navName,
364  $this->navContainerName
365  );
366  } else {
367  $navBlock = $pageLayout->addBlock(\Magento\Ui\Component\Layout\Tabs\Nav::class, $navName, 'content');
368  }
369  $navBlock->setTemplate('Magento_Ui::layout/tabs/nav/default.phtml');
370  $navBlock->setData('data_scope', $this->namespace);
371 
372  $this->component->getContext()->addComponentDefinition(
373  'nav',
374  [
375  'component' => 'Magento_Ui/js/form/components/tab_group',
376  'config' => [
377  'template' => 'ui/tab'
378  ],
379  'extends' => $this->namespace
380  ]
381  );
382  }
383 
389  protected function getNextSortIncrement()
390  {
391  $this->sortIncrement += 10;
392  return $this->sortIncrement;
393  }
394 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
createTabComponent(UiComponentInterface $childComponent, $name)
Definition: Tabs.php:228
$config
Definition: fraud_order.php:17
build(UiComponentInterface $component)
Definition: Tabs.php:55
prepareConfig(array $config, $name, $parentName)
Definition: Tabs.php:297
__construct(UiComponentFactory $uiComponentFactory, $navContainerName=null, $data=[])
Definition: Tabs.php:43
__()
Definition: __.php:13
$block
Definition: block.php:8
$label
Definition: details.phtml:21
addWrappedBlock(BlockWrapperInterface $childComponent, array &$childrenNode)
Definition: Generic.php:145
if(!isset($_GET['name'])) $name
Definition: log.php:14