I'm trying to use the token service to replace a piece of text using a Taxonomy Term.

Hi [term:field_first_name], ...

The term is getting loaded correctly, but the text is not changing:

    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);

    $token_service = \Drupal::token();
    $text = $token_service->replace($text, [
        'term' => $term,


The print returns:
Hi [term:field_first_name], ...
instead of:
Hi John, ...

I'm currently using drupal version 9.3.2.

  • 1
    Welcome to Drupal Answers! Are you sure field_first_name is a field added to taxonomy terms? It seems more a user field, by its name.
    – apaderno
    Commented May 9, 2022 at 12:51
  • @apaderno Thank you for your answer, but it is a field, I just wanted to simplify the example.
    – murko233
    Commented May 9, 2022 at 14:08

1 Answer 1


Drupal out-of-the-box has only basic token support, which can replace core entity standard fields like [term:name] or [term:description].

Configured fields, usually those with a prefix field_, need the module Token to be installed. Then [term:field_first_name] will be replaced, if the field exists.

  • Thank you for your answer. I know i didn't mention this, but i didn't want to rely on modules, thought it was already built in. I chose another path, looped through term: foreach($term as $field_name => $_) and then just used str_replace, to replace the text with the field names
    – murko233
    Commented May 11, 2022 at 10:23
  • I agree Drupal site owners should not install large amounts of modules, including beta or dev versions. But there are a few standard modules you can and should use if you need the functionality. And modules like Token can be installed and uninstalled with very low risk because they don't change the database schema.
    – 4uk4
    Commented May 11, 2022 at 11:47

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.