b7c5b2977588be903746aca754950c8f78cb4bbb
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid_styled_content / Classes / ViewHelpers / Menu / ListViewHelper.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 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
17
18 /**
19 * A view helper which returns a list of pages
20 *
21 * = Example =
22 *
23 * <code title="List of pages with uid = 1 and uid = 2">
24 * <ce:menu.list pageUids="{0: 1, 1: 2}" as="pages">
25 * <f:for each="{pages}" as="page">
26 * {page.title}
27 * </f:for>
28 * </ce:menu.list>
29 * </code>
30 *
31 * <output>
32 * Page with uid = 1
33 * Page with uid = 2
34 * </output>
35 */
36 class ListViewHelper extends AbstractViewHelper
37 {
38 use MenuViewHelperTrait;
39
40 /**
41 * Output escaping is disabled as child content contains HTML by default
42 *
43 * @var bool
44 */
45 protected $escapeOutput = false;
46
47 /**
48 * Initialize ViewHelper arguments
49 */
50 public function initializeArguments()
51 {
52 $this->registerArgument('as', 'string', 'Name of template variable which will contain selected pages', true);
53 $this->registerArgument('levelAs', 'string', 'Name of template variable which will contain current level', false, null);
54 $this->registerArgument('pageUids', 'array', 'Page UIDs of parent pages', false, array());
55 $this->registerArgument('entryLevel', 'integer', 'The entry level', false, null);
56 $this->registerArgument('maximumLevel', 'integer', 'Maximum level for rendering of nested menus', false, 10);
57 $this->registerArgument('includeNotInMenu', 'boolean', 'Include pages that are marked "hide in menu"?', false, false);
58 $this->registerArgument('includeMenuSeparator', 'boolean', 'Include pages of the type "Menu separator"?', false, false);
59 }
60
61 /**
62 * Render the view helper
63 *
64 * @return string
65 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
66 */
67 public function render()
68 {
69 $typoScriptFrontendController = $this->getTypoScriptFrontendController();
70 $as = $this->arguments['as'];
71 $pageUids = (array)$this->arguments['pageUids'];
72 $entryLevel = $this->arguments['entryLevel'];
73 $levelAs = $this->arguments['levelAs'];
74 $maximumLevel = $this->arguments['maximumLevel'];
75 $includeNotInMenu = (bool)$this->arguments['includeNotInMenu'];
76 $includeMenuSeparator = (bool)$this->arguments['includeMenuSeparator'];
77
78 $pageUids = $this->getPageUids($pageUids, $entryLevel);
79 $pages = $typoScriptFrontendController->sys_page->getMenuForPages(
80 $pageUids,
81 '*',
82 '',
83 $this->getPageConstraints($includeNotInMenu, $includeMenuSeparator)
84 );
85
86 $output = '';
87
88 if (!empty($pages)) {
89 if (!$typoScriptFrontendController->register['ceMenuLevel_list']) {
90 $typoScriptFrontendController->register['ceMenuLevel_list'] = 1;
91 $typoScriptFrontendController->register['ceMenuMaximumLevel_list'] = $maximumLevel;
92 } else {
93 $typoScriptFrontendController->register['ceMenuLevel_list']++;
94 }
95
96 if ($typoScriptFrontendController->register['ceMenuLevel_list'] > $typoScriptFrontendController->register['ceMenuMaximumLevel_list']) {
97 return '';
98 }
99
100 $variables = array(
101 $as => $pages
102 );
103 if (!empty($levelAs)) {
104 $variables[$levelAs] = $typoScriptFrontendController->register['ceMenuLevel_list'];
105 }
106 $output = $this->renderChildrenWithVariables($variables);
107
108 $typoScriptFrontendController->register['ceMenuLevel_list']--;
109
110 if ($typoScriptFrontendController->register['ceMenuLevel_list'] === 0) {
111 unset($typoScriptFrontendController->register['ceMenuLevel_list']);
112 unset($typoScriptFrontendController->register['ceMenuMaximumLevel_list']);
113 }
114 }
115
116 return $output;
117 }
118 }