diff --git a/libraries/src/Form/Field/SubformField.php b/libraries/src/Form/Field/SubformField.php index cd2213e647718..50e0e79db4541 100644 --- a/libraries/src/Form/Field/SubformField.php +++ b/libraries/src/Form/Field/SubformField.php @@ -23,7 +23,7 @@ * * @Example with all attributes: * * @@ -73,6 +73,12 @@ class SubformField extends FormField */ protected $buttons = ['add' => true, 'remove' => true, 'move' => true]; + /** + * Remove key from array + * @var bool + */ + protected $indexed = false; + /** * Method to get certain otherwise inaccessible properties from the form field object. * @@ -198,7 +204,7 @@ public function setup(\SimpleXMLElement $element, $value, $group = null) return false; } - foreach (['formsource', 'min', 'max', 'layout', 'groupByFieldset', 'buttons'] as $attributeName) { + foreach (['formsource', 'min', 'max', 'layout', 'groupByFieldset', 'buttons', 'indexed'] as $attributeName) { $this->__set($attributeName, $element[$attributeName]); } @@ -448,4 +454,44 @@ public function filter($value, $group = null, ?Registry $input = null) return $return; } + + /** + * Method to post-process a field value. + * + * @param mixed $value The optional value to use as the default for the field. + * @param string $group The optional dot-separated form group path on which to find the field. + * @param ?Registry $input An optional Registry object with the entire data set to filter + * against the entire form. + * + * @return mixed The processed value. + * + * @since 4.0.0 + */ + public function postProcess($value, $group = null, ?Registry $input = null) + { + if ($value && $this->indexed && $this->multiple) { + $value = array_values((array)$value); + } + + if ($value) { + $this->value = $value; + + // We set min to 0 here to avoid issues with postProcess when there are less items than min + $this->min = 0; + + $tmpl = $this->loadSubForm(); + $forms = $this->loadSubFormData($tmpl); + + // We use the original keys from the value to keep any associative array keys + $index = 0; + foreach ($this->value as $k => $v) { + $value[$k] = $forms[$index]->postProcess($v); + $index++; + } + + return $value; + } + + return parent::postProcess($value, $group, $input); + } }