Skip to content

Commit

Permalink
Merge pull request #1 from andersundsehr/feature/add-mm-sorting
Browse files Browse the repository at this point in the history
✨ add support for foreign_field
  • Loading branch information
Kanti authored Feb 2, 2024
2 parents 020d743 + bf6b889 commit a7122da
Show file tree
Hide file tree
Showing 3 changed files with 667 additions and 1,327 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
public/
vendor/
var/
.idea/
104 changes: 73 additions & 31 deletions Classes/DataProcessing/RelationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,74 @@ public function getRelation(ContentObjectRenderer $cObj, string $table, string $
$tcaConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config'] ?? throw new RuntimeException(
'TCA config for ' . $table . '.' . $field . ' not found'
);

$foreignTable = $tcaConfig['foreign_table'] ?? throw new RuntimeException('TCA config foreign_table not found');

if (isset($tcaConfig['foreign_field'])) {
$rows = $this->getRowsForeignField($tcaConfig, $uid);
} else {
$rows = $this->getRowsMM($tcaConfig, $foreignTable, $uid);
}

$records = [];

$pageRepository = $cObj->getTypoScriptFrontendController()?->sys_page;
$pageRepository instanceof PageRepository ?: throw new RuntimeException('PageRepository not found');

foreach ($rows as $row) {
// Versioning preview:
$pageRepository->versionOL($foreignTable, $row, true);

if (!$row) {
continue;
}

// Language overlay:
$row = $pageRepository->getLanguageOverlay($foreignTable, $row);

if (!$row) {
continue; // Might be unset in the language overlay
}

$records[] = $row;
}

return $records;
}

/**
* @param array<string, mixed> $tcaConfig
* @return list<array<string, string|int|float|bool>>
*/
private function getRowsForeignField(array $tcaConfig, int $uid): array
{
$foreignTable = $tcaConfig['foreign_table'];
$foreignField = $tcaConfig['foreign_field'];
$foreignSortby = $tcaConfig['foreign_sortby'] ?? null;
$maxitems = (int)($tcaConfig['maxitems'] ?? 0);

$queryBuilder = $this->connectionPool->getQueryBuilderForTable($foreignTable);
$queryBuilder
->select('*')
->from($foreignTable)
->where($queryBuilder->expr()->eq($foreignField, $uid));

if ($foreignSortby) {
$queryBuilder->orderBy($foreignSortby);
}

if ($maxitems) {
$queryBuilder->setMaxResults($maxitems);
}

return $queryBuilder->executeQuery()->fetchAllAssociative();
}

/**
* @return list<array<string, string|int|float|bool>>
*/
private function getRowsMM(mixed $tcaConfig, mixed $foreignTable, int $uid): array
{
if (isset($tcaConfig['MM_hasUidField'])) {
throw new RuntimeException('TCA config MM_hasUidField not supported');
}
Expand All @@ -75,15 +143,10 @@ public function getRelation(ContentObjectRenderer $cObj, string $table, string $
throw new RuntimeException('TCA config MM_oppositeUsage not supported');
}

if (isset($tcaConfig['foreign_field'])) {
//inline not supported right now
throw new RuntimeException('TCA config foreign_field not supported');
}

$mmTable = $tcaConfig['MM'] ?? throw new RuntimeException('TCA config MM not found');
$foreignTable = $tcaConfig['foreign_table'] ?? throw new RuntimeException('TCA config foreign_table not found');

$matchFields = $tcaConfig['MM_match_fields'] ?? [];
$sorting = $tcaConfig['mm_sorting_field'] ?? '';

$otherWay = isset($tcaConfig['MM_opposite_field']);

Expand All @@ -102,6 +165,9 @@ public function getRelation(ContentObjectRenderer $cObj, string $table, string $
->join('relation', $mmTable, 'mm', $queryBuilder->expr()->eq('relation.uid', 'mm.' . $otherField))
->where($queryBuilder->expr()->eq('mm.' . $selfField, $uid));

if ($sorting) {
$queryBuilder->orderBy($sorting);
}

$transOrigPointerField = $GLOBALS['TCA'][$foreignTable]['ctrl']['transOrigPointerField'] ?? null;
if ($transOrigPointerField) {
Expand All @@ -114,30 +180,6 @@ public function getRelation(ContentObjectRenderer $cObj, string $table, string $
);
}

$rows = $queryBuilder->executeQuery()->fetchAllAssociative();
$records = [];

$pageRepository = $cObj->getTypoScriptFrontendController()?->sys_page;
$pageRepository instanceof PageRepository ?: throw new RuntimeException('PageRepository not found');

foreach ($rows as $row) {
// Versioning preview:
$pageRepository->versionOL($foreignTable, $row, true);

if (!$row) {
continue;
}

// Language overlay:
$row = $pageRepository->getLanguageOverlay($foreignTable, $row);

if (!$row) {
continue; // Might be unset in the language overlay
}

$records[] = $row;
}

return $records;
return $queryBuilder->executeQuery()->fetchAllAssociative();
}
}
Loading

0 comments on commit a7122da

Please sign in to comment.