0

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)

  1. field_articles = articlePage1 (target_id = 1)

Article page -> ArticlePage1 (nid = 1)

  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]

1 Answer 1

0

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' && !empty($data['node'])) {
        foreach ($tokens as $name => $original) {
      // to do
        $replacements[$original] ='xxx';
       }
      }
      return $replacements;
    }

See more api hook_tokens()

1
  • the custom token has been working as expected. Aug 23, 2018 at 15:38

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.