CommentLazyBuilders::buildLinks

protected CommentLazyBuilders::buildLinks(CommentInterface $entity, EntityInterface $commented_entity)

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 164

Class

CommentLazyBuilders
Defines a service for comment #lazy_builder callbacks.

Namespace

Drupal\comment

Code

protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
  $links = array();
  $status = $commented_entity->get($entity->getFieldName())->status;

  if ($status == CommentItemInterface::OPEN) {
    if ($entity->access('delete')) {
      $links['comment-delete'] = array(
        'title' => t('Delete'),
        'url' => $entity->urlInfo('delete-form'),
      );
    }

    if ($entity->access('update')) {
      $links['comment-edit'] = array(
        'title' => t('Edit'),
        'url' => $entity->urlInfo('edit-form'),
      );
    }
    if ($entity->access('create')) {
      $links['comment-reply'] = array(
        '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'] = array(
        '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'] = array(
      'title' => t('Translate'),
      'url' => $entity->urlInfo('drupal:content-translation-overview'),
    );
  }

  return array(
    '#theme' => 'links__comment__comment',
    // The "entity" property is specified to be present, so no need to check.
    '#links' => $links,
    '#attributes' => array('class' => array('links', 'inline')),
  );
}
doc_Drupal
2016-10-29 08:50:49
Comments
Leave a Comment

Please login to continue.