Token module in Drupal 8
Mashirul Haque
https://www.drupal.org/u/mashir
Agenda
● What is Token?
● What Token module does?
● How to create a custom token Programatically?
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?
Two hooks are required to create a custom token.
● hook_token_info()
● hook_tokens()
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.
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
hook_tokens()
hook_tokens()
Parameters:
● DrupalCoreRenderBubbleableMetadata
$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().
DEMO
THANKS

Token module in drupal 8

  • 1.
    Token module inDrupal 8 Mashirul Haque https://www.drupal.org/u/mashir
  • 2.
    Agenda ● What isToken? ● What Token module does? ● How to create a custom token Programatically?
  • 3.
    What is Token? Tokenis a small bits of text, which can be placed into a large documents as a simple placeholders.
  • 4.
    What Token moduledoes? 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.
  • 6.
    How to createa custom token Programatically? Two hooks are required to create a custom token. ● hook_token_info() ● hook_tokens()
  • 7.
    hook_token_info() It provides informationof available placeholders and Token types. Return Value: It returns an array of all available tokens and token type.
  • 8.
    It provides thereplacement 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 hook_tokens()
  • 9.
    hook_tokens() Parameters: ● DrupalCoreRenderBubbleableMetadata $bubbleable_metadata: Bubbleablemetadata. Return value: It return an associative array of replacement values.
  • 10.
    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().
  • 11.
  • 12.