Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] DatabaseQueryProcessor improvements #719

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions Classes/DataProcessing/DatabaseQueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,12 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu
$targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'records');

$records = $cObj->getRecords($tableName, $processorConfiguration);
$processedRecordVariables = $this->processRecordVariables($cObj, $records, $tableName, $processorConfiguration);

$processedData[$targetVariableName] = $processedRecordVariables;

return $processedData;
}

/**
* @param array $records
* @param string $tableName
* @param array $processorConfiguration
*
* @return array
*/
private function processRecordVariables(ContentObjectRenderer $cObj, array $records, string $tableName, array $processorConfiguration): array
{
$request = $cObj->getRequest();
$processedRecordVariables = [];
foreach ($records as $key => $record) {
$recordContentObjectRenderer = $this->createContentObjectRenderer();
$recordContentObjectRenderer->setRequest($request);
$recordContentObjectRenderer->start($record, $tableName);
$recordContentObjectRenderer->setRequest($cObj->getRequest());

$objConf = [];
$objName = '< ' . $tableName;
Expand All @@ -124,7 +109,7 @@ private function processRecordVariables(ContentObjectRenderer $cObj, array $reco
$objConf = $this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $fields, '_typoScriptNodeValue' => 'JSON']);
}

$processedRecordVariables[$key] = \json_decode($recordContentObjectRenderer->cObjGetSingle($objName, $objConf), true);
$processedRecordVariables[$key] = $objConf !== [] ? \json_decode($recordContentObjectRenderer->cObjGetSingle($objName, $objConf), true) : $record;
$processedRecordVariables[$key] = $this->contentDataProcessor->process($recordContentObjectRenderer, $processorConfiguration, $processedRecordVariables[$key]);

if (isset($processorConfiguration['overrideFields.'])) {
Expand All @@ -142,7 +127,9 @@ private function processRecordVariables(ContentObjectRenderer $cObj, array $reco
}
}

return $processedRecordVariables;
$processedData[$targetVariableName] = $processedRecordVariables;

return $processedData;
}

protected function createContentObjectRenderer(): ContentObjectRenderer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tt_content.menu_categorized_content {
menu = TEXT
menu {
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\DatabaseQueryProcessor
10 = headless-database-query
10 {
table = tt_content
selectFields = {#tt_content}.*
Expand All @@ -27,12 +27,24 @@ tt_content.menu_categorized_content {
orderBy = tt_content.sorting
as = menu
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
10 = headless-files
10 {
references.fieldName = image
as = media
}
}

fields {
uid = INT
uid {
field = uid
}

header = TEXT
header {
field = header
}
}
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions Configuration/TypoScript/ContentElement/MenuSection.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tt_content.menu_section {
menu = TEXT
menu {
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\MenuProcessor
10 = headless-menu
10 {
appendData = 1
as = menu
Expand All @@ -30,25 +30,37 @@ tt_content.menu_section {
}

dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
10 = headless-files
10 {
references.fieldName = media
as = media
}
20 = FriendsOfTYPO3\Headless\DataProcessing\DatabaseQueryProcessor
20 = headless-database-query
20 {
table = tt_content
pidInList.field = uid
as = content
where = sectionIndex = 1
orderBy = sorting
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
10 = headless-files
10 {
references.fieldName = image
as = media
}
}

fields {
uid = INT
uid {
field = uid
}

header = TEXT
header {
field = header
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,43 @@ tt_content.menu_section_pages {
menu = TEXT
menu {
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\MenuProcessor
10 = headless-menu
10 {
appendData = 1
special = directory
special.value.field = pages
as = menu
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
10 = headless-files
10 {
references.fieldName = media
as = media
}
20 = FriendsOfTYPO3\Headless\DataProcessing\DatabaseQueryProcessor
20 = headless-database-query
20 {
table = tt_content
pidInList.field = uid
orderBy = sorting
as = content
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
10 = headless-files
10 {
references.fieldName = image
as = media
}
}

fields {
uid = INT
uid {
field = uid
}

header = TEXT
header {
field = header
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@ public function testMenuContentElement()
self::assertIsArray($contentElement['content']['menu'][1]);

$firstCategorizedContentElement = $contentElement['content']['menu'][0];
self::assertEquals('17', $firstCategorizedContentElement['id']);
self::assertEquals('header', $firstCategorizedContentElement['type']);
self::assertEquals('default', $firstCategorizedContentElement['appearance']['frameClass']);
self::assertEquals('1', $firstCategorizedContentElement['colPos']);
self::assertEquals('SysCategory3Title', $firstCategorizedContentElement['categories']);
self::assertSame(17, $firstCategorizedContentElement['uid']);
self::assertArrayHasKey('header', $firstCategorizedContentElement);
self::assertArrayHasKey('media', $firstCategorizedContentElement);

$secondCategorizedContentElement = $contentElement['content']['menu'][1];
self::assertEquals('18', $secondCategorizedContentElement['id']);
self::assertEquals('textpic', $secondCategorizedContentElement['type']);
self::assertEquals('default', $secondCategorizedContentElement['appearance']['frameClass']);
self::assertEquals('1', $secondCategorizedContentElement['colPos']);
self::assertEquals('SysCategory3Title', $secondCategorizedContentElement['categories']);
self::assertSame(18, $secondCategorizedContentElement['uid']);
self::assertArrayHasKey('header', $secondCategorizedContentElement);
self::assertArrayHasKey('media', $secondCategorizedContentElement);
}
}