TYPO3 CMS  TYPO3_7-6
CategoriesViewHelper.php
Go to the documentation of this file.
1 <?php
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 {
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([
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 = [];
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 }
static load($id, $fillItems=false, $tableName='', $fieldName='')
registerArgument($name, $type, $description, $required=false, $defaultValue=null)
findByCategories($categoryUids, $relationField, $tableName='pages')