Tag form fields that support tokens

Created on 1 September 2022, about 2 years ago
Updated 2 June 2024, 6 months ago

Problem/Motivation

The plugin configuration forms contain fields, some of which support tokens, others don't. And those who do support tokens come in two different flavours: some ask for a token name and others support token syntax such that [token:syntax] gets replaced with values.

If we were to tag each field that supported one of those 2 features, we could use those tags for other helpful things:

  • The modeller would only show the link to the token browser for fields that actually do support them
  • The field descriptions for all plugins could be automatically adjusted by ECA code to indicate if a field supported tokens

Currently, we do have some field descriptions with the token support text. But not all of them, and not with a consistent message. With tags in place, we could make sure to add a consistent indicator everywhere.

Proposed resolution

Let's add 2 more properties to all ECA plugin fields:

  • #eca_token_replacement
  • #eca_token_reference

They both default to FALSE and should be omitted for all fields, where they are not relevant.

The property #eca_token_replacement should be set to TRUE for fields that support token replacements, i.e. where tokens can be used with brackets, either just with their name or with more complex syntax to reference a nested property.

The property #eca_token_reference should be set to TRUE for field, where the token name is expected. This input then has to happen without brackets and not other text is allowed either.

In addition to that, we should remove all field descriptions that refer to tokens in either of the 2 cases. Instead, the module will add common text to the description to uniquely explain how tokens can or need to be used for each of the cases. And also, if the documentation URL is configured globally, those descriptions can link to the ECA Guide section, where tokens are documented extensively.

Feature request
Status

Fixed

Version

2.0

Component

Miscellaneous

Created by

