How to get the value of a taxonomy term from entity reference using a token? We have an Entity reference field on Basic Page content type that is set to Articles. The Articles content type has a field, field_category that is a taxonomy term. 

What is the correct token to get the value of the field_category?

Basic Page:


field_articles -> entity reference to Article content type. 


Article page:


field_category -> taxonomy term field. 


Goal:


Return the field_category label from the Article node that is set on the Basic Page


Example:

Basic Page -> page1(nid=0)


field_articles = articlePage1 (target_id = 1)


Article page -> ArticlePage1 (nid = 1)


field_category = Cloud (tid = 10)


Token should return Cloud for page1. 

We tried the following but it didn't work. 

[node:field_articles:entity:field_category]
You try use hook_tokens

    /**
     * Implements hook_tokens().
     */
    function mymodule_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = array();
// $type is token type
      if ($type == 'node' &amp;&amp; !empty($data['node'])) {
        foreach ($tokens as $name =&gt; $original) {
      // to do
        $replacements[$original] ='xxx';
       }
      }
      return $replacements;
    }


See more api hook_tokens()