[BUGFIX] Correctly handle non-existing level in menu.directory VH
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid_styled_content / Classes / ViewHelpers / Menu / MenuViewHelperTrait.php
1 <?php
2 namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Menu;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
18 use TYPO3\CMS\Frontend\Page\PageRepository;
19
20 /**
21 * Trait for Menu-ViewHelpers that require support functions for
22 * working with menus that require page selection constraints.
23 */
24 trait MenuViewHelperTrait
25 {
26 /**
27 * Get the constraints for the page based on doktype and field "nav_hide"
28 *
29 * By default the following doktypes are always ignored:
30 * - 6: Backend User Section
31 * - > 200: Folder (254)
32 * Recycler (255)
33 *
34 * Optional are:
35 * - 199: Menu separator
36 * - nav_hide: Not in menu
37 *
38 * @param bool $includeNotInMenu Should pages which are hidden for menu's be included
39 * @param bool $includeMenuSeparator Should pages of type "Menu separator" be included
40 * @return string
41 */
42 protected function getPageConstraints($includeNotInMenu = false, $includeMenuSeparator = false)
43 {
44 $constraints = [];
45
46 $constraints[] = 'doktype NOT IN (' . PageRepository::DOKTYPE_BE_USER_SECTION . ',' . PageRepository::DOKTYPE_RECYCLER . ',' . PageRepository::DOKTYPE_SYSFOLDER . ')';
47
48 if (!$includeNotInMenu) {
49 $constraints[] = 'nav_hide = 0';
50 }
51
52 if (!$includeMenuSeparator) {
53 $constraints[] = 'doktype != ' . PageRepository::DOKTYPE_SPACER;
54 }
55
56 return 'AND ' . implode(' AND ', $constraints);
57 }
58
59 /**
60 * Get a filtered list of page UIDs according to initial list
61 * of UIDs and entryLevel parameter.
62 *
63 * @param array $pageUids
64 * @param int|NULL $entryLevel
65 * @return array
66 */
67 protected function getPageUids(array $pageUids, $entryLevel = 0)
68 {
69 $typoScriptFrontendController = $this->getTypoScriptFrontendController();
70
71 // Remove empty entries from array
72 $pageUids = array_filter($pageUids);
73
74 // If no pages have been defined, use the current page
75 if (!empty($pageUids)) {
76 return $pageUids;
77 }
78
79 if ($entryLevel === null) {
80 return [$typoScriptFrontendController->id];
81 }
82
83 if ($entryLevel < 0) {
84 $entryLevel = count($typoScriptFrontendController->tmpl->rootLine) - 1 + $entryLevel;
85 }
86 if (isset($typoScriptFrontendController->tmpl->rootLine[$entryLevel]['uid'])) {
87 return [$typoScriptFrontendController->tmpl->rootLine[$entryLevel]['uid']];
88 }
89 return [];
90 }
91
92 /**
93 * @param array $variables
94 * @return mixed
95 */
96 protected function renderChildrenWithVariables(array $variables)
97 {
98 foreach ($variables as $name => $value) {
99 $this->templateVariableContainer->add($name, $value);
100 }
101
102 $output = $this->renderChildren();
103
104 foreach ($variables as $name => $_) {
105 $this->templateVariableContainer->remove($name);
106 }
107
108 return $output;
109 }
110
111 /**
112 * @return TypoScriptFrontendController
113 */
114 protected function getTypoScriptFrontendController()
115 {
116 return $GLOBALS['TSFE'];
117 }
118 }