{"id":58107,"date":"2023-08-29T12:44:03","date_gmt":"2023-08-29T07:14:03","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58107"},"modified":"2023-08-29T16:22:11","modified_gmt":"2023-08-29T10:52:11","slug":"how-to-create-a-custom-token-in-drupal-8-9","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-create-a-custom-token-in-drupal-8-9\/","title":{"rendered":"How to create a custom token in Drupal 8\/9"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Tokens are an essential feature in Drupal that allows you to dynamically insert data into various parts of your website, such as content, email templates, etc. While Drupal provides a set of default tokens, you might find yourself in a situation where you must create custom tokens to display specific information or improve the user experience. In this blog post, I walk you through the process of creating custom tokens in Drupal.<\/p>\n<p><strong>Step 1<\/strong><\/p>\n<p>Create a Custom Module Custom tokens are usually created within custom modules. If you haven&#8217;t already, create a custom module for your project.<\/p>\n<p><strong>Step 2<\/strong><\/p>\n<p>Define the Token In your custom module&#8217;s folder, locate the <span style=\"font-size: x-large;\"><code class=\"western\">.module<\/code><\/span> file (example: <span style=\"font-size: x-large;\"><code class=\"western\">custom.module<\/code><\/span>) and open it in a text editor. Now, define your custom token using the <span style=\"font-size: x-large;\"><code class=\"western\">hook_token_info()<\/code><\/span> function. Add the following code:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58105 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-19.png\" alt=\"\" width=\"1092\" height=\"380\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-19.png 1092w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-19-300x104.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-19-1024x356.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-19-768x267.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-19-624x217.png 624w\" sizes=\"(max-width: 1092px) 100vw, 1092px\" \/><\/p>\n<p><strong>Step 3<\/strong><\/p>\n<p>Implement the Token Value Next, you need to implement the token&#8217;s value by defining a function that generates the required data. Add the following code to your <span style=\"color: #374151;\"><span style=\"font-size: x-large;\"><code class=\"western\">.module<\/code><\/span><\/span> file:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58106 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-34.png\" alt=\"\" width=\"1143\" height=\"556\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-34.png 1143w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-34-300x146.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-34-1024x498.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-34-768x374.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-23-10-06-34-624x304.png 624w\" sizes=\"(max-width: 1143px) 100vw, 1143px\" \/><\/p>\n<p><strong>Step 4<\/strong><\/p>\n<p>Clear the Cache After defining your custom token and implementing its value, you need to clear Drupal&#8217;s cache to make the new token available. Go to &#8220;Configuration&#8221; &gt; &#8220;Development&#8221; &gt; &#8220;Performance&#8221; and click the &#8220;Clear all caches&#8221; button.<\/p>\n<p><strong>Step 5<\/strong><\/p>\n<p>Using the Custom Token Your custom token is now ready for use. You can use it in various places, such as node content or email templates, by using the following syntax: <span style=\"color: #374151;\"><span style=\"font-size: x-large;\"><code class=\"western\">[custom:registration_date]<\/code><\/span><\/span><span style=\"color: #374151;\"><span style=\"font-family: S hne, ui-sans-serif, system-ui, apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, Helvetica Neue, Arial, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;\"><span style=\"font-size: x-large;\">.<\/span><\/span><\/span><\/p>\n<h2>Conclusion<\/h2>\n<p>Creating custom tokens in Drupal allows you to enhance your website&#8217;s functionality and provide dynamic content to your users.<\/p>\n<p>&nbsp;<\/p>\n<p>Explore our\u00a0<a href=\"https:\/\/www.tothenew.com\/blog\/\">blogs<\/a>\u00a0for an in-depth understanding of various trending topics.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction Tokens are an essential feature in Drupal that allows you to dynamically insert data into various parts of your website, such as content, email templates, etc. While Drupal provides a set of default tokens, you might find yourself in a situation where you must create custom tokens to display specific information or improve the [&hellip;]<\/p>\n","protected":false},"author":1510,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":43},"categories":[3602],"tags":[4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58107"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1510"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58107"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58107\/revisions"}],"predecessor-version":[{"id":58197,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58107\/revisions\/58197"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}