Action to alter webform element

Created on 7 June 2025, about 1 month ago

Problem/Motivation

ECA webform already exposes the alter element event in ECA, but as far as I know, it is not yet possible to set the element properties through any ECA action since the respective tokens are read-only.

My use case is to dynamically update the cardinality of a webform element based on a field value in the webform submission's source entity.
Without using ECA, doing so involves:

  1. Implementing hook_webform_element_alter()
  2. Checking if the element is a multi-value input field by checking the value of $element['#multiple']
  3. Loading source entity through $form_state->getFormObject()->getSourceEntity()
  4. Load field value of source entity
  5. Setting the value of $element['#min_value'] to the field value
  6. Set $element['#add'], $element['#remove'], and $element['#add_more'] to FALSE

Currently, step 1 is already implemented in ECA webform, step 4 is possible through ECA content. Step 2 may be possible as well by just using ECA base and checking token values. Steps 3, 5, and 6 are not possible to perform.

Steps to reproduce

Proposed resolution

  • Add a new action to load source entity OR add source entity to event tokens
  • Add an action to use YAML key-value pairs to set $element properties.

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

2.1

Component

Code

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

Comments & Activities

  • Issue created by @starlight-sparkle
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Have you tried using the Build form event to alter the form like any other form in Drupal?

  • That event would work as well, but that doesn't change the fact that there are no actions to make the alterations I require; it seems webform is using some webform-specific element attributes that are not present in Drupal Core form elements.

    Additionally, the ECA Form actions seem to be limited to changing the disabled state, accessibility, weight, options and label of the field; there is no way to alter other element attributes. There are also no actions that would allow ephemerally altering the widget or field settings ahead of the form build.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    That's correct @starlight-sparkle. When providing new action plugins to allow for more options, then we should always carefully determine whether this would be Webform-specific or maybe generic. For the latter, enhancements should go into the eca_form submodule, others would be best in here.

    Contributions are very welcome. I'll be helping with questions and reviewing MRs, I just don't have the resources available to build such features myself in the short and mid term.

Production build 0.71.5 2024