8a3aedfa56a7208f0ab267b0b80b4366be37ff5e
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid_styled_content / Classes / ViewHelpers / Menu / SectionViewHelper.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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19
20 /**
21 * A view helper which returns content elements with 'Show in Section Menus' enabled
22 *
23 * By default only content in colPos=0 will be found. This can be overruled by using "column"
24 *
25 * If you set property "type" to 'all', then the 'Show in Section Menus' checkbox is not considered
26 * and all content elements are selected.
27 *
28 * If the property "type" is 'header' then only content elements with a visible header layout
29 * (and a non-empty 'header' field!) are selected.
30 * In other words, if the header layout of an element is set to 'Hidden' then the element will not be in the results.
31 *
32 * = Example =
33 *
34 * <code title="Content elements in page with uid = 1 and 'Show in Section Menu's' enabled">
35 * <ce:menu.section pageUid="1" as="contentElements">
36 * <f:for each="{contentElements}" as="contentElement">
37 * {contentElement.header}
38 * </f:for>
39 * </ce:menu.section>
40 * </code>
41 *
42 * <output>
43 * Content element 1 in page with uid = 1 and "Show in section menu's" enabled
44 * Content element 2 in page with uid = 1 and "Show in section menu's" enabled
45 * Content element 3 in page with uid = 1 and "Show in section menu's" enabled
46 * </output>
47 */
48 class SectionViewHelper extends AbstractViewHelper
49 {
50 use MenuViewHelperTrait;
51
52 /**
53 * Output escaping is disabled as child content contains HTML by default
54 *
55 * @var bool
56 */
57 protected $escapeOutput = false;
58
59 /**
60 * Initialize ViewHelper arguments
61 *
62 * @return void
63 */
64 public function initializeArguments()
65 {
66 $this->registerArgument('as', 'string', 'Name of the template variable that will contain selected pages', true);
67 $this->registerArgument('column', 'string', 'Column numbers (colPos) from which to select content', false, '0');
68 $this->registerArgument('pageUid', 'integer', 'UID of page containing section-objects; defaults to current page', false, null);
69 $this->registerArgument('type', 'string', 'Search method when selecting indices from page', false, '');
70 }
71
72 /**
73 * Render the view helper
74 *
75 * @return string
76 */
77 public function render()
78 {
79 $as = (string)$this->arguments['as'];
80 $pageUid = (int)$this->arguments['pageUid'];
81 $type = (string)$this->arguments['type'];
82
83 if (empty($pageUid)) {
84 $pageUid = $this->getTypoScriptFrontendController()->id;
85 }
86
87 if (!empty($type) && !in_array($type, array('all', 'header'), true)) {
88 return '';
89 }
90
91 return $this->renderChildrenWithVariables(array(
92 $as => $this->findBySection($pageUid, $type, $this->arguments['column'])
93 ));
94 }
95
96 /**
97 * Find content with 'Show in Section Menus' enabled in a page
98 *
99 * By default only content in colPos=0 will be found. This can be overruled by using $column
100 *
101 * If you set property type to "all", then the 'Show in Section Menus' checkbox is not considered
102 * and all content elements are selected.
103 *
104 * If the property $type is 'header' then only content elements with a visible header layout
105 * (and a non-empty 'header' field!) is selected.
106 * In other words, if the header layout of an element is set to 'Hidden' then the page will not appear in the menu.
107 *
108 * @param int $pageUid The page uid
109 * @param string $type Search method
110 * @param string $column Restrict content by the column number
111 * @return array
112 */
113 protected function findBySection($pageUid, $type = '', $column = '')
114 {
115 $constraints = [];
116 if (trim($column) !== '') {
117 $colPosList = implode(',', GeneralUtility::intExplode(',', $column, true));
118 $constraints[] = 'colPos IN(' . ($colPosList !== '' ? $colPosList : '0') . ')';
119 }
120
121 switch ($type) {
122 case 'all':
123 break;
124 case 'header':
125 $constraints[] = 'sectionIndex = 1';
126 $constraints[] = 'header <> \'\'';
127 $constraints[] = 'header_layout <> 100';
128 break;
129 default:
130 $constraints[] = 'sectionIndex = 1';
131 }
132
133 $whereStatement = implode(' AND ', $constraints);
134
135 $contentElements = $this->getTypoScriptFrontendController()->cObj->getRecords('tt_content', [
136 'where' => $whereStatement,
137 'orderBy' => 'sorting',
138 'languageField = sys_language_uid',
139 'pidInList' => (int)$pageUid
140 ]);
141
142 return $contentElements;
143 }
144 }