Skip to content

Commit

Permalink
✨ add support for foreign_field
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Feb 2, 2024
1 parent 61e399a commit bf6b889
Show file tree
Hide file tree
Showing 2 changed files with 388 additions and 350 deletions.
107 changes: 73 additions & 34 deletions Classes/DataProcessing/RelationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu
$table = $cObj->getCurrentTable();
$uid = $cObj->data['uid'];
$field = $processorConfiguration['field'];
$sorting = $processorConfiguration['mm_sorting_field'] ?? '';

$relations = $this->getRelation($cObj, $table, $field, $uid, $sorting);
$relations = $this->getRelation($cObj, $table, $field, $uid);
$request = $cObj->getRequest();
$processedRecordVariables = [];

Expand All @@ -59,11 +58,79 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu
/**
* @return list<array<string, string|int|float|bool>>
*/
public function getRelation(ContentObjectRenderer $cObj, string $table, string $field, int $uid, string $sorting): array
public function getRelation(ContentObjectRenderer $cObj, string $table, string $field, int $uid): array
{
$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 @@ -76,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 +164,7 @@ public function getRelation(ContentObjectRenderer $cObj, string $table, string $
->from($foreignTable, 'relation')
->join('relation', $mmTable, 'mm', $queryBuilder->expr()->eq('relation.uid', 'mm.' . $otherField))
->where($queryBuilder->expr()->eq('mm.' . $selfField, $uid));

if ($sorting) {
$queryBuilder->orderBy($sorting);
}
Expand All @@ -117,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 bf6b889

Please sign in to comment.