Skip to content
Open
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
28 changes: 16 additions & 12 deletions Classes/ContentObject/JsonContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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];
Expand All @@ -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
);
}
}
}
Expand All @@ -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,
Expand All @@ -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;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Tests/Unit/ContentObject/JsonContentObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<<<!--INT_SCRIPT.202cb962ac59075b964b07152d234b70-->>>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']])],
];
}
}
Loading