Score:0

Display comments only for users with "administer comments" permission

id flag

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;
  }
}
Score:0
id flag

Solution below:

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

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.