You are here

function message_example_tokens in Message 8

Same name and namespace in other branches
  1. 7 message_example/message_example.tokens.inc \message_example_tokens()

Implements hook_tokens().

File

modules/message_example/message_example.tokens.inc, line 64
Place holder for the message example module.

Code

function message_example_tokens($type, $tokens, array $data = [], array $options = []) {
  $token_service = \Drupal::token();
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = [];
  if ($type == 'message' && !empty($data['message'])) {

    /** @var \Drupal\message\Entity\Message $message */
    $message = $data['message'];
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Simple key values on the comment.
        case 'user-name':
          $replacements[$original] = $message
            ->getOwner()
            ->label();
          break;
        case 'user-url':
          if ($message
            ->getOwner()) {
            $replacements[$original] = $message
              ->getOwner()
              ->toUrl()
              ->toString();
          }
          break;
        case 'node-title':
          if ($message
            ->bundle() == 'example_create_comment') {
            $replacements[$original] = $message->field_comment_reference->entity
              ->getCommentedEntity()
              ->label();
          }
          else {
            $replacements[$original] = $message->field_node_reference
              ->get(0)->entity
              ->label();
          }
          break;
        case 'node-url':
          if ($message
            ->bundle() == 'example_create_comment') {
            $replacements[$original] = $message->field_comment_reference->entity
              ->getCommentedEntity()
              ->url();
          }
          else {
            $replacements[$original] = $message->field_node_reference
              ->get(0)->entity
              ->url();
          }
          break;
        case 'node-render':
          if ($message
            ->bundle() == 'example_create_comment') {
            $entity = $message->field_comment_reference->entity
              ->getCommentedEntity();
          }
          else {
            $entity = $message->field_node_reference
              ->get(0)->entity;
          }
          $view_builder = \Drupal::entityTypeManager()
            ->getViewBuilder($entity
            ->getEntityTypeId());
          $message_view = $view_builder
            ->view($entity, 'teaser');
          $replacements[$original] = \Drupal::service('renderer')
            ->renderRoot($message_view);
          break;
        case 'comment-url':
          $replacements[$original] = $message->field_comment_reference->entity
            ->id();
          break;
      }
    }
  }
  return $replacements;
}