|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AUS\RelationProcessor\DataProcessing; |
| 6 | + |
| 7 | +use RuntimeException; |
| 8 | +use TYPO3\CMS\Core\Database\Connection; |
| 9 | +use TYPO3\CMS\Core\Database\ConnectionPool; |
| 10 | +use TYPO3\CMS\Core\Domain\Repository\PageRepository; |
| 11 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 12 | +use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor; |
| 13 | +use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; |
| 14 | +use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface; |
| 15 | + |
| 16 | +final readonly class RelationProcessor implements DataProcessorInterface |
| 17 | +{ |
| 18 | + public function __construct( |
| 19 | + private ConnectionPool $connectionPool, |
| 20 | + private ContentDataProcessor $contentDataProcessor |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Process content object data |
| 26 | + * |
| 27 | + * @param ContentObjectRenderer $cObj The data of the content element or page |
| 28 | + * @param array<string, mixed> $contentObjectConfiguration The configuration of Content Object |
| 29 | + * @param array<string, mixed> $processorConfiguration The configuration of this processor |
| 30 | + * @param array<string, mixed> $processedData Key/value store of processed data (e.g. to be passed to a Fluid View) |
| 31 | + * @return array<string, mixed> the processed data as key/value store |
| 32 | + */ |
| 33 | + public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData) |
| 34 | + { |
| 35 | + $table = $cObj->getCurrentTable(); |
| 36 | + $uid = $cObj->data['uid']; |
| 37 | + $field = $processorConfiguration['field']; |
| 38 | + |
| 39 | + $relations = $this->getRelation($cObj, $table, $field, $uid); |
| 40 | + $request = $cObj->getRequest(); |
| 41 | + $processedRecordVariables = []; |
| 42 | + |
| 43 | + foreach ($relations as $key => $record) { |
| 44 | + $recordContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class); |
| 45 | + $recordContentObjectRenderer->start($record, $GLOBALS['TCA'][$table]['columns'][$field]['config']['foreign_table'], $request); |
| 46 | + $processedRecordVariables[$key] = ['data' => $record]; |
| 47 | + $processedRecordVariables[$key] = $this->contentDataProcessor->process( |
| 48 | + $recordContentObjectRenderer, |
| 49 | + $processorConfiguration, |
| 50 | + $processedRecordVariables[$key] |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + $processedData['data'][$field] = $processedRecordVariables; |
| 55 | + return $processedData; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return list<array<string, string|int|float|bool>> |
| 60 | + */ |
| 61 | + public function getRelation(ContentObjectRenderer $cObj, string $table, string $field, int $uid): array |
| 62 | + { |
| 63 | + $tcaConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config'] ?? throw new RuntimeException( |
| 64 | + 'TCA config for ' . $table . '.' . $field . ' not found' |
| 65 | + ); |
| 66 | + if (isset($tcaConfig['MM_hasUidField'])) { |
| 67 | + throw new RuntimeException('TCA config MM_hasUidField not supported'); |
| 68 | + } |
| 69 | + |
| 70 | + if (isset($tcaConfig['MM_is_foreign'])) { |
| 71 | + throw new RuntimeException('TCA config MM_is_foreign not supported'); |
| 72 | + } |
| 73 | + |
| 74 | + if (isset($tcaConfig['MM_oppositeUsage'])) { |
| 75 | + throw new RuntimeException('TCA config MM_oppositeUsage not supported'); |
| 76 | + } |
| 77 | + |
| 78 | + if (isset($tcaConfig['foreign_field'])) { |
| 79 | + //inline not supported right now |
| 80 | + throw new RuntimeException('TCA config foreign_field not supported'); |
| 81 | + } |
| 82 | + |
| 83 | + $mmTable = $tcaConfig['MM'] ?? throw new RuntimeException('TCA config MM not found'); |
| 84 | + $foreignTable = $tcaConfig['foreign_table'] ?? throw new RuntimeException('TCA config foreign_table not found'); |
| 85 | + |
| 86 | + $matchFields = $tcaConfig['MM_match_fields'] ?? []; |
| 87 | + |
| 88 | + $otherWay = isset($tcaConfig['MM_opposite_field']); |
| 89 | + |
| 90 | + if ($otherWay) { |
| 91 | + $selfField = 'uid_foreign'; |
| 92 | + $otherField = 'uid_local'; |
| 93 | + } else { |
| 94 | + $selfField = 'uid_local'; |
| 95 | + $otherField = 'uid_foreign'; |
| 96 | + } |
| 97 | + |
| 98 | + $queryBuilder = $this->connectionPool->getQueryBuilderForTable($foreignTable); |
| 99 | + $queryBuilder |
| 100 | + ->select('relation.*') |
| 101 | + ->from($foreignTable, 'relation') |
| 102 | + ->join('relation', $mmTable, 'mm', $queryBuilder->expr()->eq('relation.uid', 'mm.' . $otherField)) |
| 103 | + ->where($queryBuilder->expr()->eq('mm.' . $selfField, $uid)); |
| 104 | + |
| 105 | + |
| 106 | + $transOrigPointerField = $GLOBALS['TCA'][$foreignTable]['ctrl']['transOrigPointerField'] ?? null; |
| 107 | + if ($transOrigPointerField) { |
| 108 | + $queryBuilder->andWhere($queryBuilder->expr()->eq('relation.' . $transOrigPointerField, 0)); |
| 109 | + } |
| 110 | + |
| 111 | + foreach ($matchFields as $matchField => $value) { |
| 112 | + $queryBuilder->andWhere( |
| 113 | + $queryBuilder->expr()->eq($matchField, $queryBuilder->createNamedParameter($value, Connection::PARAM_STR)) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + $rows = $queryBuilder->executeQuery()->fetchAllAssociative(); |
| 118 | + $records = []; |
| 119 | + |
| 120 | + $pageRepository = $cObj->getTypoScriptFrontendController()?->sys_page; |
| 121 | + $pageRepository instanceof PageRepository ?: throw new RuntimeException('PageRepository not found'); |
| 122 | + |
| 123 | + foreach ($rows as $row) { |
| 124 | + // Versioning preview: |
| 125 | + $pageRepository->versionOL($foreignTable, $row, true); |
| 126 | + |
| 127 | + if (!$row) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + // Language overlay: |
| 132 | + $row = $pageRepository->getLanguageOverlay($foreignTable, $row); |
| 133 | + |
| 134 | + if (!$row) { |
| 135 | + continue; // Might be unset in the language overlay |
| 136 | + } |
| 137 | + |
| 138 | + $records[] = $row; |
| 139 | + } |
| 140 | + |
| 141 | + return $records; |
| 142 | + } |
| 143 | +} |
0 commit comments