You are here

public function ExampleTokenEventSubscriber::tokenInfo in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 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_dispatcher

Code

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);
}