Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions libraries/src/Form/Field/SubformField.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @Example with all attributes:
* <field name="field-name" type="subform"
* formsource="path/to/form.xml" min="1" max="3" multiple="true" buttons="add,remove,move"
* formsource="path/to/form.xml" min="1" max="3" multiple="true" buttons="add,remove,move" indexed="true"
* layout="joomla.form.field.subform.repeatable-table" groupByFieldset="false" component="com_example" client="site"
* label="Field Label" description="Field Description" />
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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]);
}

Expand Down Expand Up @@ -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);
}
}
Loading