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