diff --git a/Classes/Persistence/Generic/Mapper/ColumnMap.php b/Classes/Persistence/Generic/Mapper/ColumnMap.php index b1e94460..7b5e9549 100644 --- a/Classes/Persistence/Generic/Mapper/ColumnMap.php +++ b/Classes/Persistence/Generic/Mapper/ColumnMap.php @@ -81,6 +81,15 @@ class ColumnMap */ private $childTableName; + /** + * todo: Check if this property should support null. If not, set default value. + * The name of the field the results from the child's table are sorted by default + * + * @see https://docs.typo3.org/m/typo3/reference-tca/main/en-us/ColumnsConfig/Type/Inline/Properties/ForeignDefaultSortby.html + * @var string|null + */ + private $childDefaultSortByFieldName; + /** * todo: Check if this property should support null. If not, set default value. * The name of the field the results from the child's table are sorted by @@ -242,6 +251,22 @@ class ColumnMap return $this->childTableName; } + /** + * @param string|null $childDefaultSortByFieldName + */ + public function setChildDefaultSortByFieldName(?string $childDefaultSortByFieldName): void + { + $this->childDefaultSortByFieldName = $childDefaultSortByFieldName; + } + + /** + * @return string|null + */ + public function getChildDefaultSortByFieldName(): ?string + { + return $this->childDefaultSortByFieldName; + } + /** * @param string|null $childSortByFieldName */ diff --git a/Classes/Persistence/Generic/Mapper/DataMapFactory.php b/Classes/Persistence/Generic/Mapper/DataMapFactory.php index 08a9473b..95aaa0ef 100644 --- a/Classes/Persistence/Generic/Mapper/DataMapFactory.php +++ b/Classes/Persistence/Generic/Mapper/DataMapFactory.php @@ -369,6 +369,7 @@ class DataMapFactory implements SingletonInterface } // todo: don't update column map if value(s) isn't/aren't set. $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby'] ?? null); + $columnMap->setChildDefaultSortByFieldName($columnConfiguration['foreign_default_sortby'] ?? null); $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field'] ?? null); $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field'] ?? null); if (isset($columnConfiguration['foreign_match_fields']) && is_array($columnConfiguration['foreign_match_fields'])) { @@ -397,6 +398,7 @@ class DataMapFactory implements SingletonInterface } // todo: don't update column map if value(s) isn't/aren't set. $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby'] ?? null); + $columnMap->setChildDefaultSortByFieldName($columnConfiguration['foreign_default_sortby'] ?? null); $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field'] ?? null); $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field'] ?? null); if (isset($columnConfiguration['foreign_match_fields']) && is_array($columnConfiguration['foreign_match_fields'])) { diff --git a/Classes/Persistence/Generic/Mapper/DataMapper.php b/Classes/Persistence/Generic/Mapper/DataMapper.php index 4efdf7bc..eb62c06e 100644 --- a/Classes/Persistence/Generic/Mapper/DataMapper.php +++ b/Classes/Persistence/Generic/Mapper/DataMapper.php @@ -414,19 +414,42 @@ class DataMapper $query->getQuerySettings()->setLanguageAspect($languageAspect); if ($columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_MANY) { - if ($columnMap->getChildSortByFieldName() !== null) { - $query->setOrderings([$columnMap->getChildSortByFieldName() => QueryInterface::ORDER_ASCENDING]); + if ($columnMap->getChildSortByFieldName() !== null || $columnMap->getChildDefaultSortByFieldName() !== null) { + $query->setOrderings($this->getOrderingsForColumnMap($columnMap)); } } elseif ($columnMap->getTypeOfRelation() === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) { $query->setSource($this->getSource($parentObject, $propertyName)); - if ($columnMap->getChildSortByFieldName() !== null) { - $query->setOrderings([$columnMap->getChildSortByFieldName() => QueryInterface::ORDER_ASCENDING]); + if ($columnMap->getChildSortByFieldName() !== null || $columnMap->getChildDefaultSortByFieldName() !== null) { + $query->setOrderings($this->getOrderingsForColumnMap($columnMap)); } } $query->matching($this->getConstraint($query, $parentObject, $propertyName, $fieldValue, (array)$columnMap->getRelationTableMatchFields())); return $query; } + /** + * Get orderings array for extbase query by columnMap + * + * @param ColumnMap $columnMap + * @return array + */ + protected function getOrderingsForColumnMap(ColumnMap $columnMap): array + { + $configuration = GeneralUtility::trimExplode(' ', $columnMap->getChildSortByFieldName() ?? $columnMap->getChildDefaultSortByFieldName()); + $fields = GeneralUtility::trimExplode(',', $configuration[0]); + // default direction is ascending + $direction = QueryInterface::ORDER_ASCENDING; + $directionValues = [QueryInterface::ORDER_ASCENDING, QueryInterface::ORDER_DESCENDING]; + if (array_key_exists(1, $configuration) && in_array(strtoupper($configuration[1]), $directionValues, true)) { + $direction = strtoupper($configuration[1]); + } + $orderings = []; + foreach ($fields as $field) { + $orderings[$field] = $direction; + } + return $orderings; + } + /** * Builds and returns the constraint for multi value properties. *