In hook_node_view_alter, I'm trying to restrict the display of comments to users who have the "administer comments" permission.

So far I have this but it is not working.

if (isset($build['comment'])) {
  if (!\Drupal::currentUser()->hasPermission('administer comments')) {
    $build['comment']['#access'] = FALSE;

1 Answer 1


Solution below:

$build['comment']['#access'] = Drupal::currentUser()->hasPermission('administer comments');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.