TYPO3  7.6
CategoriesViewHelper.php
Go to the documentation of this file.
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 
20 
40 {
41  use MenuViewHelperTrait;
42 
48  public function initializeArguments()
49  {
50  $this->registerArgument('categoryUids', 'array', 'The categories assigned', true);
51  $this->registerArgument('as', 'string', 'Name of the template variable that will contain resolved pages', true);
52  $this->registerArgument('relationField', 'string', 'The category field for MM relation table', true);
53  $this->registerArgument('table', 'string', 'The table to which categories are assigned (source table)', true);
54  }
55 
61  public function render()
62  {
63  $categoryUids = (array)$this->arguments['categoryUids'];
64  $as = (string)$this->arguments['as'];
65  if (empty($categoryUids)) {
66  return '';
67  }
68 
69  return $this->renderChildrenWithVariables(array(
70  $as => $this->findByCategories($categoryUids, $this->arguments['relationField'], $this->arguments['table'])
71  ));
72  }
73 
83  protected function findByCategories($categoryUids, $relationField, $tableName = 'pages')
84  {
85  $result = array();
86 
87  foreach ($categoryUids as $categoryUid) {
88  try {
89  $collection = CategoryCollection::load(
90  $categoryUid,
91  true,
92  $tableName,
93  $relationField
94  );
95  if ($collection->count() > 0) {
96  foreach ($collection as $record) {
97  $result[$record['uid']] = $record;
98  }
99  }
100  } catch (\RuntimeException $e) {
101  throw new Exception($e->getMessage());
102  }
103  }
104 
105  return $result;
106  }
107 }