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?