0

I am trying to hide the comment box for anonymous users. Anonymous users should only be able to view the comments. How can I hide the comment form?

enter image description here

2

The comment module provides a permission, "Post comments" (post comments). Before using any code option, you should remove that permission for anonymous users.

0

I added this inside my hook and it worked:

if($form_id == 'comment_comment_form') 
    {
        if (\Drupal::currentUser()->isAnonymous()) 
        {
           $form['comment_body']['#access'] = FALSE;
         }

     }
1
  • If the default "Post comments" permission isn't enough to handle your needs, you should consider creating your own permission in a custom module and checking if the user has the permission (vs checking for a specific role). It's pretty easy to add a permission, see: drupal.stackexchange.com/a/214501/48114 – sonfd Jan 20 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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