diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php index a811d3f2..a8cfc67d 100755 --- a/Classes/ContentObject/JsonContentObject.php +++ b/Classes/ContentObject/JsonContentObject.php @@ -66,7 +66,7 @@ public function render($conf = []): string $data = $this->cObjGet($conf['fields.']); } if (isset($conf['dataProcessing.'])) { - $data = $this->processFieldWithDataProcessing($conf); + $data = $this->processFieldWithDataProcessing($conf, $data); } $json = ''; @@ -100,7 +100,6 @@ public function cObjGet(array $setup, string $addKey = ''): array $theValue = $setup[$theKey]; if ((string)$theKey && strpos($theKey, '.') === false) { $conf = $setup[$theKey . '.'] ?? []; - $contentDataProcessing['dataProcessing.'] = $conf['dataProcessing.'] ?? []; $content[$theKey] = $this->cObj->cObjGetSingle($theValue, $conf, $addKey . $theKey); if ((isset($conf['intval']) && $conf['intval']) || $theValue === 'INT') { $content[$theKey] = (int)$content[$theKey]; @@ -117,19 +116,21 @@ public function cObjGet(array $setup, string $addKey = ''): array if ((int)($conf['ifEmptyReturnNull'] ?? 0) === 1 && $content[$theKey] === '') { $content[$theKey] = null; } - if (!empty($contentDataProcessing['dataProcessing.'])) { - $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($contentDataProcessing); + if (!empty($conf['dataProcessing.'] ?? [])) { + $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($conf); } } if ((string)$theKey && strpos($theKey, '.') > 0 && !isset($setup[rtrim($theKey, '.')])) { $contentFieldName = $theValue['source'] ?? rtrim($theKey, '.'); - $contentFieldTypeProcessing['dataProcessing.'] = $theValue['dataProcessing.'] ?? []; if (array_key_exists('fields.', $theValue)) { $content[$contentFieldName] = $this->cObjGet($theValue['fields.']); } - if (!empty($contentFieldTypeProcessing['dataProcessing.'])) { - $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($contentFieldTypeProcessing); + if (!empty($theValue['dataProcessing.'] ?? [])) { + $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing( + $theValue, + $content[rtrim($theKey, '.')] ?? null + ); } } } @@ -155,11 +156,10 @@ protected function filterByStringKeys(array $setupArr, bool $acceptAnyKeys = fal return array_unique($filteredKeys); } - /** - * @param array $dataProcessing - */ - protected function processFieldWithDataProcessing(array $dataProcessing): mixed + protected function processFieldWithDataProcessing(array $conf, mixed $fieldsData = null): mixed { + $dataProcessing['dataProcessing.'] = $conf['dataProcessing.'] ?? []; + $data = $this->contentDataProcessor->process( $this->cObj, $dataProcessing, @@ -177,7 +177,8 @@ protected function processFieldWithDataProcessing(array $dataProcessing): mixed $dataProcessingData = $data[$value]; } } - return $dataProcessingData; + $merge = ((int)($conf['merge'] ?? 0) === 1) && is_array($fieldsData) && is_array($dataProcessingData); + return $merge ? array_merge($fieldsData, $dataProcessingData) : $dataProcessingData; } /** diff --git a/Tests/Unit/ContentObject/JsonContentObjectTest.php b/Tests/Unit/ContentObject/JsonContentObjectTest.php index bc121ec0..b6f28d5a 100644 --- a/Tests/Unit/ContentObject/JsonContentObjectTest.php +++ b/Tests/Unit/ContentObject/JsonContentObjectTest.php @@ -156,15 +156,17 @@ public function dataProvider(): array [['fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites'], '20' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '20.' => ['as' => 'sites']]]]], json_encode(['test' => ['SomeCustomProcessing']])], [['fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites'], 'dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']]]]]], json_encode(['test' => ['SomeCustomProcessing']])], [['fields.' => ['test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites'], 'dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']]]]]], json_encode(['test' => ['SomeCustomProcessing']])], - [['returnNullIfDataProcessingEmpty'=>1, 'fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\EmptyDataProcessingExample', '10.' => ['as' => 'sites']]]]], json_encode(['test' => [null]])], + [['returnNullIfDataProcessingEmpty' => 1, 'fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\EmptyDataProcessingExample', '10.' => ['as' => 'sites']]]]], json_encode(['test' => [null]])], [['fields.' => ['test' => 'INT', 'test.' => ['value' => 1]]], json_encode(['test' => 1])], [['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 0]]], json_encode(['test' => false])], [['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1]]], json_encode(['test' => true])], - [['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1], 'nested.' => ['fields.' => ['nestedTest' => 'INT', 'nestedTest.' => ['value' => 10]]] ]], json_encode(['test' => true, 'nested' => ['nestedTest' => 10]])], + [['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1], 'nested.' => ['fields.' => ['nestedTest' => 'INT', 'nestedTest.' => ['value' => 10]]]]], json_encode(['test' => true, 'nested' => ['nestedTest' => 10]])], [['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1], 'nested.' => ['fields.' => ['nestedTest' => 'INT', 'nestedTest.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']]]]]]], json_encode(['test' => true, 'nested' => ['nestedTest' => ['SomeCustomProcessing']]])], [['fields.' => ['test' => 'FLOAT', 'test.' => ['value' => 12.34]]], json_encode(['test' => 12.34])], [['fields.' => ['test' => 'USER_INT', 'test.' => ['userFunc' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\ExampleUserFunc->someUserFunc']]], json_encode(['test' => 'HEADLESS_INT_START<<>>HEADLESS_INT_END'])], [['fields.' => ['test' => 'USER', 'test.' => ['userFunc' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\ExampleUserFunc->someUserFunc']]], json_encode(['test' => ['test2' => 'someExtraCustomData']])], + [['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']], 'merge' => '1', 'fields.' => ['test' => 'TEXT', 'test.' => ['value' => '1']]], json_encode(['test' => '1', 'SomeCustomProcessing'])], + [['fields.' => ['test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']], 'merge' => '1', 'fields.' => ['nested' => 'TEXT', 'nested.' => ['value' => '1']]]]], json_encode(['test' => ['nested' => '1', 'SomeCustomProcessing']])], ]; } }