[!!!][FEATURE] Streamline Fluid Styled Content and CSS Styled Content
[Packages/TYPO3.CMS.git] / typo3 / sysext / compatibility7 / Classes / ViewHelpers / Menu / CategoriesViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Compatibility7\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\Exception;
18 use TYPO3\CMS\Frontend\Category\Collection\CategoryCollection;
19
20 /**
21 * A view helper which returns records with assigned categories
22 *
23 * = Example =
24 *
25 * <code title="Pages with categories 1 and 2 assigned">
26 * <ce:menu.categories categoryUids="{0: 1, 1: 2}" as="pages" relationField="categories" table="pages">
27 * <f:for each="{pages}" as="page">
28 * {page.title}
29 * </f:for>
30 * </ce:menu.categories>
31 * </code>
32 *
33 * <output>
34 * Page with category 1 assigned
35 * Page with category 1 and 2 assigned
36 * </output>
37 */
38 class CategoriesViewHelper extends AbstractMenuViewHelper
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 * @return void
51 */
52 public function initializeArguments()
53 {
54 $this->registerArgument('categoryUids', 'array', 'The categories assigned', true);
55 $this->registerArgument('as', 'string', 'Name of the template variable that will contain resolved pages', true);
56 $this->registerArgument('relationField', 'string', 'The category field for MM relation table', true);
57 $this->registerArgument('table', 'string', 'The table to which categories are assigned (source table)', true);
58 }
59
60 /**
61 * Render the view helper
62 *
63 * @return string
64 */
65 public function render()
66 {
67 $categoryUids = (array)$this->arguments['categoryUids'];
68 $as = (string)$this->arguments['as'];
69 if (empty($categoryUids)) {
70 return '';
71 }
72
73 return $this->renderChildrenWithVariables([
74 $as => $this->findByCategories($categoryUids, $this->arguments['relationField'], $this->arguments['table'])
75 ]);
76 }
77
78 /**
79 * Find records from a certain table which have categories assigned
80 *
81 * @param array $categoryUids The uids of the categories
82 * @param string $relationField Field relation in MM table
83 * @param string $tableName Name of the table to search in
84 * @return array
85 * @throws Exception
86 */
87 protected function findByCategories($categoryUids, $relationField, $tableName = 'pages')
88 {
89 $result = [];
90
91 foreach ($categoryUids as $categoryUid) {
92 try {
93 $collection = CategoryCollection::load(
94 $categoryUid,
95 true,
96 $tableName,
97 $relationField
98 );
99 if ($collection->count() > 0) {
100 foreach ($collection as $record) {
101 $result[$record['uid']] = $record;
102 }
103 }
104 } catch (\RuntimeException $e) {
105 throw new Exception($e->getMessage(), 1476122186);
106 }
107 }
108
109 return $result;
110 }
111 }