Drupal 8: How to create custom token programmatically

 What is Token:
Token is a small bits of text, which can be placed into a large documents as a simple placeholders
What Token Module Does?
The token module provides an API to provide tokens for other modules. It also provide some additional tokens and provide a browsing facility for all available tokens in UI.
How to create a custom token programatically?

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,

A: Declaring the token (by using hook_token_info()).

hook_token_info(): 

It provides information of available placeholders and Token types

Return Value:

 It returns an array of all available tokens and token type.

B: And providing values for the token (by using hook_tokens()).

hook_tokens(): It provides the replacement value of placeholder tokens.

Parameters:

$type: Machine name of type of token

$tokens: Array of tokens

$data: Array of data objects

$options: Array of options for token replacement

\Drupal\Core\Render\BubbleableMetadata

$bubbleable_metadata: Bubbleable metadata

Return Value: 

It return an associative array of replacement values.

Other Token hooks:

1. hook_token_info_alter():

Parameter:

$data - Array of tokens.

2. hook_tokens_alter()

Parameter:

$replacements - Array of replacements return by hook_tokens().

$context - Array of all keys of 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 custom_token.info.yml. In this file implements the following code.


Create Token File :

So, create a file inside your custom module directory and give it the same name as your custom module but with the extension .module. In my case, it will be custom_token.module. In this file, implement the following code:

<?php


/**

* @file

* contains custom_token.module..

*/

use Drupal\Core\Render\BubbleableMetadata;

/*

* Implemets : hook_token_info().

*/

function custom_token_token_info() {

$types['custom_token_type'] = array(

'name' => t('Custom Token Type'),

'description' => t('Define Custom Token Type')

);

$tokens['custom_token_name'] =array(

                  'name' => t('Custom Token Name'),

'description' => t('Define Custom Token Name')

);

return array(

 'types' => $types,

'tokens' => array(

  'custom_token_type' => $tokens

)

);

}

/*

* Implements hook_tokens()

*/

function custom_token_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {

$replacements = [];

if ($type == 'custom_token_type') {

foreach ($tokens as $name => $original) {

switch ($name) {

case 'custom_token_name':

$text = 'My Custom Token';

$replacements[$original] = $text;

break;

}

}

}

return $replacements;

}


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.

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






No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...