‪TYPO3CMS  ‪main
CategoryMenuUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
29 {
33  protected static ‪$sortingField;
34 
43  public function ‪collectPages($selectedCategories, $configuration, $parentObject)
44  {
45  $selectedPages = [];
46  $categoriesPerPage = [];
47  // Determine the name of the relation field
48  $relationField = (string)$parentObject->getParentContentObject()->stdWrapValue('relation', $configuration ?? []);
49  // Get the pages for each selected category
50  $selectedCategories = ‪GeneralUtility::intExplode(',', $selectedCategories, true);
51  foreach ($selectedCategories as $aCategory) {
52  $collection = ‪CategoryCollection::load(
53  $aCategory,
54  true,
55  'pages',
56  $relationField
57  );
58  $categoryUid = 0;
59  if ($collection instanceof ‪AbstractRecordCollection) {
60  $categoryUid = $collection->getUid();
61  }
62  // Loop on the results, overlay each page record found
63  foreach ($collection as $pageItem) {
64  $parentObject->getSysPage()->versionOL('pages', $pageItem, true);
65  if (is_array($pageItem)) {
66  $selectedPages[$pageItem['uid']] = $parentObject->getSysPage()->getLanguageOverlay('pages', $pageItem);
67  // Keep a list of the categories each page belongs to
68  if (!isset($categoriesPerPage[$pageItem['uid']])) {
69  $categoriesPerPage[$pageItem['uid']] = [];
70  }
71  $categoriesPerPage[$pageItem['uid']][] = $categoryUid;
72  }
73  }
74  }
75  // Loop on the selected pages to add the categories they belong to, as comma-separated list of category uid's)
76  // (this makes them available for rendering, if needed)
77  foreach ($selectedPages as ‪$uid => $pageRecord) {
78  $selectedPages[‪$uid]['_categories'] = implode(',', $categoriesPerPage[‪$uid]);
79  }
80 
81  // Sort the pages according to the sorting property
82  self::$sortingField = (string)$parentObject->getParentContentObject()->stdWrapValue('sorting', $configuration ?? []);
83  $order = (string)$parentObject->getParentContentObject()->stdWrapValue('order', $configuration ?? []);
84  $selectedPages = $this->‪sortPages($selectedPages, $order);
85 
86  return $selectedPages;
87  }
88 
99  protected function ‪sortPages($pages, $order)
100  {
101  // Perform the sorting only if a criterion was actually defined
102  if (!empty(self::$sortingField)) {
103  // Check that the sorting field exists (checking the first record is enough)
104  $firstPage = current($pages);
105  if (isset($firstPage[self::$sortingField])) {
106  // Make sure the order property is either "asc" or "desc" (default is "asc")
107  if (!empty($order)) {
108  $order = strtolower($order);
109  if ($order !== 'desc') {
110  $order = 'asc';
111  }
112  }
113  uasort(
114  $pages,
115  [
116  self::class,
117  'sortPagesUtility',
118  ]
119  );
120  // If the sort order is descending, reverse the sorted array
121  if ($order === 'desc') {
122  $pages = array_reverse($pages, true);
123  }
124  }
125  }
126  return $pages;
127  }
128 
136  public static function ‪sortPagesUtility($pageA, $pageB)
137  {
138  return strnatcasecmp($pageA[self::$sortingField], $pageB[self::$sortingField]);
139  }
140 }
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\$sortingField
‪static string $sortingField
Definition: CategoryMenuUtility.php:32
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection
Definition: AbstractRecordCollection.php:40
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection\load
‪static CategoryCollection load($id, $fillItems=false, $tableName='', $fieldName='')
Definition: CategoryCollection.php:72
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\sortPages
‪array sortPages($pages, $order)
Definition: CategoryMenuUtility.php:98
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\sortPagesUtility
‪static int sortPagesUtility($pageA, $pageB)
Definition: CategoryMenuUtility.php:135
‪TYPO3\CMS\Frontend\ContentObject\Menu
Definition: AbstractMenuContentObject.php:16
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility\collectPages
‪array collectPages($selectedCategories, $configuration, $parentObject)
Definition: CategoryMenuUtility.php:42
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Frontend\ContentObject\Menu\CategoryMenuUtility
Definition: CategoryMenuUtility.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static list< int > intExplode(string $delimiter, string $string, bool $removeEmptyValues=false)
Definition: GeneralUtility.php:756
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection
Definition: CategoryCollection.php:33