Skip to content

Commit 9fd4a92

Browse files
committed
Allow subform to return an indexed array
1 parent 7fc5fdf commit 9fd4a92

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

libraries/src/Form/Field/SubformField.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @Example with all attributes:
2525
* <field name="field-name" type="subform"
26-
* formsource="path/to/form.xml" min="1" max="3" multiple="true" buttons="add,remove,move"
26+
* formsource="path/to/form.xml" min="1" max="3" multiple="true" buttons="add,remove,move" indexed="true"
2727
* layout="joomla.form.field.subform.repeatable-table" groupByFieldset="false" component="com_example" client="site"
2828
* label="Field Label" description="Field Description" />
2929
*
@@ -73,6 +73,12 @@ class SubformField extends FormField
7373
*/
7474
protected $buttons = ['add' => true, 'remove' => true, 'move' => true];
7575

76+
/**
77+
* Remove key from array
78+
* @var bool
79+
*/
80+
protected $indexed = false;
81+
7682
/**
7783
* Method to get certain otherwise inaccessible properties from the form field object.
7884
*
@@ -198,7 +204,7 @@ public function setup(\SimpleXMLElement $element, $value, $group = null)
198204
return false;
199205
}
200206

201-
foreach (['formsource', 'min', 'max', 'layout', 'groupByFieldset', 'buttons'] as $attributeName) {
207+
foreach (['formsource', 'min', 'max', 'layout', 'groupByFieldset', 'buttons', 'indexed'] as $attributeName) {
202208
$this->__set($attributeName, $element[$attributeName]);
203209
}
204210

@@ -448,4 +454,44 @@ public function filter($value, $group = null, ?Registry $input = null)
448454

449455
return $return;
450456
}
457+
458+
/**
459+
* Method to post-process a field value.
460+
*
461+
* @param mixed $value The optional value to use as the default for the field.
462+
* @param string $group The optional dot-separated form group path on which to find the field.
463+
* @param ?Registry $input An optional Registry object with the entire data set to filter
464+
* against the entire form.
465+
*
466+
* @return mixed The processed value.
467+
*
468+
* @since 4.0.0
469+
*/
470+
public function postProcess($value, $group = null, ?Registry $input = null)
471+
{
472+
if ($value && $this->indexed && $this->multiple) {
473+
$value = array_values((array)$value);
474+
}
475+
476+
if ($value) {
477+
$this->value = $value;
478+
479+
// We set min to 0 here to avoid issues with postProcess when there are less items than min
480+
$this->min = 0;
481+
482+
$tmpl = $this->loadSubForm();
483+
$forms = $this->loadSubFormData($tmpl);
484+
485+
// We use the original keys from the value to keep any associative array keys
486+
$index = 0;
487+
foreach ($this->value as $k => $v) {
488+
$value[$k] = $forms[$index]->postProcess($v);
489+
$index++;
490+
}
491+
492+
return $value;
493+
}
494+
495+
return parent::postProcess($value, $group, $input);
496+
}
451497
}

0 commit comments

Comments
 (0)