|
23 | 23 | *
|
24 | 24 | * @Example with all attributes:
|
25 | 25 | * <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" |
27 | 27 | * layout="joomla.form.field.subform.repeatable-table" groupByFieldset="false" component="com_example" client="site"
|
28 | 28 | * label="Field Label" description="Field Description" />
|
29 | 29 | *
|
@@ -73,6 +73,12 @@ class SubformField extends FormField
|
73 | 73 | */
|
74 | 74 | protected $buttons = ['add' => true, 'remove' => true, 'move' => true];
|
75 | 75 |
|
| 76 | + /** |
| 77 | + * Remove key from array |
| 78 | + * @var bool |
| 79 | + */ |
| 80 | + protected $indexed = false; |
| 81 | + |
76 | 82 | /**
|
77 | 83 | * Method to get certain otherwise inaccessible properties from the form field object.
|
78 | 84 | *
|
@@ -198,7 +204,7 @@ public function setup(\SimpleXMLElement $element, $value, $group = null)
|
198 | 204 | return false;
|
199 | 205 | }
|
200 | 206 |
|
201 |
| - foreach (['formsource', 'min', 'max', 'layout', 'groupByFieldset', 'buttons'] as $attributeName) { |
| 207 | + foreach (['formsource', 'min', 'max', 'layout', 'groupByFieldset', 'buttons', 'indexed'] as $attributeName) { |
202 | 208 | $this->__set($attributeName, $element[$attributeName]);
|
203 | 209 | }
|
204 | 210 |
|
@@ -448,4 +454,44 @@ public function filter($value, $group = null, ?Registry $input = null)
|
448 | 454 |
|
449 | 455 | return $return;
|
450 | 456 | }
|
| 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 | + } |
451 | 497 | }
|
0 commit comments