Skip to content

Commit

Permalink
[!!!][TASK] Use Core RelationResolver (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhovratov authored Aug 27, 2024
1 parent 215aa94 commit dc1730d
Show file tree
Hide file tree
Showing 46 changed files with 268 additions and 1,682 deletions.
56 changes: 17 additions & 39 deletions Classes/Backend/Preview/PageLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,31 @@

namespace TYPO3\CMS\ContentBlocks\Backend\Preview;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Controller\Event\ModifyPageLayoutContentEvent;
use TYPO3\CMS\Backend\Module\ModuleData;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockDataDecorator;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentTypeResolver;
use TYPO3\CMS\ContentBlocks\DataProcessing\RelationResolver;
use TYPO3\CMS\ContentBlocks\Definition\ContentType\ContentTypeInterface;
use TYPO3\CMS\ContentBlocks\Definition\TableDefinition;
use TYPO3\CMS\ContentBlocks\Definition\TableDefinitionCollection;
use TYPO3\CMS\ContentBlocks\Registry\ContentBlockRegistry;
use TYPO3\CMS\ContentBlocks\Utility\ContentBlockPathUtility;
use TYPO3\CMS\Core\Domain\RecordFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
* @internal Not part of TYPO3's public API.
*/
class PageLayout
readonly class PageLayout
{
public function __construct(
protected readonly TableDefinitionCollection $tableDefinitionCollection,
protected readonly RelationResolver $relationResolver,
protected readonly ContentBlockRegistry $contentBlockRegistry,
protected readonly ContentBlockDataDecorator $contentBlockDataDecorator,
protected readonly RootPathsSettings $rootPathsSettings,
protected TableDefinitionCollection $tableDefinitionCollection,
protected RecordFactory $recordFactory,
protected ContentBlockRegistry $contentBlockRegistry,
protected ContentBlockDataDecorator $contentBlockDataDecorator,
protected RootPathsSettings $rootPathsSettings,
protected ContentTypeResolver $contentTypeResolver,
) {}

public function __invoke(ModifyPageLayoutContentEvent $event): void
Expand All @@ -59,21 +57,23 @@ public function __invoke(ModifyPageLayoutContentEvent $event): void
if (!$this->tableDefinitionCollection->hasTable($pageTypeTable)) {
return;
}
$tableDefinition = $this->tableDefinitionCollection->getTable($pageTypeTable);
$contentTypeDefinitionCollection = $tableDefinition->getContentTypeDefinitionCollection();
if ($contentTypeDefinitionCollection === null) {
return;
}
$pageUid = (int)($request->getQueryParams()['id'] ?? 0);
$pageRow = BackendUtility::getRecord($pageTypeTable, $pageUid);
$contentTypeDefinition = ContentTypeResolver::resolve($tableDefinition, $pageRow);
if ($pageRow === null) {
return;
}
$resolvedRecord = $this->recordFactory->createResolvedRecordFromDatabaseRow(
$pageTypeTable,
$pageRow,
);
$contentTypeDefinition = $this->contentTypeResolver->resolve($resolvedRecord);
if ($contentTypeDefinition === null) {
return;
}
if ($this->getEditorPreviewExtPath($contentTypeDefinition) === null) {
return;
}
$contentBlockData = $this->getContentBlockData($pageRow, $request, $contentTypeDefinition, $tableDefinition);
$contentBlockData = $this->contentBlockDataDecorator->decorate($resolvedRecord);
$view = $this->createView($contentTypeDefinition, $pageUid);
$view->setRequest($request);
$view->assign('data', $contentBlockData);
Expand Down Expand Up @@ -129,26 +129,4 @@ protected function getEditorPreviewExtPath(ContentTypeInterface $contentTypeDefi
}
return $contentBlockExtPath;
}

protected function getContentBlockData(
?array $pageRow,
ServerRequestInterface $request,
ContentTypeInterface $contentTypeDefinition,
TableDefinition $tableDefinition,
): ContentBlockData {
$pageTypeTable = 'pages';
$this->relationResolver->setRequest($request);
$resolvedData = $this->relationResolver->resolve(
$contentTypeDefinition,
$tableDefinition,
$pageRow,
$pageTypeTable,
);
$contentBlockData = $this->contentBlockDataDecorator->decorate(
$contentTypeDefinition,
$tableDefinition,
$resolvedData,
);
return $contentBlockData;
}
}
19 changes: 4 additions & 15 deletions Classes/Backend/Preview/PreviewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
use TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer;
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockDataDecorator;
use TYPO3\CMS\ContentBlocks\DataProcessing\RelationResolver;
use TYPO3\CMS\ContentBlocks\Definition\TableDefinitionCollection;
use TYPO3\CMS\ContentBlocks\Registry\ContentBlockRegistry;
use TYPO3\CMS\ContentBlocks\Utility\ContentBlockPathUtility;
use TYPO3\CMS\Core\Domain\RecordFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

