comment_tokens

comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata)

Implements hook_tokens().

File

core/modules/comment/comment.tokens.inc, line 123
Builds placeholder replacement tokens for comment-related data.

Code

function comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();

  $url_options = array('absolute' => TRUE);
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = NULL;
  }
  $replacements = array();

  if ($type == 'comment' && !empty($data['comment'])) {
    /** @var \Drupal\comment\CommentInterface $comment */
    $comment = $data['comment'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        // Simple key values on the comment.
        case 'cid':
          $replacements[$original] = $comment->id();
          break;

          // Poster identity information for comments.
        case 'hostname':
          $replacements[$original] = $comment->getHostname();
          break;

        case 'mail':
          $mail = $comment->getAuthorEmail();
          // Add the user cacheability metadata in case the author of the comment
          // is not the anonymous user.
          if ($comment->getOwnerId()) {
            $bubbleable_metadata->addCacheableDependency($comment->getOwner());
          }
          $replacements[$original] = $mail;
          break;

        case 'homepage':
          $replacements[$original] = UrlHelper::stripDangerousProtocols($comment->getHomepage());
          break;

        case 'title':
          $replacements[$original] = $comment->getSubject();
          break;

        case 'body':
          // "processed" returns a \Drupal\Component\Render\MarkupInterface via
          // check_markup().
          $replacements[$original] = $comment->comment_body->processed;
          break;

        case 'langcode':
          $replacements[$original] = $comment->language()->getId();
          break;

          // Comment related URLs.
        case 'url':
          $url_options['fragment'] = 'comment-' . $comment->id();
          $replacements[$original] = $comment->url('canonical', $url_options);
          break;

        case 'edit-url':
          $url_options['fragment'] = NULL;
          $replacements[$original] = $comment->url('edit-form', $url_options);
          break;

        case 'author':
          $name = $comment->getAuthorName();
          // Add the user cacheability metadata in case the author of the comment
          // is not the anonymous user.
          if ($comment->getOwnerId()) {
            $bubbleable_metadata->addCacheableDependency($comment->getOwner());
          }
          $replacements[$original] = $name;
          break;

        case 'parent':
          if ($comment->hasParentComment()) {
            $parent = $comment->getParentComment();
            $bubbleable_metadata->addCacheableDependency($parent);
            $replacements[$original] = $parent->getSubject();
          }
          break;

        case 'created':
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
          $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
          break;

        case 'changed':
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
          $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
          break;

        case 'entity':
          $entity = $comment->getCommentedEntity();
          $bubbleable_metadata->addCacheableDependency($entity);
          $title = $entity->label();
          $replacements[$original] = $title;
          break;
      }
    }

    // Chained token relationships.
    if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
      $entity = $comment->getCommentedEntity();
      $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options, $bubbleable_metadata);
    }

    if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options, $bubbleable_metadata);
    }

    if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options, $bubbleable_metadata);
    }

    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options, $bubbleable_metadata);
    }

    if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
      $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options, $bubbleable_metadata);
    }
  }
  // Replacement tokens for any content entities that have comment field.
  elseif (!empty($data[$type]) && $data[$type] instanceof FieldableEntityInterface) {
    /** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
    $entity = $data[$type];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'comment-count':
          $count = 0;
          $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
          $definitions = array_keys($entity->getFieldDefinitions());
          $valid_fields = array_intersect($fields, $definitions);
          foreach ($valid_fields as $field_name) {
            $count += $entity->get($field_name)->comment_count;
          }
          $replacements[$original] = $count;
          break;

        case 'comment-count-new':
          $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
          break;
      }
    }
  }

  return $replacements;
}
doc_Drupal
2016-10-29 08:51:30
Comments
Leave a Comment

Please login to continue.