If an array field has ignore_empty: true, only save options with a value

This commit is contained in:
Flavio Copes
2016-11-28 18:59:41 +01:00
parent f6ddba52d8
commit 3838de1d97
2 changed files with 15 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
1. [](#improved)
* Added alias `selfupdate` to the `self-upgrade` `bin/gpm` CLI command
* Add `ignore_empty` property to be used on array fields, if positive only save options with a value
# v1.1.9-rc.2
## 11/26/2016

View File

@@ -9,6 +9,7 @@
namespace Grav\Common\Data;
use Grav\Common\Grav;
use Grav\Common\Utils;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
@@ -569,6 +570,7 @@ class Validation
return null;
}
if ($options) {
$useKey = isset($field['use']) && $field['use'] == 'keys';
foreach ($values as $key => $value) {
@@ -586,6 +588,18 @@ class Validation
}
}
if (isset($field['ignore_empty']) && Utils::isPositive($field['ignore_empty'])) {
foreach ($values as $key => $value) {
foreach ($value as $inner_key => $inner_value) {
if ($inner_value == '') {
unset($value[$inner_key]);
}
}
$values[$key] = $value;
}
}
return $values;
}