🇩🇪Germany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
  • Accessibility

    It affects the ability of people with disabilities or special needs (such as blindness or color-blindness) to use Drupal.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States johnpicozzi Providence, RI

    As part of this issue resolution I would suggest also adding a link to those fields to open the token browser. This will allow easy access to the token list and allow people to populate the field without searching for tokens.

  • 🇺🇸United States johnpicozzi Providence, RI

    I'm now seeing that after installing the Token module a link is added to the top of the ECA window to the Token Browser. I would suggest moving that link to the fields that support tokens and maybe (if possible) filtering the token list to what's allowed.

  • Assigned to jurgenhaas
  • 🇩🇪Germany jurgenhaas Gottmadingen

    The second part with the #eca_token_reference form field property has just been implemented in Add token syntax validation where possible Fixed together with the form validation, that a token name comes without the brackets around it.

    I'm now seeing that after installing the Token module a link is added to the top of the ECA window to the Token Browser. I would suggest moving that link to the fields that support tokens

    Isn't that overwhelming the UI? I mean, with the new property #eca_token_replacement, this would technically be pretty easy, but I'm a bit uncertain if half a dozen similar links in a config form would actually make sense.

    and maybe (if possible) filtering the token list to what's allowed.

    That's pretty difficult, if not impossible. While building a model, it's not always known what tokens will be available when processing it. E.g. when starting with a Pre-save content entity event, it may be unknown which entity type will be the token entity. Or, if the model works with conditions, some tokens are only present if certain conditions are true.

  • 🇩🇪Germany jurgenhaas Gottmadingen
  • 🇩🇪Germany jurgenhaas Gottmadingen
    • jurgenhaas committed 2249f61b on 2.0.x
      Issue #3307337 by jurgenhaas, johnpicozzi, mxh: Tag form fields that...
    • jurgenhaas committed c79374b5 on 2.0.x
      Issue #3307337 by jurgenhaas, johnpicozzi, mxh: Tag form fields that...
  • Issue was unassigned.
  • 🇩🇪Germany jurgenhaas Gottmadingen

    All configuration forms are now properly tagged so that we can identify all fields that either reference a token or those that support token replacements.

    All those fields get identical description texts, which is done in \Drupal\eca\Plugin\ECA\PluginFormTrait::updateConfigurationForm. There we can later add some extras, that have been suggested above.

    Note, this only works for plugins where the maintainer provides the tags. We should encourage them to do that, at least there is a change record where they get informed about this: [#3381190]

  • 🇩🇪Germany mxh Offenburg

    You might already know that since this issue is not yet fixed, but anyway I've seen that currently most of the tagged form fields do not show the additional descriptions that are implemented in \Drupal\eca\Plugin\ECA\PluginFormTrait::updateConfigurationForm.

    I've tried the condition plugin eca_current_user_id and action plugin eca_token_set_value that both have one of the newly introduced tags, but they're not showing the additional description:

    Besides that, the currently implemented loop in \Drupal\eca\Plugin\ECA\PluginFormTrait::updateConfigurationForm is problematic:

    <?php
    protected function updateConfigurationForm(array $form): array {
        $containsTokenReplacement = FALSE;
        foreach ($form as &$value) {
          if (!empty($value['#eca_token_replacement'])) {
            //...
          }
        }
        unset($value);
    ?>
    

    The problem above is that the foreach statement iterates through a renderable array, which may or may not contain arrays as $value. One proper way to iterate through its children is the following:

    <?php
    foreach (\Drupal\Core\Render\Element::children($form) as $child_key) {
      $value = &$form[$child_key];
      // ...       
    }
    unset($value);
    ?>
    

    That loop needs to get fixed, so that we are able to pass through renderable arrays that already contain render information. Otherwise we may get fatal errors as !empty($value['#eca_token_replacement']) currently always tries to access $value as an array.

    • jurgenhaas committed 44827dd6 on 2.0.x
      Issue #3307337 by mxh, jurgenhaas, johnpicozzi: Tag form fields that...
    • jurgenhaas committed 3480ead1 on 2.0.x
      Issue #3307337 by mxh, jurgenhaas, johnpicozzi: Tag form fields that...
    • jurgenhaas committed cd3a5408 on 2.0.x
      Issue #3307337 by mxh, jurgenhaas, johnpicozzi: Tag form fields that...
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thanks @mxh for pointing out these 2 issues. I actually didn't realize yet, that some plugins were missing the extra token info. The reason for that is, that we didn't follow the same sequence of form building everywhere. What we have almost everywhere is to define the plugin specific form elements first, and then call the parent form builder:

      public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
        $form['user_id'] = [
          '#type' => 'textfield',
          '#title' => $this->t('User ID'),
          '#description' => $this->t('The user ID, which gets compared.'),
          '#default_value' => $this->configuration['user_id'],
          '#weight' => -10,
          '#eca_token_replacement' => TRUE,
        ];
        return parent::buildConfigurationForm($form, $form_state);
      }
    

    The parent form builder is adding the extra and common pieces, like the token explanation. When that parent form builder is called first, then the extra fields don't exist yet, when adding the common pieces.

    I've updated all form builders to follow the same sequence and started a change records for this in https://www.drupal.org/node/3427033

    Also thanks for the correction of the loop with render elements. I've fixed that too. But we need to keep in mind, that especially the bpmn_io modeller won't show any of the markup in a form whatsoever. That's something we can hopefully add at some point in the future there.

  • 🇩🇪Germany jurgenhaas Gottmadingen
    • jurgenhaas committed 8db81fa6 on 2.0.x
      Issue #3307337 by mxh, jurgenhaas, johnpicozzi: Tag form fields that...
  • 🇩🇪Germany jurgenhaas Gottmadingen

    I've now added some markup to the config form, when at least one fields supports tokens:

    This template supports the usage of tokens. Please refer to the token chapter in the ECA Guide, and if you install and enable the token module, you will find a link to the token browser right below the property panel, which shows you all available tokens.

    For the bpmn_io modeller, this is displayed in the template tab, as this is currently the only place, where we can add extra info. If we get better options from the upstream library, we can easily move that there and then hopefully add some markup with line breaks and links.

  • Status changed to Fixed 6 months ago
    • jurgenhaas committed bb7bd0d3 on 2.0.x
      Issue #3307337 by mxh, jurgenhaas, johnpicozzi: Tag form fields that...
    • jurgenhaas committed a0627712 on 2.0.x
      Issue #3307337 by mxh, jurgenhaas, johnpicozzi: Tag form fields that...
    • jurgenhaas committed 82342a3a on 2.0.x
      Issue #3307337 by mxh, jurgenhaas, johnpicozzi: Tag form fields that...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024