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_custom|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.

Issue fork token_or-3519448

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

6run0 created an issue. See original summary.

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

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

6run0’s picture

Issue summary: View changes
pianomansam’s picture

Status: Active » Needs work

@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.

tiago’s picture

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

pianomansam’s picture

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

lolgm made their first commit to this issue’s fork.

lolgm’s picture

Version: 2.3.0 » 2.x-dev
Status: Needs work » Needs review

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

  • pianomansam committed c6a15bd6 on 2.x authored by lolgm
    Issue #3519448 by 6run0, lolgm: token_or module fails without explicit '...
pianomansam’s picture

Status: Needs review » Fixed

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

pianomansam’s picture

Status: Fixed » Closed (fixed)

Released in v2.3.2