function comment_token_info

Same name in other branches
  1. 7.x modules/comment/comment.tokens.inc \comment_token_info()
  2. 8.9.x core/modules/comment/comment.tokens.inc \comment_token_info()
  3. 10 core/modules/comment/comment.tokens.inc \comment_token_info()
  4. 11.x core/modules/comment/comment.tokens.inc \comment_token_info()

Implements hook_token_info().

File

core/modules/comment/comment.tokens.inc, line 17

Code

function comment_token_info() {
    $type = [
        'name' => t('Comments'),
        'description' => 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' => t("Comment count"),
                'description' => t("The number of comments posted on an entity."),
            ];
            $tokens[$token_type]['comment-count-new'] = [
                'name' => t("New comment count"),
                'description' => t("The number of comments posted on an entity since the reader last viewed it."),
            ];
        }
    }
    // Core comment tokens
    $comment['cid'] = [
        'name' => t("Comment ID"),
        'description' => t("The unique ID of the comment."),
    ];
    $comment['hostname'] = [
        'name' => t("IP Address"),
        'description' => t("The IP address of the computer the comment was posted from."),
    ];
    $comment['mail'] = [
        'name' => t("Email address"),
        'description' => t("The email address left by the comment author."),
    ];
    $comment['homepage'] = [
        'name' => t("Home page"),
        'description' => t("The home page URL left by the comment author."),
    ];
    $comment['title'] = [
        'name' => t("Title"),
        'description' => t("The title of the comment."),
    ];
    $comment['body'] = [
        'name' => t("Content"),
        'description' => t("The formatted content of the comment itself."),
    ];
    $comment['langcode'] = [
        'name' => t('Language code'),
        'description' => t('The language code of the language the comment is written in.'),
    ];
    $comment['url'] = [
        'name' => t("URL"),
        'description' => t("The URL of the comment."),
    ];
    $comment['edit-url'] = [
        'name' => t("Edit URL"),
        'description' => t("The URL of the comment's edit page."),
    ];
    // Chained tokens for comments
    $comment['created'] = [
        'name' => t("Date created"),
        'description' => t("The date the comment was posted."),
        'type' => 'date',
    ];
    $comment['changed'] = [
        'name' => t("Date changed"),
        'description' => t("The date the comment was most recently updated."),
        'type' => 'date',
    ];
    $comment['parent'] = [
        'name' => t("Parent"),
        'description' => t("The comment's parent, if comment threading is active."),
        'type' => 'comment',
    ];
    $comment['entity'] = [
        'name' => t("Entity"),
        'description' => t("The entity the comment was posted to."),
        'type' => 'entity',
    ];
    $comment['author'] = [
        'name' => t("Author"),
        'description' => 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.