How to Use Tokens in Drupal Fields

How to Use Tokens in Drupal Fields

Token is one of the 3 most popular modules in Drupal.

It allows you to use small placeholders to automatically complete tasks.

To take a simple example, if you put [site:name] on your site, it will be replaced by the actual name of your site. To take a more complicated example, you can use Token together with the Pathauto module to automatically create URL patterns for your whole site.

However, Token needs other modules to work. If you want to use Token inside fields, there are several options but one of the most reliable is a module called Token Filter.

If you have Token and Token Filter enabled, here’s how to add tokens to your fields:

  • Go to Configuration > Text formats
  • Click “Configure” next to the text format you want to use. Be careful with this – do not choose a text format that your regular site visitors will be able to use.
  • Check the “Replace tokens” box, as in the image below.
  • Click “Save Configuration”.
media_1419350208299.png
  • Go to Structure > Content types > Manage fields
  • Click “Edit” next to a field
  • There will be a “Browse available tokens” option.
Browser available tokens in Drupal
  • You can use these tokens to set up default values for your fields:
Default values for tokens in Drupal fields

It’s worth noting that some fields types use the “Plain text” format, by default. So, make sure you have token support available for Plain text if needed.

However, be careful with this. Remember that you don’t want to give token access to anyone on your site. In the wrong hands, tokens could expose private information.

Text processing for Drupal tokens

Instructor

  • Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.

0 0 votes
Blog Rating
Subscribe
Notify of
16 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jorge

Howdy,
Interesting module. Can one use tokens to render fields from the current node/content type?

steve

Hi Jorge. Yes, you’ll find a list of available tokens in /admin/help/token/
Under “node”, that will include tokens for all your fields.

Goli Houssou

hi thanks for this clear explanation

However is possible to have token listed (possible custom token list ) in a select list box for insertion?

Exactly as the Advance Entity token module provide trough is submodule Advanced Entity Insert ?

Nithin Kolekar

is this only for textarea fields or can it used for any text fields?

Brooke

Same question. I’m hoping to do this with a plain text field. 

Adrian Cid Almaguer

thanks for the tutorial, I will use this in a site. 😉

Jelle den Butter

Is is also possible to include custom tokens for specific fields?

schifazl

I don’t like this solution. From the entity modelling perspective, you’re mixing data from different entities into one. I would prefer using the EVA module and attach a view to the entity display.

steve

More on EVA for anyone reading this: https://www.ostraining.com/class/eva/

Lincoln

How are people commenting two years ago if this article was written last week?

steve

The article was published two years ago, then had a major update for 2017

Lincoln

Oh gotcha

Ramya

Hey I want to use token generated by “Auto login Url” like

[‘tokens’][‘user’][‘auto-login-url-account-edit-token’] in mass mailing.

But it seems to fail.

I get it as text.
Could You please guide me !!!

gilberto

hi, i need to use a field in content type as Token, so.

Gaetano Leoni

Hi Steve, I read with interest the article you published and I’m interested in why I can use it for my application. I use D8 but when I browse the tokens it doesn’t show me the token node.
I also did a test by creating a text field and writing the token for the year, but it doesn’t show me anything. I don’t understand where I’m wrong.
Thank you

imrodmartin

Hey Gaetano,

According to the Token Filter docs:
“This doesn’t mean that all tokens will work in every location. For example, if you use a [node:field_foo] token in the text of a block, the token system will not know which node you are referring to and will not replace the token.

It is up to you when you are implementing this module to be aware of the context for token usage and use them appropriately.”

I was able to insert the current site and current year in the body field after I turned on the filter per the instructions above (though they do look a little different in 2020).

hope that helps,
Rod

16
0
Would love your thoughts, please comment.x
()
x