You are here

protected function CommentLazyBuilders::buildLinks in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/comment/src/CommentLazyBuilders.php \Drupal\comment\CommentLazyBuilders::buildLinks()
  2. 9 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 drupal_pre_render_links().

File

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

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
      ->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',
      ],
    ],
  ];
}