Run prettier on all *.md files

```
prettier '{app,source}/**/**.md' --write
```
This commit is contained in:
Oliver Davies 2020-03-08 17:52:59 +00:00
parent a3ceeaf0f3
commit 85a10c545b
170 changed files with 5127 additions and 2282 deletions

View file

@ -1,31 +1,44 @@
---
title: Creating and using custom tokens in Drupal 7
date: 2013-02-16
excerpt: This post outlines the steps required to create your own custom tokens in Drupal.
excerpt:
This post outlines the steps required to create your own custom tokens in
Drupal.
tags:
- drupal
- drupal-planet
- drupal-7
- tokens
---
This post outlines the steps required to create your own custom tokens in Drupal.
When writing the recent releases of the [Copyright Block](http://drupal.org/project/copyright_block) module, I used tokens to allow the user to edit and customise their copyright message and place the copyright_message:dates token in the desired position. When the block is rendered, the token is replaced by the necessary dates.
This post outlines the steps required to create your own custom tokens in
Drupal.
We will be using the fictional *foo* module to demonstrate this.
When writing the recent releases of the
[Copyright Block](http://drupal.org/project/copyright_block) module, I used
tokens to allow the user to edit and customise their copyright message and place
the copyright_message:dates token in the desired position. When the block is
rendered, the token is replaced by the necessary dates.
We will be using the fictional _foo_ module to demonstrate this.
## Requirements
* [Token module](http://drupal.org/project/token)
- [Token module](http://drupal.org/project/token)
## Recommended
* [Devel module](http://drupal.org/project/devel) - useful to run `dpm()` and `kpr()` functions
* [Copyright Block module](http://drupal.org/project/copyright_block) - 7.x-2.x and 6.x-1.x use tokens, handy as a reference
- [Devel module](http://drupal.org/project/devel) - useful to run `dpm()` and
`kpr()` functions
- [Copyright Block module](http://drupal.org/project/copyright_block) - 7.x-2.x
and 6.x-1.x use tokens, handy as a reference
## Implementing hook_token_info()
The first thing that we need to do is define the new token type and/or the token itself, along with it's descriptive text. To view the existing tokens and types, use `dpm(token_get_info());`, assuming that you have the [Devel module](http://drupal.org/project/devel) installed.
The first thing that we need to do is define the new token type and/or the token
itself, along with it's descriptive text. To view the existing tokens and types,
use `dpm(token_get_info());`, assuming that you have the
[Devel module](http://drupal.org/project/devel) installed.
```language-php
/**
@ -42,13 +55,16 @@ function foo_token_info() {
}
```
In this case, the token called *bar* resides within the *foo* group.
In this case, the token called _bar_ resides within the _foo_ group.
If I needed to add a new token within an existing token type, such as 'node', the syntax would be `$info['tokens']['node']['bar']`.
If I needed to add a new token within an existing token type, such as 'node',
the syntax would be `$info['tokens']['node']['bar']`.
## Implementing hook_tokens()
Now that the Token module is aware of our new token, we now need to determine what the token is replaced with. This is done using `hook_tokens()`. Here is the basic code needed for an implementation:
Now that the Token module is aware of our new token, we now need to determine
what the token is replaced with. This is done using `hook_tokens()`. Here is the
basic code needed for an implementation:
```language-php
/**
@ -64,7 +80,13 @@ function foo_tokens($type, $tokens, array $data = array(), array $options = arra
}
```
The first thing to check for is the type of token using an `if()` function, as this could be an existing type like 'node', 'user' or 'site', or a custom token type like 'foo'. Once we're sure that we're looking at the right type(s), we can use `foreach ($tokens as $name => $original)` to loop through each of the available tokens using a `switch()`. For each token, you can perform some logic to work out the replacement text and then add it into the replacements array using `$replacements[$original] = $new;`.
The first thing to check for is the type of token using an `if()` function, as
this could be an existing type like 'node', 'user' or 'site', or a custom token
type like 'foo'. Once we're sure that we're looking at the right type(s), we can
use `foreach ($tokens as $name => $original)` to loop through each of the
available tokens using a `switch()`. For each token, you can perform some logic
to work out the replacement text and then add it into the replacements array
using `$replacements[$original] = $new;`.
```language-php
/**
@ -127,7 +149,9 @@ function copyright_block_tokens($type, $tokens, array $data = array(), array $op
## Using token_replace()
With everything defined, all that we now need to do is pass some text through the `token_replace()` function to replace it with the values defined within `hook_token()`.
With everything defined, all that we now need to do is pass some text through
the `token_replace()` function to replace it with the values defined within
`hook_token()`.
```language-php
$a = t('Something');