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