- Issue created by @jurgenhaas
- Merge request !497Issue #3519886 by jurgenhaas: ECA Form: set field value in ajax calls → (Closed) created by jurgenhaas
- 🇩🇪Germany mxh Offenburg
The action is called "Form field: set default value" and is not supposed to overwrite an already submitted value. We're using this action a lot and changing it in the suggested way will for sure break our existing configurations.
When you want to manipulate from what was submitted, then "Form field: set submitted value" is more likely the proper action you're looking for. However it might need to be executed in the right form event before values are being set in the form elements by the Form API.
- 🇩🇪Germany jurgenhaas Gottmadingen
Thanks @mxh, I was actually expecting some feedback like this. I really got confused last week when I ran into this, which, I thought, should not even be an issue. But I was unable to resolve it. I also tried the set submitted value action, but couldn't find the right event for it to do what I was looking for.
So, maybe you can help me out? The use case is simple: a node edit form where we add an ajax handler to the title field. And if that ajax handler triggers, we need to set the value of another field to something.
It sounds too simple to be not possible, but I failed. Let's see if there is a solution even without any code changes.
- 🇩🇪Germany mxh Offenburg
I also tried the set submitted value action, but couldn't find the right event for it to do what I was looking for.
You're right, it doesn't seem to work for the rebuilt form to show the newly set value. So we might need to look for a way to provide a more convenient way to achieve it.
- 🇩🇪Germany jurgenhaas Gottmadingen
I'm glad you can reproduce this :-) and yes, if there is a more direct way of doing this, I'm all for it.
What does work during the form build event is to use
$element['#value'] = $value;
So, it's probably just a question on where to put this code.
We do have the plugins
eca_form_field_set_value
andeca_form_field_get_value
, but they operate on the form submission, not during form build.However, we could probably check on the event type for which this action is being used, and then either go for build or for submission functionality. The build functionality could be derived from the
eca_form_field_default_value
plugin with the different of default value replaced by value.@mxh would that be an acceptable approach from your point of view?
- 🇩🇪Germany mxh Offenburg
Yes, I think we can extend
eca_form_field_set_value
that we change the '#value' (similar logic as theeca_form_field_default_value
action) of the target$element
. Meaning when$element = &$this->getTargetElement()
returns a non-empty array. Maybe not even necessary to check the source event, rather just set the key on the$element
if it's available might be the most flexible option. - 🇩🇪Germany jurgenhaas Gottmadingen
jurgenhaas → changed the visibility of the branch 3519886-eca-form-set to hidden.
- Merge request !504Issue #3519886 by jurgenhaas, mxh: ECA Form: provide a way to set the form... → (Open) created by jurgenhaas
- 🇩🇪Germany jurgenhaas Gottmadingen
Created a new MR with the new approach. It does what we're expecting, it's just the action label "Form field: set submitted value" which may be misleading. The action ID
eca_form_field_set_value
is better, so I've updated the label accordingly.