ECA Form: set field value in ajax calls

Created on 18 April 2025, 25 days ago

Problem/Motivation

Use case: the user has a form on screen, and one of the existing text fields should receive a different value as a result of an ajax request triggered on the current page, this currently can't be done. The "Set default value" action doesn't work as it sets the #default_value which has no effect when the form is rebuilt rather than build for the first time.

Proposed resolution

In this scenario, we rather need to set the #value property. We could switch to that property key when the form is rebuilding.

Feature request
Status

Active

Version

2.1

Component

Code

Created by

🇩🇪Germany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @jurgenhaas
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • Pipeline finished with Success
    25 days ago
    Total: 437s
    #476572
  • 🇩🇪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 and eca_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 the eca_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.

  • Pipeline finished with Canceled
    17 days ago
    Total: 87s
    #482569
  • 🇩🇪Germany jurgenhaas Gottmadingen

    jurgenhaas changed the visibility of the branch 3519886-eca-form-set to hidden.

  • 🇩🇪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.

  • Pipeline finished with Success
    17 days ago
    Total: 507s
    #482570
  • Pipeline finished with Canceled
    17 days ago
    Total: 271s
    #482572
  • Pipeline finished with Success
    17 days ago
    Total: 524s
    #482578
  • Pipeline finished with Success
    7 days ago
    Total: 505s
    #490430
Production build 0.71.5 2024