[BUGFIX] Render correct colPos in section menus again
[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 * Initialize ViewHelper arguments
54 *
55 * @return void
56 */
57 public function initializeArguments()
58 {
59 $this->registerArgument('as', 'string', 'Name of the template variable that will contain selected pages', true);
60 $this->registerArgument('column', 'string', 'Column numbers (colPos) from which to select content', false, '0');
61 $this->registerArgument('pageUid', 'integer', 'UID of page containing section-objects; defaults to current page', false, null);
62 $this->registerArgument('type', 'string', 'Search method when selecting indices from page', false, '');
63 }
64
65 /**
66 * Render the view helper
67 *
68 * @return string
69 */
70 public function render()
71 {
72 $as = (string)$this->arguments['as'];
73 $pageUid = (int)$this->arguments['pageUid'];
74 $type = (string)$this->arguments['type'];
75
76 if (empty($pageUid)) {
77 $pageUid = $this->getTypoScriptFrontendController()->id;
78 }
79
80 if (!empty($type) && !in_array($type, array('all', 'header'), true)) {
81 return '';
82 }
83
84 return $this->renderChildrenWithVariables(array(
85 $as => $this->findBySection($pageUid, $type, $this->arguments['column'])
86 ));
87 }
88
89 /**
90 * Find content with 'Show in Section Menus' enabled in a page
91 *
92 * By default only content in colPos=0 will be found. This can be overruled by using $column
93 *
94 * If you set property type to "all", then the 'Show in Section Menus' checkbox is not considered
95 * and all content elements are selected.
96 *
97 * If the property $type is 'header' then only content elements with a visible header layout
98 * (and a non-empty 'header' field!) is selected.
99 * In other words, if the header layout of an element is set to 'Hidden' then the page will not appear in the menu.
100 *
101 * @param int $pageUid The page uid
102 * @param string $type Search method
103 * @param string $column Restrict content by the column number
104 * @return array
105 */
106 protected function findBySection($pageUid, $type = '', $column = '')
107 {
108 $constraints = [];
109 if (trim($column) !== '') {
110 $colPosList = implode(',', GeneralUtility::intExplode(',', $column, true));
111 $constraints[] = 'colPos IN(' . ($colPosList !== '' ? $colPosList : '0') . ')';
112 }
113
114 switch ($type) {
115 case 'all':
116 break;
117 case 'header':
118 $constraints[] = 'sectionIndex = 1';
119 $constraints[] = 'header <> \'\'';
120 $constraints[] = 'header_layout <> 100';
121 break;
122 default:
123 $constraints[] = 'sectionIndex = 1';
124 }
125
126 $whereStatement = implode(' AND ', $constraints);
127
128 $contentElements = $this->getTypoScriptFrontendController()->cObj->getRecords('tt_content', [
129 'where' => $whereStatement,
130 'orderBy' => 'sorting',
131 'languageField = sys_language_uid',
132 'pidInList' => (int)$pageUid
133 ]);
134
135 return $contentElements;
136 }
137 }