‪TYPO3CMS  11.5
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 
24 
30 {
36  protected ‪$itemProviders = [
37  PageProvider::class,
38  RecordProvider::class,
39  ];
40 
47  public function ‪getItems(string $table, string $identifier, string $context = ''): array
48  {
49  $items = [];
50  $itemsProviders = $this->‪getAvailableProviders($table, $identifier, $context);
51 
53  foreach ($itemsProviders as $provider) {
54  $items = $provider->addItems($items);
55  }
56  return $this->‪cleanItems($items);
57  }
58 
65  protected function ‪getAvailableProviders(string $table, string $identifier, string $context): array
66  {
67  $providers = array_merge($this->itemProviders, ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['ContextMenu']['ItemProviders'] ?? []);
68  $availableProviders = [];
69  foreach ($providers as $providerClass) {
70  $provider = GeneralUtility::makeInstance($providerClass, $table, $identifier, $context);
71  if ($provider->canHandle()) {
72  $priority = $provider->getPriority();
73  $availableProviders[$priority] = $provider;
74  }
75  }
76  krsort($availableProviders);
77  return $availableProviders;
78  }
79 
87  protected function ‪cleanItems(array $items): array
88  {
89  $canRender = false;
90  $prevItemWasDivider = false;
91 
92  foreach ($items as $key => $item) {
93  if ($item['type'] === 'item') {
94  $canRender = true;
95  $prevItemWasDivider = false;
96  continue;
97  }
98  if ($item['type'] === 'divider') {
99  if ($prevItemWasDivider === true) {
100  unset($items[$key]);
101  } else {
102  $prevItemWasDivider = true;
103  }
104  continue;
105  }
106  if ($item['type'] === 'submenu') {
107  $childItems = $this->‪cleanItems($item['childItems']);
108  if (empty($childItems)) {
109  unset($items[$key]);
110  } else {
111  $items[$key]['childItems'] = $childItems;
112  $canRender = true;
113  $prevItemWasDivider = false;
114  }
115  continue;
116  }
117  }
118 
119  if ($canRender) {
120  //Remove first and last divider
121  $fistItem = reset($items);
122  if ($fistItem['type'] === 'divider') {
123  $key = key($items);
124  unset($items[$key]);
125  }
126  $lastItem = end($items);
127  if ($lastItem['type'] === 'divider') {
128  $key = key($items);
129  unset($items[$key]);
130  }
131  } else {
132  //no menu when there are no item or submenu
133  $items = [];
134  }
135  return $items;
136  }
137 }
‪TYPO3\CMS\Backend\ContextMenu\ItemProviders\PageProvider
Definition: PageProvider.php:31
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\cleanItems
‪array cleanItems(array $items)
Definition: ContextMenu.php:86
‪TYPO3\CMS\Backend\ContextMenu\ItemProviders\ProviderInterface
Definition: ProviderInterface.php:24
‪TYPO3\CMS\Backend\ContextMenu
Definition: ContextMenu.php:18
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu
Definition: ContextMenu.php:30
‪TYPO3\CMS\Backend\ContextMenu\ItemProviders\RecordProvider
Definition: RecordProvider.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\$itemProviders
‪array $itemProviders
Definition: ContextMenu.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\getItems
‪array getItems(string $table, string $identifier, string $context='')
Definition: ContextMenu.php:46
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\getAvailableProviders
‪array getAvailableProviders(string $table, string $identifier, string $context)
Definition: ContextMenu.php:64