public function CommentAccess::replyFormAccess in Comment Permissions 8
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.
File
- src/
Access/ CommentAccess.php, line 85
Class
- CommentAccess
- Comment types additional overrides for access checks.
Namespace
Drupal\comment_perm\AccessCode
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;
$comment_type = $entity
->get($field_name)
->getSettings()['comment_type'];
// Check if the user has the proper permissions.
$access = AccessResult::allowedIf($this
->accessPostComment($account, $comment_type));
// If commenting is open on the entity.
$status = $entity->{$field_name}->status;
$access = $access
->andIf(AccessResult::allowedIf($status == CommentItemInterface::OPEN)
->addCacheableDependency($entity))
->andIf(AccessResult::allowedIf($entity
->access('view')));
// $pid indicates that this is a reply to a comment.
if ($pid) {
// Check if the user has the proper permissions.
$access = $access
->andIf(AccessResult::allowedIf($this
->accessComment($account, $comment_type)));
// Load the parent comment.
$comment = $this->entityTypeManager
->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;
}