function CommentLazyBuilders::buildLinks

Same name in other branches
  1. 9 core/modules/comment/src/CommentLazyBuilders.php \Drupal\comment\CommentLazyBuilders::buildLinks()
  2. 8.9.x core/modules/comment/src/CommentLazyBuilders.php \Drupal\comment\CommentLazyBuilders::buildLinks()
  3. 10 core/modules/comment/src/CommentLazyBuilders.php \Drupal\comment\CommentLazyBuilders::buildLinks()

Build the default links (reply, edit, delete …) for a comment.

Parameters

\Drupal\comment\CommentInterface $entity: The comment object.

\Drupal\Core\Entity\EntityInterface $commented_entity: The entity to which the comment is attached.

Return value

array An array that can be processed by Link::preRenderLinks().

See also

\Drupal\Core\Render\Element\Link::preRenderLinks()

1 call to CommentLazyBuilders::buildLinks()
CommentLazyBuilders::renderLinks in core/modules/comment/src/CommentLazyBuilders.php
#lazy_builder callback; builds a comment's links.

File

core/modules/comment/src/CommentLazyBuilders.php, line 168

Class

CommentLazyBuilders
Defines a service for comment #lazy_builder callbacks.

Namespace

Drupal\comment

Code

protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
    $links = [];
    $status = $commented_entity->get($entity->getFieldName())->status;
    if ($status == CommentItemInterface::OPEN) {
        if ($entity->access('delete')) {
            $links['comment-delete'] = [
                'title' => t('Delete'),
                'url' => $entity->toUrl('delete-form'),
            ];
        }
        if ($entity->access('update')) {
            $links['comment-edit'] = [
                'title' => t('Edit'),
                'url' => $entity->toUrl('edit-form'),
            ];
        }
        $field_definition = $commented_entity->getFieldDefinition($entity->getFieldName());
        if ($entity->isPublished() && $entity->access('create') && $field_definition->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED) {
            $links['comment-reply'] = [
                'title' => t('Reply'),
                'url' => Url::fromRoute('comment.reply', [
                    'entity_type' => $entity->getCommentedEntityTypeId(),
                    'entity' => $entity->getCommentedEntityId(),
                    'field_name' => $entity->getFieldName(),
                    'pid' => $entity->id(),
                ]),
            ];
        }
        if (!$entity->isPublished() && $entity->access('approve')) {
            $links['comment-approve'] = [
                'title' => t('Approve'),
                'url' => Url::fromRoute('comment.approve', [
                    'comment' => $entity->id(),
                ]),
            ];
        }
        if (empty($links) && $this->currentUser
            ->isAnonymous()) {
            $links['comment-forbidden']['title'] = $this->commentManager
                ->forbiddenMessage($commented_entity, $entity->getFieldName());
        }
    }
    // Add translations link for translation-enabled comment bundles.
    if ($this->moduleHandler
        ->moduleExists('content_translation') && $this->access($entity)
        ->isAllowed()) {
        $links['comment-translations'] = [
            'title' => t('Translate'),
            'url' => $entity->toUrl('drupal:content-translation-overview'),
        ];
    }
    return [
        '#theme' => 'links__comment__comment',
        // The "entity" property is specified to be present, so no need to check.
'#links' => $links,
        '#attributes' => [
            'class' => [
                'links',
                'inline',
            ],
        ],
    ];
}

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