‪TYPO3CMS  9.5
ContextMenu.php
Go to the documentation of this file.
1 <?php
2 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 
25 {
31  protected ‪$itemProviders = [
32  ItemProviders\PageProvider::class,
33  ItemProviders\RecordProvider::class
34  ];
35 
42  public function ‪getItems(string $table, string $identifier, string $context = ''): array
43  {
44  $items = [];
45  $itemsProviders = $this->‪getAvailableProviders($table, $identifier, $context);
46 
48  foreach ($itemsProviders as $provider) {
49  $items = $provider->addItems($items);
50  }
51  return $this->‪cleanItems($items);
52  }
53 
60  protected function ‪getAvailableProviders(string $table, string $identifier, string $context): array
61  {
62  $providers = array_merge($this->itemProviders, ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['ContextMenu']['ItemProviders'] ?? []);
63  $availableProviders = [];
64  foreach ($providers as $providerClass) {
65  $provider = GeneralUtility::makeInstance($providerClass, $table, $identifier, $context);
66  if ($provider->canHandle()) {
67  $priority = $provider->getPriority();
68  $availableProviders[$priority] = $provider;
69  }
70  }
71  krsort($availableProviders);
72  return $availableProviders;
73  }
74 
82  protected function ‪cleanItems(array $items): array
83  {
84  $canRender = false;
85  $prevItemWasDivider = false;
86  foreach ($items as $key => $item) {
87  if ($item['type'] === 'item') {
88  $canRender = true;
89  $prevItemWasDivider = false;
90  continue;
91  }
92  if ($item['type'] === 'divider') {
93  if ($prevItemWasDivider === true) {
94  unset($items[$key]);
95  } else {
96  $prevItemWasDivider = true;
97  }
98  continue;
99  }
100  if ($item['type'] === 'submenu') {
101  $childItems = $this->‪cleanItems($item['childItems']);
102  if (empty($childItems)) {
103  unset($items[$key]);
104  } else {
105  $items[$key]['childItems'] = $childItems;
106  $canRender = true;
107  $prevItemWasDivider = false;
108  }
109  continue;
110  }
111  }
112  //Remove first and last divider
113  $fistItem = reset($items);
114  if ($fistItem['type'] === 'divider') {
115  $key = key($items);
116  unset($items[$key]);
117  }
118  $lastItem = end($items);
119  if ($lastItem['type'] === 'divider') {
120  $key = key($items);
121  unset($items[$key]);
122  }
123  //no menu when there are no item or submenu
124  if (!$canRender) {
125  $items = [];
126  }
127  return $items;
128  }
129 }
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\cleanItems
‪array cleanItems(array $items)
Definition: ContextMenu.php:81
‪TYPO3\CMS\Backend\ContextMenu
Definition: ContextMenu.php:3
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu
Definition: ContextMenu.php:25
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\$itemProviders
‪array $itemProviders
Definition: ContextMenu.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\getItems
‪array getItems(string $table, string $identifier, string $context='')
Definition: ContextMenu.php:41
‪TYPO3\CMS\Backend\ContextMenu\ContextMenu\getAvailableProviders
‪array getAvailableProviders(string $table, string $identifier, string $context)
Definition: ContextMenu.php:59