diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e257c9a1..759174a4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Added optional password/database attributes for redis in `system.yaml` * Added ability to filter enabled or disabled with bin/gpm index [#3187](https://github.com/getgrav/grav/pull/3187) * Added `$grav->getVersion()` or `grav.version` in twig to get the current Grav version [#3142](https://github.com/getgrav/grav/issues/3142) + * Added second parameter to `$blueprint->flattenData()` to include every field, including those which have no data 1. [](#bugfix) * Fixed CLI progressbar in `backup` and `security` commands to use styled output [#3198](https://github.com/getgrav/grav/issues/3198) * Fixed page save failing because of uploaded images [#3191](https://github.com/getgrav/grav/issues/3191) diff --git a/system/src/Grav/Common/Data/Blueprint.php b/system/src/Grav/Common/Data/Blueprint.php index 57351a7bd..25c4266e9 100644 --- a/system/src/Grav/Common/Data/Blueprint.php +++ b/system/src/Grav/Common/Data/Blueprint.php @@ -294,13 +294,14 @@ class Blueprint extends BlueprintForm * Flatten data by using blueprints. * * @param array $data + * @param bool $includeAll * @return array */ - public function flattenData(array $data) + public function flattenData(array $data, bool $includeAll = false) { $this->initInternals(); - return $this->blueprintSchema->flattenData($data); + return $this->blueprintSchema->flattenData($data, $includeAll); } diff --git a/system/src/Grav/Common/Data/BlueprintSchema.php b/system/src/Grav/Common/Data/BlueprintSchema.php index 544d42025..f082f9e0b 100644 --- a/system/src/Grav/Common/Data/BlueprintSchema.php +++ b/system/src/Grav/Common/Data/BlueprintSchema.php @@ -107,11 +107,22 @@ class BlueprintSchema extends BlueprintSchemaBase implements ExportInterface * Flatten data by using blueprints. * * @param array $data Data to be flattened. + * @param bool $includeAll * @return array */ - public function flattenData(array $data) + public function flattenData(array $data, bool $includeAll = false) { - return $this->flattenArray($data, $this->nested, ''); + $list = []; + if ($includeAll) { + foreach ($this->items as $key => $rules) { + $type = $rules['type'] ?? ''; + if (!str_starts_with($type, '_')) { + $list[$key] = null; + } + } + } + + return array_replace($list, $this->flattenArray($data, $this->nested, '')); } /** @@ -139,6 +150,7 @@ class BlueprintSchema extends BlueprintSchemaBase implements ExportInterface $array[$prefix.$key] = $field; } } + return $array; }