AJAX Handler syntax

Created on 3 June 2023, over 1 year ago

I'm looking for some help understanding the correct way to set up an Ajax Handler to refresh a form field when another field has been changed.

I have contact form fields set up in Views using as entity form fields. Field 1 is the parent with a radio field list of options. Field 2 has a backend dependency on Field 1 values. The View form is ajax enabled, so if I change Field 1, it auto updates in the form. If I refresh the page, Field 2 updates to reflect the Field 1 value change, so I know that's all working fine.

I'm trying to create an ECA model so the form triggered when Field 1 is updated and autosaved. I've tried this 2 different ways, neither working.

Attempt 1:

StartEvent: Update Content Entity
Condition: Entity Field Value Changed
Ajax Handler:
-field name = input name from parent field = form_field_field_parent1[0][field_parent1]
-validate form fields = parent and child field machine names = field_parent1,field_child2
-target = child field machine name = field_child2

When that didn't work (child field does not update in form), I thought perhaps I had to load the form and use a trigger, so...

Attempt 2:

StartEvent: Update Content Entity
Condition: Entity Field Value Changed
Load: current scope, contact message
Trigger: entity aware custom event

StartEvent: entity aware custom event
Ajax Handler: config as above.

I'd appreciate some help understanding how to configure this correctly.

💬 Support request
Status

Active

Version

1.1

Component

Documentation

Created by

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

Comments & Activities

  • Issue created by @tonka67
  • 🇩🇪Germany jurgenhaas Gottmadingen

    When you say "contact form fields set up in Views", what exactly does that mean? Views is about displaying data, not really to build forms.

    And in general, ECA is a processor to work on the backend. When your request is about dependant fields in the frontend, that's not really what ECA is built for. Such conditions are best addressed with javascript solutions. One way of getting javascript in the browser to change field values or visibility depending on changed values of other fields, is the Drupal state feature of the form API. You would have to add all the necessary state properties to the form with the Build form event.

    However, if that's your requirement, you may consider looking into the conditional fields module, as this is doing that task just perfectly.

  • Status changed to Postponed: needs info over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • I'm not looking to affect visibility. Rather, I have one field whose display values are dependent on another. For examples:

    Field 1 (Categories): Cars / Apples /Horses
    Field 2 (Items): Honda / Toyota / Granny Smith /Fuji / Arabian / Pony

    When Field 1 is set to a category, only the items relating to that category are displayed.

    Business Rules does this, but it's already several years old and doesn't work properly. Dependent fields only works with select fields, which isn't useful.

    I was hoping there might be some way to accomplish this in ECA instead. For example, when Field 1 is changed, send the new value as an argument to the entity reference field in Field 2, then refresh the view to get the updated value?

  • Status changed to Active over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    I don't think, ECA can already do that. It may be possible to add something like that, and I'm changing this to a feature request, in case anybody were to pick this up and wanted to prove an MR.

  • Thank you. I'm happy to assist with testing.

  • 🇩🇪Germany jurgenhaas Gottmadingen
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • Status changed to Postponed: needs info 26 days ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    @tonka67 we have now all ajax commands from Drupal core implemented in the eca_endpoint sub-module. We've used that ourselves to build intensive interaction between frontend and backend, with great success. Do you want to give this a try too?

  • Thanks @jurgenhaas. I'll try to find some time to play around with it in the next week or so.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Marking as fixed, please open a new issue if anything is missing.

  • 🇩🇪Germany jurgenhaas Gottmadingen
Production build 0.71.5 2024