Expand All @@ -37,7 +37,7 @@ class PreviewRenderer extends StandardContentPreviewRenderer
{
public function __construct(
protected TableDefinitionCollection $tableDefinitionCollection,
protected RelationResolver $relationResolver,
protected RecordFactory $recordFactory,
protected ContentBlockRegistry $contentBlockRegistry,
protected ContentBlockDataDecorator $contentBlockDataDecorator,
protected RootPathsSettings $rootPathsSettings,
Expand Down Expand Up @@ -67,19 +67,8 @@ public function renderPageModulePreviewContent(GridColumnItem $item): string
$result = parent::renderPageModulePreviewContent($item);
return $result;
}
$this->relationResolver->setRequest($request);
$resolvedData = $this->relationResolver->resolve(
$contentTypeDefinition,
$tableDefinition,
$record,
$table,
);
$data = $this->contentBlockDataDecorator->decorate(
$contentTypeDefinition,
$tableDefinition,
$resolvedData,
$item->getContext(),
);
$resolvedRecord = $this->recordFactory->createResolvedRecordFromDatabaseRow($table, $record);
$data = $this->contentBlockDataDecorator->decorate($resolvedRecord, $item->getContext());
$view = $this->createView($contentBlockPrivatePath, $request, $item);
$view->assign('data', $data);
$result = (string)$view->render();
Expand Down
2 changes: 1 addition & 1 deletion Classes/DataProcessing/ContentBlockData.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* - {data.computedProperties.localizedUid}
* - {data.computedProperties.versionedUid}
*/
final class ContentBlockData implements \ArrayAccess, RecordInterface
final class ContentBlockData implements RecordInterface, \ArrayAccess
{
public function __construct(
protected ?Record $_record = null,
Expand Down
96 changes: 41 additions & 55 deletions Classes/DataProcessing/ContentBlockDataDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
use TYPO3\CMS\ContentBlocks\Definition\TableDefinitionCollection;
use TYPO3\CMS\ContentBlocks\Definition\TcaFieldDefinition;
use TYPO3\CMS\ContentBlocks\FieldType\FieldType;
use TYPO3\CMS\Core\Domain\RecordFactory;
use TYPO3\CMS\Core\Collection\LazyRecordCollection;
use TYPO3\CMS\Core\Domain\Record;

/**
* @internal Not part of TYPO3's public API.
Expand All @@ -38,7 +39,7 @@ public function __construct(
private readonly ContentBlockDataDecoratorSession $contentBlockDataDecoratorSession,
private readonly GridProcessor $gridProcessor,
private readonly ContentObjectProcessor $contentObjectProcessor,
private readonly RecordFactory $recordFactory,
private readonly ContentTypeResolver $contentTypeResolver,
) {}

public function setRequest(ServerRequestInterface $request): void
Expand All @@ -47,18 +48,15 @@ public function setRequest(ServerRequestInterface $request): void
$this->contentObjectProcessor->setRequest($request);
}

public function decorate(
ContentTypeInterface $contentTypeDefinition,
TableDefinition $tableDefinition,
ResolvedRelation $resolvedRelation,
?PageLayoutContext $context = null,
): ContentBlockData {
$identifier = $this->getRecordIdentifier($resolvedRelation->table, $resolvedRelation->raw);
public function decorate(Record $resolvedRecord, ?PageLayoutContext $context = null): ContentBlockData
{
$tableDefinition = $this->tableDefinitionCollection->getTable($resolvedRecord->getMainType());
$contentTypeDefinition = $this->contentTypeResolver->resolve($resolvedRecord);
$identifier = $this->getRecordIdentifier($resolvedRecord);
$this->contentBlockDataDecoratorSession->addContentBlockData($identifier, new ContentBlockData());
$record = $this->recordFactory->createFromDatabaseRow($resolvedRelation->table, $resolvedRelation->raw);
$resolvedContentBlockDataRelation = new ResolvedContentBlockDataRelation();
$resolvedContentBlockDataRelation->record = $record;
$resolvedContentBlockDataRelation->resolved = $resolvedRelation->resolved;
$resolvedContentBlockDataRelation->record = $resolvedRecord;
$resolvedContentBlockDataRelation->resolved = $resolvedRecord->toArray();
$contentBlockData = $this->buildContentBlockDataObjectRecursive(
$contentTypeDefinition,
$tableDefinition,
Expand Down Expand Up @@ -88,11 +86,10 @@ private function buildContentBlockDataObjectRecursive(
if ($fieldTypeEnum?->isStructureField()) {
continue;
}
$resolvedField = $resolvedRelation->resolved[$tcaFieldDefinition->getUniqueIdentifier()];
$resolvedField = $resolvedRelation->record[$tcaFieldDefinition->getUniqueIdentifier()];
if ($this->isRelationField($resolvedField)) {
$resolvedField = $this->handleRelation(
$resolvedRelation,
$tcaFieldDefinition,
$resolvedField,
$depth,
$context,
);
Expand Down Expand Up @@ -121,7 +118,7 @@ private function handleGrids(
): array {
if ($context === null && $this->request !== null) {
$renderedGridItemDataObjects = $resolvedField;
if (!is_array($renderedGridItemDataObjects)) {
if (!is_iterable($renderedGridItemDataObjects)) {
$renderedGridItemDataObjects = [$renderedGridItemDataObjects];
}
foreach ($renderedGridItemDataObjects as $contentBlockDataObject) {
Expand Down Expand Up @@ -154,53 +151,43 @@ private function handleGrids(
}

private function handleRelation(
ResolvedContentBlockDataRelation $resolvedRelation,
TcaFieldDefinition $tcaFieldDefinition,
Record|LazyRecordCollection $resolvedField,
int $depth,
?PageLayoutContext $context = null,
): mixed {
$resolvedField = $resolvedRelation->resolved[$tcaFieldDefinition->getUniqueIdentifier()];
$fieldType = $tcaFieldDefinition->getFieldType();
$resolvedField = match ($fieldType::getTcaType()) {
'inline', 'group', 'category' => $this->transformMultipleRelation(
$resolvedField,
$depth,
$context,
),
'select' => $this->transformSelectRelation(
): ContentBlockData|LazyRecordCollection {
if ($resolvedField instanceof LazyRecordCollection) {
$resolvedField = $this->transformMultipleRelation(
$resolvedField,
$depth,
$context,
),
default => $resolvedField,
};
);
return $resolvedField;
}
$resolvedField = $this->transformSelectRelation(
$resolvedField,
$depth,
$context,
);
return $resolvedField;
}

private function isRelationField(mixed $resolvedField): bool
{
if ($resolvedField instanceof ResolvedRelation) {
if ($resolvedField instanceof Record) {
return true;
}
if (!is_array($resolvedField)) {
return false;
}
if (($resolvedField[0] ?? null) instanceof ResolvedRelation) {
if ($resolvedField instanceof LazyRecordCollection) {
return true;
}
return false;
}

/**
* @param ResolvedRelation[] $processedField
* @return array<ContentBlockData>|ContentBlockData
*/
private function transformSelectRelation(
array|ResolvedRelation $processedField,
LazyRecordCollection|Record $processedField,
int $depth,
?PageLayoutContext $context = null,
): array|ContentBlockData {
if ($processedField instanceof ResolvedRelation) {
): LazyRecordCollection|ContentBlockData {
if ($processedField instanceof Record) {
$processedField = $this->transformSingleRelation(
$processedField,
$depth,
Expand All @@ -217,13 +204,13 @@ private function transformSelectRelation(
}

/**
* @return array<ContentBlockData>
* @return LazyRecordCollection<ContentBlockData>
*/
private function transformMultipleRelation(
array $processedField,
LazyRecordCollection $processedField,
int $depth,
?PageLayoutContext $context = null,
): array {
): LazyRecordCollection {
foreach ($processedField as $key => $processedFieldItem) {
$processedField[$key] = $this->transformSingleRelation(
$processedFieldItem,
Expand All @@ -235,26 +222,25 @@ private function transformMultipleRelation(
}

private function transformSingleRelation(
ResolvedRelation $item,
Record $item,
int $depth,
?PageLayoutContext $context = null,
): ContentBlockData {
$contentBlockRelation = new ResolvedContentBlockDataRelation();
$foreignTable = $item->table;
$record = $this->recordFactory->createFromDatabaseRow($item->table, $item->raw);
$contentBlockRelation->record = $record;
$contentBlockRelation->resolved = $item->resolved;
$foreignTable = $item->getMainType();
$contentBlockRelation->record = $item;
$contentBlockRelation->resolved = $item->toArray();
$hasTableDefinition = $this->tableDefinitionCollection->hasTable($foreignTable);
$collectionTableDefinition = null;
if ($hasTableDefinition) {
$collectionTableDefinition = $this->tableDefinitionCollection->getTable($foreignTable);
}
$typeDefinition = null;
if ($hasTableDefinition) {
$typeDefinition = ContentTypeResolver::resolve($collectionTableDefinition, $contentBlockRelation->record->getRawRecord()->toArray());
$typeDefinition = $this->contentTypeResolver->resolve($contentBlockRelation->record);
}
if ($collectionTableDefinition !== null && $typeDefinition !== null) {
$identifier = $this->getRecordIdentifier($foreignTable, $contentBlockRelation->record->toArray());
$identifier = $this->getRecordIdentifier($contentBlockRelation->record);
if ($this->contentBlockDataDecoratorSession->hasContentBlockData($identifier)) {
$contentBlockData = $this->contentBlockDataDecoratorSession->getContentBlockData($identifier);
return $contentBlockData;
Expand Down Expand Up @@ -302,9 +288,9 @@ private function buildFakeContentBlockDataObject(ResolvedContentBlockDataRelatio
return $contentBlockDataObject;
}

private function getRecordIdentifier(string $table, array $record): string
private function getRecordIdentifier(Record $record): string
{
$identifier = $table . '-' . ($record['_LOCALIZED_UID'] ?? $record['uid']);
$identifier = $record->getMainType() . '-' . $record->getOverlaidUid();
return $identifier;
}
}
Loading

0 comments on commit dc1730d

Please sign in to comment.