I have created a token in .module file. that gives current node title.

But every-time I change the title of node, I have to rebuilt cache so that updated title get reflected in my token.

Of-course clearing cache again and again on every page refresh is not a good idea.

Any other way to call hook_token_info and hook_tokens automatically on every page load?

PS: I also installed "token" module and this module is using hook_module_implements_alter(&$implementations, $hook).


function hook_token_info() {
    $info = array();
    $info['tokens']['etf']['node-title'] = array(
    'name' => t('Node Title'),
    'description' => t("Provides current node title."),
    'type' => 'etf',
    return $info;

function hook_tokens($type, array $tokens, array $data = array(), array $options = array(), BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();

  $url_options = array('absolute' => TRUE);
  if(isset($options['langcode'])) {
      $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
      $langcode = $options['langcode'];
  else {
      $langcode = NULL;
  $request = \Drupal::request();
  $route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT);
  $title = \Drupal::service('title_resolver')->getTitle($request, $route);
  if ($type == 'etf') {

    foreach ($tokens as $name => $original) {
      switch ($name) {
              case 'node-title':
              $replacements[$original] = $title;
//              drupal_flush_all_caches();
//              sleep(2);

  return $replacements;

All code is written in .module file

  • 1
    Your question sounds a bit fishy, I'm not sure you can do what you want to do with tokens. Can you explain a bit what you mean by current node and how you use the token?
    – googletorp
    Commented Apr 22, 2016 at 7:16
  • I agree with @googletorp, here. It's not totally clear what your code is doing. As far as I see, modules implementing tokens don't need to clear any cache, to get their tokens updated. Also, it's not clear how the part about your module implementing hook_module_implements_alter() matters for the answer.
    – apaderno
    Commented Apr 22, 2016 at 7:20
  • I have edited the question with code i'm trying to implement. When using token [etf:node-title], this token gets replaced with current node's title. But for this, I have to re-built cache every time. Commented Apr 22, 2016 at 7:22

2 Answers 2



This is wrong use of tokens, it can never work.


So what you are doing is incorrect usage of tokens. The idea with tokens, is that you have some data object, like a Node, a User etc and based on that you can output something from that object, like the label, uri etc.

Tokens aren't designed to load data from somewhere and display it. For Drupal 8 with all the new caches, it's even more tricky, but even for Drupal 7 this would not work. The reason is that the result of token replacement is cached, since the idea with tokens is that it will always be the same (as long as the data objects which are used for to make the tokens do not change.

To achieve this feature, you would need to load the current node and make it available in the texts where you want to use it as a token, and for Drupal 8 you would also need to add cache context of the url. If I knew what you are using this for, I might be able to give a more precise advise on what the best way would be to achieve this.

  • Thanks @googletorp but the approach of using hook_token_info in .module file worked fine in Drupal 7. Without clearing cache, everything is running perfect. and the same approach is used by drupal.org/project/token (token) contibuted module, which is also running smooth. Commented Apr 22, 2016 at 7:47
  • 1
    @SugandhKhanna You might have been using it in a such a way, that you avoided the issue, but this is definitely a misuse of tokens. Like I said, to really help you we need to know what you actually want to achieve.
    – googletorp
    Commented Apr 22, 2016 at 8:18

The node title already has a token, you can see this, if you install the token module: https://www.drupal.org/project/token

This is only an alpha version, but it is OK to use it.

Go to the help page of the module and you will see that the node title is already there:


You can use this token only in a node context. If you use it outside of this, for example in a block, you have to add a cache context in this block:

$build['#cache']['contexts'][] = 'url.path';

And a cache tag to invalidate the block if the node is changed:

$build['#cache']['tags'][] = 'node:N';

or if any node is changed:

$build['#cache']['tags'][] = 'node_list';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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