diff --git a/CHANGELOG.md b/CHANGELOG.md index 2636ba949..c15bb8d0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Fixed new `Flex Users` being stored with wrong filename, login issues [#2785](https://github.com/getgrav/grav/issues/2785) * Fixed `ignore_empty: true` not removing empty values in blueprint filtering * Fixed `{{ false|string }}` twig to return '0' instead of '' + * Fixed `Data::filter()` removing empty fields (such as empty list) by default * Grav 1.7: Fixed `Flex Pages` unserialize issues if Flex-Objects Plugin has not been installed * Grav 1.7: Require Flex-Objects Plugin to edit Flex Accounts diff --git a/system/src/Grav/Common/Data/Data.php b/system/src/Grav/Common/Data/Data.php index 18a19aa62..d70360fb5 100644 --- a/system/src/Grav/Common/Data/Data.php +++ b/system/src/Grav/Common/Data/Data.php @@ -21,16 +21,18 @@ class Data implements DataInterface, \ArrayAccess, \Countable, \JsonSerializable /** @var string */ protected $gettersVariable = 'items'; - /** @var array */ protected $items; - /** @var Blueprint|null */ protected $blueprints; - /** @var FileInterface|null */ protected $storage; + /** @var bool */ + private $missingValuesAsNull = false; + /** @var bool */ + private $keepEmptyValues = true; + /** * @param array $items * @param Blueprint|callable|null $blueprints @@ -41,6 +43,28 @@ class Data implements DataInterface, \ArrayAccess, \Countable, \JsonSerializable $this->blueprints = $blueprints; } + /** + * @param bool $value + * @return $this + */ + public function setKeepEmptyValues(bool $value) + { + $this->keepEmptyValues = $value; + + return $this; + } + + /** + * @param bool $value + * @return $this + */ + public function setMissingValuesAsNull(bool $value) + { + $this->missingValuesAsNull = $value; + + return $this; + } + /** * Get value by using dot notation for nested arrays/objects. * @@ -201,8 +225,8 @@ class Data implements DataInterface, \ArrayAccess, \Countable, \JsonSerializable public function filter() { $args = func_get_args(); - $missingValuesAsNull = (bool)(array_shift($args) ?: false); - $keepEmptyValues = (bool)(array_shift($args) ?: false); + $missingValuesAsNull = (bool)(array_shift($args) ?? $this->missingValuesAsNull); + $keepEmptyValues = (bool)(array_shift($args) ?? $this->keepEmptyValues); $this->items = $this->blueprints()->filter($this->items, $missingValuesAsNull, $keepEmptyValues);