public function ExampleTokenEventSubscriber::tokenInfo in Hook Event Dispatcher 8.2
Same name and namespace in other branches
- 3.x examples/ExampleTokenEventSubscriber.php \Drupal\hook_event_dispatcher\ExampleTokenEventSubscriber::tokenInfo()
Provides new token types and tokens.
Parameters
\Drupal\core_event_dispatcher\Event\Token\TokensInfoEvent $event: The token info event.
Throws
\UnexpectedValueException
File
- examples/
ExampleTokenEventSubscriber.php, line 44
Class
- ExampleTokenEventSubscriber
- Class ExampleTokenEventSubscriber.
Namespace
Drupal\hook_event_dispatcherCode
public function tokenInfo(TokensInfoEvent $event) : void {
// The node type already exists, but it's just an example how to add a type.
$type = TokenType::create('node', 'Node')
->setDescription('Node tokens')
->setNeedsData('node');
$event
->addTokenType($type);
// Add node token.
$name = new TranslatableMarkup('Serialized string of the node');
$token = Token::create('node', 'serialized', $name)
->setDescription('Node serialized');
$event
->addToken($token);
}