Tokens
The last thing we will cover in this chapter is the Token API in Drupal 9. We will cover a few bits of theory and, as usual, demonstrate them via examples on our existing Hello World module code. We will do this in the context of the emails we are sending out for error logs.
It would be nice if we could include some personalized information in the mail text without having to hardcode it in the module code or configuration. For example, in our case, we might want to include the username of the current user that is triggering the error log that is being emailed.
Let's first understand how the Token API works, before going into our Hello World module.
The Token API
Tokens in Drupal are a standard formatted placeholder, which can be found inside a string and replaced by a real value extracted from a related object. The format tokens use is type:token
, where type
is the machine-readable name of a token type (a group of related tokens), and token
is the machine-readable...