TYPO3 CMS  TYPO3_8-7
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 = $this->itemProviders;
63  if (is_array($GLOBALS['TYPO3_CONF_VARS']['BE']['ContextMenu']['ItemProviders'])) {
64  $providers = array_merge($this->itemProviders, $GLOBALS['TYPO3_CONF_VARS']['BE']['ContextMenu']['ItemProviders']);
65  }
66 
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 }
getAvailableProviders(string $table, string $identifier, string $context)
Definition: ContextMenu.php:60
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']