Using Tokens in Drupal 8 fields

Tokens in Drupal are a great way to make content dynamic in views and configuration options, but what if you want to use a token in a field?

For that, we’re going to need a module. I’m using Token Filter to do the job.

I’m using Drush v8.0.5 a Drupal 8 installation, so to install Token Filter all we need to do is tell Drush:

$ drush en -y token_filter

This will tell Drush to enable token_filter, but since we don’t have it installed yet it will first go out and fetch the module, install it, then enable it in one step.

Once installed, we need to tell the text formats that we can use tokens.

To do that, head to Configuration > Content Authoring > Text Formats and Editors, or /admin/config/content/formats as of Drupal 8.3.x.

Select the text format that you want to use tokens in (or create a new one) and then look for this option:

Once that’s checked and saved, any content type that uses that text editor will now have the option to use token, and they will also have a “Browse Tokens” link which will open up a modal with available tokens:

Which will give you the final tokenized result:

This also works really well with custom tokens.

One note is that anyone who has access to these editors will also have access to the tokens they can use, so be aware that this could potentially be a security risk.

Leave a Reply

Your email address will not be published. Required fields are marked *