function CommentTokensHooks::tokenInfo

Same name and namespace in other branches
  1. 11.x core/modules/comment/src/Hook/CommentTokensHooks.php \Drupal\comment\Hook\CommentTokensHooks::tokenInfo()

Implements hook_token_info().

Attributes

#[Hook('token_info')]

File

core/modules/comment/src/Hook/CommentTokensHooks.php, line 23

Class

CommentTokensHooks
Hook implementations for comment.

Namespace

Drupal\comment\Hook

Code

public function tokenInfo() : array {
  $type = [
    'name' => $this->t('Comments'),
    'description' => $this->t('Tokens for comments posted on the site.'),
    'needs-data' => 'comment',
  ];
  $tokens = [];
  // Provides an integration for each entity type except comment.
  foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
    if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class)) {
      continue;
    }
    if (\Drupal::service('comment.manager')->getFields($entity_type_id)) {
      // Get the correct token type.
      $token_type = $entity_type_id == 'taxonomy_term' ? 'term' : $entity_type_id;
      // @todo Make this work per field. See https://www.drupal.org/node/2031903.
      $tokens[$token_type]['comment-count'] = [
        'name' => $this->t("Comment count"),
        'description' => $this->t("The number of comments posted on an entity."),
      ];
    }
  }
  // Core comment tokens.
  $comment['cid'] = [
    'name' => $this->t("Comment ID"),
    'description' => $this->t("The unique ID of the comment."),
  ];
  $comment['uuid'] = [
    'name' => $this->t('UUID'),
    'description' => $this->t("The UUID of the comment."),
  ];
  $comment['hostname'] = [
    'name' => $this->t("IP Address"),
    'description' => $this->t("The IP address of the computer the comment was posted from."),
  ];
  $comment['mail'] = [
    'name' => $this->t("Email address"),
    'description' => $this->t("The email address left by the comment author."),
  ];
  $comment['homepage'] = [
    'name' => $this->t("Home page"),
    'description' => $this->t("The home page URL left by the comment author."),
  ];
  $comment['title'] = [
    'name' => $this->t("Title"),
    'description' => $this->t("The title of the comment."),
  ];
  $comment['body'] = [
    'name' => $this->t("Content"),
    'description' => $this->t("The formatted content of the comment itself."),
  ];
  $comment['langcode'] = [
    'name' => $this->t('Language code'),
    'description' => $this->t('The language code of the language the comment is written in.'),
  ];
  $comment['url'] = [
    'name' => $this->t("URL"),
    'description' => $this->t("The URL of the comment."),
  ];
  $comment['edit-url'] = [
    'name' => $this->t("Edit URL"),
    'description' => $this->t("The URL of the comment's edit page."),
  ];
  // Chained tokens for comments.
  $comment['created'] = [
    'name' => $this->t("Date created"),
    'description' => $this->t("The date the comment was posted."),
    'type' => 'date',
  ];
  $comment['changed'] = [
    'name' => $this->t("Date changed"),
    'description' => $this->t("The date the comment was most recently updated."),
    'type' => 'date',
  ];
  $comment['parent'] = [
    'name' => $this->t("Parent"),
    'description' => $this->t("The comment's parent, if comment threading is active."),
    'type' => 'comment',
  ];
  $comment['entity'] = [
    'name' => $this->t("Entity"),
    'description' => $this->t("The entity the comment was posted to."),
    'type' => 'entity',
  ];
  $comment['author'] = [
    'name' => $this->t("Author"),
    'description' => $this->t("The author name of the comment."),
    'type' => 'user',
  ];
  return [
    'types' => [
      'comment' => $type,
    ],
    'tokens' => [
      'comment' => $comment,
    ] + $tokens,
  ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.