diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php index b7045281..fd6adb2a 100755 --- a/Classes/ContentObject/JsonContentObject.php +++ b/Classes/ContentObject/JsonContentObject.php @@ -69,7 +69,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 = ''; @@ -104,7 +104,6 @@ public function cObjGet(array $setup, string $addKey = ''): array $theValue = $setup[$theKey]; if ((string)$theKey && !str_contains($theKey, '.')) { $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]; @@ -124,19 +123,24 @@ public function cObjGet(array $setup, string $addKey = ''): array if ((int)($conf['ifEmptyUnsetKey'] ?? 0) === 1 && ($content[$theKey] === '' || $content[$theKey] === false)) { unset($content[$theKey]); } - if (!empty($contentDataProcessing['dataProcessing.'])) { - $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($contentDataProcessing); + if (!empty($conf['dataProcessing.'] ?? [])) { + $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing( + $conf, + $content[rtrim($theKey, '.')] + ); } } 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 + ); } } } @@ -162,11 +166,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, @@ -183,7 +186,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 dd041fc7..8e3733d7 100644 --- a/Tests/Unit/ContentObject/JsonContentObjectTest.php +++ b/Tests/Unit/ContentObject/JsonContentObjectTest.php @@ -181,11 +181,13 @@ public static function dataProvider(): array [['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']])], ]; } }