CommentController::replyFormAccess

public CommentController::replyFormAccess(EntityInterface $entity, $field_name, $pid = NULL)

Access check for the reply form.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity this comment belongs to.

string $field_name: The field_name to which the comment belongs.

int $pid: (optional) Some comments are replies to other comments. In those cases, $pid is the parent comment's comment ID. Defaults to NULL.

Return value

\Drupal\Core\Access\AccessResultInterface An access result

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

File

core/modules/comment/src/Controller/CommentController.php, line 270

Class

CommentController
Controller for the comment entity.

Namespace

Drupal\comment\Controller

Code

public function replyFormAccess(EntityInterface $entity, $field_name, $pid = NULL) {
  // Check if entity and field exists.
  $fields = $this->commentManager->getFields($entity->getEntityTypeId());
  if (empty($fields[$field_name])) {
    throw new NotFoundHttpException();
  }

  $account = $this->currentUser();

  // Check if the user has the proper permissions.
  $access = AccessResult::allowedIfHasPermission($account, 'post comments');

  $status = $entity->{$field_name}->status;
  $access = $access->andIf(AccessResult::allowedIf($status == CommentItemInterface::OPEN)
    ->addCacheableDependency($entity));

  // $pid indicates that this is a reply to a comment.
  if ($pid) {
    // Check if the user has the proper permissions.
    $access = $access->andIf(AccessResult::allowedIfHasPermission($account, 'access comments'));

    /// Load the parent comment.
    $comment = $this->entityManager()->getStorage('comment')->load($pid);
    // Check if the parent comment is published and belongs to the entity.
    $access = $access->andIf(AccessResult::allowedIf($comment && $comment->isPublished() && $comment->getCommentedEntityId() == $entity->id()));
    if ($comment) {
      $access->addCacheableDependency($comment);
    }
  }
  return $access;
}
doc_Drupal
2016-10-29 08:50:34
Comments
Leave a Comment

Please login to continue.