Skip to content

Commit

Permalink
[BUGFIX] Convert category relations to ContentBlockData
Browse files Browse the repository at this point in the history
Fixes: #157
  • Loading branch information
nhovratov committed Mar 23, 2024
1 parent 4b50543 commit 8fd2ed7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
10 changes: 4 additions & 6 deletions Classes/DataProcessing/ContentBlockDataDecorator.php
Expand Up @@ -120,16 +120,14 @@ private function handleRelation(
?PageLayoutContext $context = null,
): mixed {
$resolvedField = $resolvedRelation->resolved[$tcaFieldDefinition->getUniqueIdentifier()];
$fieldTypeName = $tcaFieldDefinition->getFieldType()->getName();
$fieldTypeEnum = FieldType::tryFrom($fieldTypeName);
$resolvedField = match ($fieldTypeEnum) {
FieldType::COLLECTION,
FieldType::RELATION => $this->transformMultipleRelation(
$fieldType = $tcaFieldDefinition->getFieldType();
$resolvedField = match ($fieldType::getTcaType()) {
'inline', 'group', 'category' => $this->transformMultipleRelation(
$resolvedField,
$depth,
$context,
),
FieldType::SELECT => $this->transformSelectRelation(
'select' => $this->transformSelectRelation(
$resolvedField,
$depth,
$context,
Expand Down
20 changes: 10 additions & 10 deletions Classes/DataProcessing/RelationResolver.php
Expand Up @@ -90,8 +90,8 @@ public function processField(
array $record,
string $table
): mixed {
$fieldTypeName = $tcaFieldDefinition->getFieldType()->getName();
$fieldTypeEnum = FieldType::tryFrom($fieldTypeName);
$fieldType = $tcaFieldDefinition->getFieldType();
$tcaType = $fieldType::getTcaType();
$recordIdentifier = $tcaFieldDefinition->getUniqueIdentifier();
if (!array_key_exists($recordIdentifier, $record)) {
throw new \RuntimeException(
Expand All @@ -101,35 +101,35 @@ public function processField(
);
}
$data = $record[$recordIdentifier];
if ($fieldTypeEnum === FieldType::FILE) {
if ($tcaType === 'file') {
$fileCollector = GeneralUtility::makeInstance(FileCollector::class);
$fileCollector->addFilesFromRelation($table, $recordIdentifier, $record);
return $fileCollector->getFiles();
}
if ($fieldTypeEnum === FieldType::COLLECTION) {
if ($tcaType === 'inline') {
return $this->processCollection($table, $record, $tcaFieldDefinition, $typeDefinition);
}
if ($fieldTypeEnum === FieldType::CATEGORY) {
if ($tcaType === 'category') {
return $this->processCategory($tcaFieldDefinition, $typeDefinition, $table, $record);
}
if ($fieldTypeEnum === FieldType::RELATION) {
if ($tcaType === 'group') {
return $this->processRelation($tcaFieldDefinition, $typeDefinition, $table, $record);
}
if ($fieldTypeEnum === FieldType::FOLDER) {
if ($tcaType === 'folder') {
$fileCollector = GeneralUtility::makeInstance(FileCollector::class);
$folders = GeneralUtility::trimExplode(',', (string)$data, true);
/** @var FolderFieldType $folderFieldType */
$folderFieldType = $tcaFieldDefinition->getFieldType();
$fileCollector->addFilesFromFolders($folders, $folderFieldType->isRecursive());
return $fileCollector->getFiles();
}
if ($fieldTypeEnum === FieldType::SELECT) {
if ($tcaType === 'select') {
return $this->processSelect($tcaFieldDefinition, $typeDefinition, $table, $record);
}
if ($fieldTypeEnum === FieldType::FLEXFORM) {
if ($tcaType === 'flex') {
return $this->flexFormService->convertFlexFormContentToArray($data);
}
if ($fieldTypeEnum === FieldType::JSON) {
if ($tcaType === 'json') {
$platform = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($table)
->getDatabasePlatform();
Expand Down
Expand Up @@ -30,3 +30,5 @@ fields:
type: Select
renderType: selectSingle
foreign_table: tt_content
- identifier: categories
type: Category
Expand Up @@ -44,3 +44,9 @@
<f:if condition="{data.circular_select}">
Circular select uid: {data.circular_select.circular_select.uid}
</f:if>
<f:if condition="{data.categories}">
<f:for each="{data.categories}" as="category">
Category table: {category.tableName}
{category.title}
</f:for>
</f:if>
18 changes: 18 additions & 0 deletions Tests/Functional/Frontend/ContentBlockFrontendRenderingTest.php
Expand Up @@ -258,4 +258,22 @@ public function circularRelationsResolved(): void
self::assertStringContainsString('Circular relation uid: 1', $html);
self::assertStringContainsString('Circular select uid: 1', $html);
}

#[Test]
public function categoryRelationResolved(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/categories.csv');
$this->setUpFrontendRootPage(
self::ROOT_PAGE_ID,
[
'EXT:content_blocks/Tests/Functional/Frontend/Fixtures/frontend.typoscript',
]
);
$response = $this->executeFrontendSubRequest((new InternalRequest())->withPageId(self::ROOT_PAGE_ID));
$html = (string)$response->getBody();

self::assertStringContainsString('Category table: sys_category', $html);
self::assertStringContainsString('Category 1', $html);
self::assertStringContainsString('Category 2', $html);
}
}
14 changes: 14 additions & 0 deletions Tests/Functional/Frontend/Fixtures/DataSet/categories.csv
@@ -0,0 +1,14 @@
"pages"
,"uid","pid","title","doktype"
,2,1,"Record Storage",254
"sys_category"
,"uid","pid","title"
,1,2,"Category 1"
,2,2,"Category 2"
"sys_category_record_mm"
,"uid_local","uid_foreign","tablenames","fieldname"
,1,1,"tt_content","simple_simplerelation_categories"
,2,1,"tt_content","simple_simplerelation_categories"
"tt_content"
,"uid","pid","t3ver_oid","t3ver_wsid","simple_simplerelation_categories","CType"
,1,1,0,0,"2","simple_simplerelation"

0 comments on commit 8fd2ed7

Please sign in to comment.