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