Add hook for tokens post-treatments

Created on 1 June 2018, almost 7 years ago
Updated 15 April 2025, 11 days ago

At this time it is possible to add a hook for token replacement values.
But it's not possible to post-treat the resulting string (after all replacements has been done) without modifying token_replace() itself (AFAIK).

We have two cases where it would be useful:

  • to "reformate" output when replacement should takes context into account (i.e. "a train" / "an apple")
  • to add conditionals (i.e. like ternary operator) in syntax (new syntax over the token's one), to have fallback to existing values in replacements

We manage to make both of these, but we had to add a function call into includes/token.inc (catching result of str_replace(), before returning). And we don't like it of course because it's not upgrade-aware. This is just for testing, not for prod.

Adding a hook would allow anyone to write modules to post-treat token replacement results. I.e. at end of token_replace():

- return str_replace($tokens, $values, $text);
+ $text = str_replace($tokens, $values, $text);
+   $context = array(
+    'text' => $text,
+   'data' => $data,
+    'options' => $options,
+  );
+  drupal_alter('token_post', $text, $context);
+  return $text;

Any feedback about that?

Feature request
Status

Closed: outdated

Version

7.0 ⚰️

Component

token system

Created by

🇫🇷France Yannick Perret

Live updates comments and jobs are added and updated live.
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.

  • 🇦🇺Australia geoffreyr Sydney, AU / Gadigal country

    Given that Drupal 7 has reached its end of life , this issue should be closed.

    Please upgrade to a supported version and feel free to reopen the issue on the new version if applicable.

    If you're still on D7 it might be worth a try to implement hook_tokens_alter and adjust your custom module's priority to run after everything else. You could also apply a custom patch to implement this change using Composer or Drush Patchfile.

Production build 0.71.5 2024