From bd021af7d11896a40a029f6f12645f536cd6fa0b Mon Sep 17 00:00:00 2001 From: Benni Mack Date: Mon, 2 Oct 2023 13:57:18 +0200 Subject: [PATCH] [FEATURE] Add option "ifEmptyUnsetKey" to JSON cObject (#650) This TypoScript option works similar to "ifEmptyReturnNull", however, the option uses the unset() feature to actually remove the key from the json output instead of setting this to null. The code checks for "" and an explicit "false" (bool) when using the cObject = BOOL Fixes: #649 --- Classes/ContentObject/JsonContentObject.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php index 4d1bfd78..1780c900 100755 --- a/Classes/ContentObject/JsonContentObject.php +++ b/Classes/ContentObject/JsonContentObject.php @@ -121,6 +121,9 @@ public function cObjGet(array $setup, string $addKey = ''): array if ((int)($conf['ifEmptyReturnNull'] ?? 0) === 1 && $content[$theKey] === '') { $content[$theKey] = null; } + if ((int)($conf['ifEmptyUnsetKey'] ?? 0) === 1 && ($content[$theKey] === '' || $content[$theKey] === false)) { + unset($content[$theKey]); + } if (!empty($contentDataProcessing['dataProcessing.'])) { $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($contentDataProcessing); }