‪TYPO3CMS  ‪main
ContextMenu.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 
28 {
30 
32  {
33  $this->itemProvidersRegistry = ‪$itemProvidersRegistry;
34  }
35 
36  public function ‪getItems(string $table, string ‪$identifier, string $context = ''): array
37  {
38  $items = [];
39  foreach ($this->‪getAvailableProviders($table, ‪$identifier, $context) as $provider) {
40  $items = $provider->addItems($items);
41  }
42  return $this->‪cleanItems($items);
43  }
44 
48  protected function ‪getAvailableProviders(string $table, string ‪$identifier, string $context): array
49  {
50  $providers = $this->itemProvidersRegistry->getItemProviders();
51  $availableProviders = [];
52  foreach ($providers as $provider) {
53  $provider->setContext($table, ‪$identifier, $context);
54  if ($provider->canHandle()) {
55  $priority = $provider->getPriority();
56  $availableProviders[$priority] = $provider;
57  }
58  }
59  krsort($availableProviders);
60  return $availableProviders;
61  }
62 
67  protected function ‪cleanItems(array $items): array
68  {
69  $canRender = false;
70  $prevItemWasDivider = false;
71 
72  foreach ($items as $key => $item) {
73  if ($item['type'] === 'item') {
74  $canRender = true;
75  $prevItemWasDivider = false;
76  continue;
77  }
78  if ($item['type'] === 'divider') {
79  if ($prevItemWasDivider === true) {
80  unset($items[$key]);
81  } else {
82  $prevItemWasDivider = true;
83  }
84  continue;
85  }
86  if ($item['type'] === 'submenu') {
87  $childItems = $this->‪cleanItems($item['childItems']);
88  if (empty($childItems)) {
89  unset($items[$key]);
90  } else {
91  $items[$key]['childItems'] = $childItems;
92  $canRender = true;
93  $prevItemWasDivider = false;
94  }
95  continue;
96  }
97  }
98 
99  if ($canRender) {
100  //Remove first and last divider
101  $fistItem = reset($items);
102  if ($fistItem['type'] === 'divider') {
103  $key = key($items);
104  unset($items[$key]);
105  }
106  $lastItem = end($items);
107  if ($lastItem['type'] === 'divider') {
108  $key = key($items);
109  unset($items[$key]);
110  }
111  } else {
112  //no menu when there are no item or submenu
113  $items = [];
114  }
115  return $items;
116  }
117 }
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\__construct
‪__construct(ItemProvidersRegistry $itemProvidersRegistry)
Definition: ContextMenu.php:31
‪TYPO3\CMS\Backend\ContextMenu\ItemProviders\ItemProvidersRegistry
Definition: ItemProvidersRegistry.php:26
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\$itemProvidersRegistry
‪ItemProvidersRegistry $itemProvidersRegistry
Definition: ContextMenu.php:29
‪TYPO3\CMS\Backend\ContextMenu\ItemProviders\ProviderInterface
Definition: ProviderInterface.php:24
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\getItems
‪getItems(string $table, string $identifier, string $context='')
Definition: ContextMenu.php:36
‪TYPO3\CMS\Backend\ContextMenu
Definition: ContextMenu.php:18
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu
Definition: ContextMenu.php:28
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\getAvailableProviders
‪ProviderInterface[] getAvailableProviders(string $table, string $identifier, string $context)
Definition: ContextMenu.php:48
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\cleanItems
‪cleanItems(array $items)
Definition: ContextMenu.php:67
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37