In a onContentPrepareForm event, it is not possible to modify the form of a subform field for existing values. I have a simplified code like:
public function onContentPrepareForm(Form $form, $data): bool
{
$field = $form->getField('directories', 'params');
if ($field instanceof SubformField) {
$field->loadSubForm()->load('<fields name="form"><fieldset name="basic"><field name="test_field" type="text" label="test label"/></fieldset></fields>');
}
return true;
}
If you put this code into a content plugin and open the local adapter plugin in the back end, then the text field is not displayed for the existing images and files entries. If you click on the "+" icon, then you will see that the last column in the table gets a text input field.
6.0
Modifying a subform from a field, should also reflect the changes for existing values.
It does not.
No response
@Fedik can shed maybe some light into it
| Labels |
Added:
No Code Attached Yet
bug
|
||
Problem is that while modifying the form in the onContentPrepareForm event, no values are set. So I can't modify the different forms there. Looks for me like a bug then, when I modify the subform at that point and it doesn't get propagated when the value instances are created.
In that case you can try set/modify "formsource" attribute for the subform field.
The whole thing is very buggy as I had to bind the data to the form in onContentPrepareForm to get all subfields for the values. In onExtensionBeforeSave i had to set the data from input, bypassing the whole validation. Not sure if this should be that broken.
Modify the formsource of the subform field in onContentPrepareForm, it is only reliable way to achieve what you want.
There still a few caveats, but in general should work much better than loadSubForm() or use of Form::getInstance();
In the future we need to look how to improve that.
And I think for nested subforms will not be possible to support such modifications.
It were not planed to work :)
That would be feature request.
However there a few issue already opened:
As workaround you basically have to modify the form for each row, additionally to what you already tried.