You are here

public function Tokens::getTokens in Content Moderation Notifications 8.3

Generate tokens.

Parameters

string $type: The machine-readable name of the type (group) of token being replaced, such as 'node', 'user', or another type defined by a hook_token_info() implementation.

array $tokens: An array of tokens to be replaced. The keys are the machine-readable token names, and the values are the raw [type:token] strings that appeared in the original text.

array $data: An associative array of data objects to be used when generating replacement values, as supplied in the $data parameter to \Drupal\Core\Utility\Token::replace().

array $options: An associative array of options for token replacement; see \Drupal\Core\Utility\Token::replace() for possible values.

\Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata: Bubbleable metadata.

Return value

array An associative array of replacement values, keyed by the raw [type:token] strings from the original text. The returned values must be either plain text strings, or an object implementing MarkupInterface if they are HTML-formatted.

See also

\content_moderation_notifications_tokens()

File

src/Tokens.php, line 106

Class

Tokens
Token generation and information class.

Namespace

Drupal\content_moderation_notifications

Code

public function getTokens($type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type === 'content_moderation_notifications' && isset($data['entity']) && $data['entity'] instanceof ContentEntityInterface) {
    $entity = $data['entity'];
    if ($this->notificationInformation
      ->isModeratedEntity($entity)) {
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'workflow':
            $workflow = $this->notificationInformation
              ->getWorkflow($entity)
              ->label();
            $replacements[$original] = $workflow;
            $bubbleable_metadata
              ->addCacheableDependency($workflow);
            break;
          case 'from-state':
            if ($transition = $this->notificationInformation
              ->getTransition($entity)) {
              $replacements[$original] = $this->notificationInformation
                ->getPreviousState($entity)
                ->label();
              $bubbleable_metadata
                ->addCacheableDependency($transition);
            }
            break;
          case 'to-state':
            if ($transition = $this->notificationInformation
              ->getTransition($entity)) {
              $replacements[$original] = $transition
                ->to()
                ->label();
              $bubbleable_metadata
                ->addCacheableDependency($transition);
            }
            break;
        }
      }
    }
  }
  return $replacements;
}