Skip to content

Commit

Permalink
[FEATURE] Add nullableFieldsIfEmpty option to JsonContentObject
Browse files Browse the repository at this point in the history
This new option adds ability to define which fields
should be set to null when parsed TypoScript returns empty string
  • Loading branch information
twoldanski committed Jul 11, 2024
1 parent 44ffec9 commit c24c470
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Classes/ContentObject/JsonContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function render($conf = []): string
public function cObjGet(array $setup, string $addKey = ''): array
{
$content = [];
$nullableFieldsIfEmpty = GeneralUtility::trimExplode(',', $this->conf['nullableFieldsIfEmpty'] ?? '', true);

$sKeyArray = $this->filterByStringKeys($setup);
foreach ($sKeyArray as $theKey) {
Expand All @@ -117,7 +118,7 @@ public function cObjGet(array $setup, string $addKey = ''): array
if ($theValue === 'USER_INT' || str_starts_with((string)$content[$theKey], '<!--INT_SCRIPT.')) {
$content[$theKey] = $this->headlessUserInt->wrap($content[$theKey], (int)($conf['ifEmptyReturnNull'] ?? 0) === 1 ? HeadlessUserInt::STANDARD_NULLABLE : HeadlessUserInt::STANDARD);
}
if ((int)($conf['ifEmptyReturnNull'] ?? 0) === 1 && $content[$theKey] === '') {
if ($content[$theKey] === '' && ((int)($conf['ifEmptyReturnNull'] ?? 0) === 1 || in_array($theKey, $nullableFieldsIfEmpty, true))) {
$content[$theKey] = null;
}
if ((int)($conf['ifEmptyUnsetKey'] ?? 0) === 1 && ($content[$theKey] === '' || $content[$theKey] === false)) {
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/ContentObject/JsonContentObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function dataProvider(): array
[[], '[]'],
[null, '[]'],
[['fields.' => ['test' => 'TEXT', 'test.' => ['value' => '', 'ifEmptyUnsetKey' => 1]]], json_encode([])],
[['nullableFieldsIfEmpty' => 'test,test3', 'fields.' => ['test' => 'TEXT', 'test.' => ['value' => ''], 'test2' => 'TEXT', 'test2.' => ['value' => '1'], 'test3' => 'TEXT', 'test3.' => ['value' => '']]], json_encode(['test' => null, 'test2' => '1', 'test3' => null])],
[['fields.' => ['test' => 'INT', 'test.' => ['value' => '1', 'ifEmptyUnsetKey' => 1]]], json_encode(['test' => 1])],
[['if.' => ['isTrue' => 0], 'fields.' => ['test' => 'TEXT', 'test.' => ['value' => '1']]], ''],
[['if.' => ['isTrue' => 1], 'fields.' => ['test' => 'TEXT', 'test.' => ['value' => '1']]], json_encode(['test' => '1'])],
Expand Down

0 comments on commit c24c470

Please sign in to comment.