Drupal: Token of a Node

I’ve been working a lot lately with generating emails from within Drupal. I was asked how to create a Token of a piece of Drupal content (a Node). Even when not coding in Drupal — tokens can be very useful! A token in Drupal-lingo is:

Tokens are small bits of text that can be placed into larger documents via simple placeholders, like %site-name or [user]. The Token module provides a central API for modules to use these tokens, and expose their own token values.

Modules may provide their own tokens using drupal Hooks. As of Drupal 7 Tokens are apart of Drupal-core.

Sample token api usage for defining or using tokens

But, the ability to create a token of a full Node view is not possible out of the box. Thankfully, with a bit of searching we can find a Community module that provides this feature to our website. The Advanced Entity Token (AET) provides tokens for entities and their view modes:

Examples[aet:node:1] will return the 1st node with the token view mode (or full if token is not available).

[aet:file:1:view-original] will return the 1st file entity with the original view mode.

[aet:taxonomy_term:1:name] will return the 1st node title field (note that this level of chaining is done by the formal token implementation of the node entity in token/token.tokens.inc -> token_token_info()).

So by using the AET you could create an email template Node Type, which could use Tokens too by the way, which is a Drupal node (content) that you could then embed in a piece of outgoing email by using this node as the message body using [aet:node:NID]. If you don’t want these email template node(s) to not show up on your site from Google or site searches I recommend using the RabbitHole module.

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com