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