After enabling Token and Redirect modules, I've found at /admin/help/token that I can use Redirect tokens. For example:

Redirect tokens in Drupal at /admin/help/token

However, I've got the problem finding the place where and how these tokens are defined (which file and hook or class).

Any ideas?


I've tried to search inside all files in docroot, e.g.

$ grep -r "The URL of the redirect" .
(nothing)
$ grep -r "The URL of the" .
(returns everything apart of redirect, no matching placeholders found)
$ grep -r token modules/contrib/redirect/
modules/contrib/redirect/config/install/views.view.redirect.yml:          tokenize: false
modules/contrib/redirect/modules/redirect_404/config/install/views.view.redirect_404.yml:          tokenize: false
modules/contrib/redirect/modules/redirect_404/redirect_404.routing.yml:    _csrf_token: 'TRUE'
modules/contrib/redirect/src/EventSubscriber/RouteNormalizerRequestSubscriber.php:      // Strip off query parameters added by the route such as a CSRF token.
$ grep -r redirect modules/contrib/token/
modules/contrib/token/src/Controller/TokenCacheController.php:   * Clear caches and redirect back to the frontpage.
modules/contrib/token/src/Controller/TokenCacheController.php:    return $this->redirect('<front>');
$ find modules/contrib/redirect modules/contrib/token -name "*.tokens.inc"
modules/contrib/token/tests/modules/token_module_test/token_module_test.tokens.inc
modules/contrib/token/token.tokens.inc (Note: nothing here for redirect)

So basically I couldn't find the source which module is defining those. This can help me in order to find out, why some of them aren't working as expected.


The main reason I ask this, because I would expect for redirect URL such as /foo/XXX, to return XXX when using [redirect:url:args:last], but these tokens aren't populated at all.

  • 1
    redirect is an entity type - tokens are provided for its base and other fields automagically – Clive Dec 4 at 17:08
  • Does it mean these tokens are populated and parsed by the core? – kenorb Dec 4 at 17:10
  • Yes exactly that @kenorb. Or maybe by the contrib token module in some cases. But not by the module that defines the entity type – Clive Dec 4 at 17:11
  • Pretty confusing, as I was expecting that [redirect:url:args:last] will give me the last part of the URL of the redirect path, but it doesn't work at all. So making redirect like /foo/XXX, I would expect that [redirect:url:args:last] would return me XXX. However, since these are dynamically created somewhere in core, I think they're useless then. – kenorb Dec 4 at 17:14
  • That example would work if the route was defined as foo/{arg}. But if the route is foo/bar, static, then it doesn’t contain any parameters, so the token would be empty – Clive Dec 4 at 17:20
up vote 0 down vote accepted

As Clive mentioned, since Redirect is an entity, those tokens are automatically generated by the Token module's token.tokens.inc.

I suggest you read Custom Tokens in Drupal 8 to understand how they are declared & work.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.