token_or module fails without explicit 'clear' option in token service replacement

Created on 16 April 2025, 3 months ago

Steps to reproduce

Execute the following code without specifying the clear option:

$node = \Drupal\node\Entity\Node::load($nid);
$result = \Drupal::service('token')->replace(
    '[node:field_custom|node:title]',
    ['node' => $node]
);

Expected Result: The token should be replaced by the corresponding value (e.g., the node title).
Actual Result: The output is the literal string "[node:field_custom]".

Now, execute the code with the ['clear' => true] option:

$node = \Drupal\node\Entity\Node::load($nid);
$result = \Drupal::service('token')->replace(
    '[node:field_title_for_yandex_direct|node:title]',
    ['node' => $node],
    ['clear' => true]
);

Expected and Actual Result: The output is the correct node title.

Impact:
This behavior indicates that the token_or module only processes the token replacement correctly when the clear option is explicitly set to true. Without this flag, the service fails to substitute the token values as expected, which could lead to inconsistencies and confusion in environments relying on default behavior.

πŸ› Bug report
Status

Active

Version

2.3

Component

Code

Created by

πŸ‡·πŸ‡ΊRussia 6RUN0

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

Merge Requests

Comments & Activities

  • Issue created by @6RUN0
  • πŸ‡·πŸ‡ΊRussia 6RUN0

    6run0 β†’ changed the visibility of the branch 3519448-tokenor-module-fails to hidden.

  • πŸ‡·πŸ‡ΊRussia 6RUN0

    6run0 β†’ changed the visibility of the branch 3519448-tokenor-module-fails to active.

  • πŸ‡·πŸ‡ΊRussia 6RUN0
  • πŸ‡ΊπŸ‡ΈUnited States pianomansam

    @6run0 thank you for the bug report and issue fork with a potential fix. In order for it to be considered, please add tests that demonstrate both the bug and the fix.

  • πŸ‡΅πŸ‡ΉPortugal tiage

    Hey yall,
    I’m getting an issue when using [3519448-tokenor-module-fails] to send Twig-based mail templates via Symfony Mailer: the HTML comments in my templates are being stripped out entirely.

    What I’m seeing
    Any HTML comment of the form <!--[if mso | IE]>…<![endif]--> is picked up by the token detector and removed from the rendered message. For example, a snippet like:

    <tr>
      <td><!--[if mso | IE]><table><tr><td><![endif]-->
        <div>
          <table>
            <tbody>
              <tr>
                ...more tables...
              </tr>
            </tbody>
          </table>
        </div>
      <!--[if mso | IE]></td></tr></table><![endif]--></td>
    </tr>
    

    is ending up as:

    <tr>
      <td><!--><table><tr><td><![endif]-->
        <div>
          <table>
            <tbody>
              <tr>
                ...more tables...
              </tr>
            </tbody>
          </table>
        </div>
      <!--></td></tr></table><![endif]--></td>
    </tr>
    



    Where's the problem
    I think the problem lies in the "src/TokenOrTokensPreAlter.php" specifically in "tokensPreAlter" function as shown bellow:

    ...
    preg_match_all("/\[[^\[\]]+\]/", $text, $matches);
    
    if (empty($matches)) {
      return;
    }
    
    foreach (reset($matches) as $match) {
      if (strpos($match, '|') !== FALSE) {
        $match_clean = substr(substr($match, 1), 0, -1);
        $sub_tokens = explode('|', $match_clean);
    ...
    
    1. preg_match_all("/\[[^\[\]]+\]/", $text, $matches); identifies them as tokens
    2. strpos($match, '|') !== FALSE identifies them as a token_or

    Thanks,
    TM

  • πŸ‡ΊπŸ‡ΈUnited States pianomansam

    @tiage thanks for the feedback on this issue. Feel free to create a separate issue branch and come up with a solution.

  • First commit to issue fork.
  • Merge request !24Resolve #3519448 "Token or module fails" β†’ (Merged) created by lolgm
  • Pipeline finished with Success
    about 1 month ago
    Total: 186s
    #506657
  • Pipeline finished with Success
    about 1 month ago
    #506668
  • πŸ‡΅πŸ‡ΉPortugal lolgm

    I’ve added tests to demonstrate the problem.
    Two functional tests in TokenOrWebformFunctionalBrokenTest were updated to reflect the expected behavior.

  • Pipeline finished with Skipped
    about 1 month ago
    #507638
  • πŸ‡ΊπŸ‡ΈUnited States pianomansam

    @lolgm thanks for the tests. With those passing, I've gone ahead and merged this into dev.

  • πŸ‡ΊπŸ‡ΈUnited States pianomansam

    Released in v2.3.2

Production build 0.71.5 2024