This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How to Create Custom Drupal 8 Tokens Modules

Updated on  30th April

3 Min Read
Reading Time: 3 minutes

In a previous blog post, I explained the process of creating a form using Drupal 8 Webform Module, this time we will discuss the process of creating Custom Tokens in Drupal 8.

create custom drupal token modules

Tokens is a fundamental element in any Drupal site. It comprises of custom pieces of text that can be used as placeholders for predefined values. These tokens are placed in a [token:type] format.

While there are many tokens available for Drupal via the API provided by the Drupal token module, there will come a time when a Drupal site builder requires the use of a custom token.

That is what I’ll be going through in this post, teaching readers how to create custom tokens in Drupal 8.

Create Custom Modules

In order to create a custom token, you will first need to create a custom Drupal token module which will use the two important parts for this process, declaring the token (by using hook_token_info()) and providing values for the token (by using hook_tokens()).

To create a custom module, first, go to your Drupal site’s module directory and create a new folder there with your custom module name.

In this folder, create a new file with your module’s name. Example: customtokenmodule.info.yml.

Here is the code you will be using to declare your module to Drupal:

Save the file.  go to your Drupal site and enable the module.

enable custom module

Create Token File

Now that we have created a custom Drupal token module, we need to include token functionality in it. As mentioned earlier, we will be making use of the hook_token_info() and hook_tokens() functions.

So, create a file inside your custom module directory and give it the same name as your custom module but with the extension .tokens.inc. In my case, it will be customtokenmodule.token.inc.

In this file, implement the following code:

The first part of the code that uses the hook_token_info() function is used to declare the tokens as well as what function it should perform and make them visible to Drupal.

custom tokens list

In the second half of the code, the hook_tokens() function is used to actually make the token perform its desired function.

Conclusion

That’s it.

I have successfully created a custom Drupal token module.

Remember, the two most crucial aspects in this process are the utilization of the hook_token_info() and hook_tokens() functions.

I hope you found this post useful.

Let me know if you have any questions and I’ll be happy to answer them.

Share your opinion in the comment section. COMMENT NOW

Hamza Zia

Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.