Added second parameter to $blueprint->flattenData() to include every field, including those which have no data

This commit is contained in:
Matias Griese
2021-02-08 21:27:47 +02:00
parent 6b25a9f00c
commit 3ee9e65f2d
3 changed files with 18 additions and 4 deletions

View File

@@ -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)

View File

@@ -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);
}

View File

@@ -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;
}