"Defined by token" does not work for derived actions with ":" within plugin ID

Created on 15 July 2025, 18 days ago

Problem/Motivation

When I try to use "Defined by token" option for the "Format" configuration in the "Render: Serialize" action, it always defaults to "json" when the action is executed.

Debugging reveals that this is because when the token named "eca_render_serialize:serialization_format" is retrieved, it actually retrieves a \Drupal\Core\TypedData\Plugin\DataType\StringData object instead of a scalar string. Hence when the action calls getTokenValue():

  protected function getTokenValue(string $fieldKey, string $default): string {
    $value = $this->tokenService->getTokenData($this->buildTokenName($fieldKey));
    if ($value instanceof DataTransferObject) {
      $value = $value->getValue();
    }
    // StringData is not scalar, and it is not instance of DataTransferObject, so $default is returned.
    return is_scalar($value) ? (string) $value : $default;
  }

Steps to reproduce

  1. Use Drupal 10.5.1
  2. Enable ECA render, ECA base, and core serialization module
  3. Create a model
    1. ECA custom event (Event ID: test_token)
    2. Token: set value (Name: eca_render_serialize:serialization_format Value: xml)
    3. Render: Serialize (Format: Defined by token Token name: serialized Value: test: "yes" Interpret as YAML: Yes)
    4. Display a message to the user (Message: [serialized])
  4. Run terminal command drush eca:trigger:custom_event test_token
  5. Observe that JSON output is printed instead of XML

Proposed resolution

Add an additional check for TypedDataInterface in getTokenValue() and if so, try to call getValue() on the object.

  protected function getTokenValue(string $fieldKey, string $default): string {
    $value = $this->tokenService->getTokenData($this->buildTokenName($fieldKey));
    if ($value instanceof DataTransferObject) {
      $value = $value->getValue();
    }
    if ($value instanceof TypedDataInterface) {
      $value = $value->getValue();
    }
    return is_scalar($value) ? (string) $value : $default;
  }

Remaining tasks

User interface changes

No changes

API changes

No changes

Data model changes

🐛 Bug report
Status

Active

Version

2.1

Component

Code

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

Comments & Activities

Production build 0.71.5 2024