0

How do I remove the "Log in to post comments" from the bottom of my content in Drupal 8 while keeping old comments visible to all and allowing existing registered users to still comment? Basically, I don't want anonymous users to find my login page, ever. These means no CSS hide or Javascript solutions (bots will still follow the link).

So, if comments exist on a node:

I want anonymous users to:

  1. See the comments as usual
  2. NOT see any sort of "add comment" link
  3. NOT see any sort of "login/register" link either

I want authenticated users to:

  1. See the comments as usual
  2. See "add/reply" comment links as usual

If comments DO NOT exist on a node:

I want anonymous users to:

  1. See NOTHING at the bottom of the node

I want authenticated users to:

  1. See "add comment" link as usual
2

You can do it using hook_preprocess_HOOK
The following hooks should be placed in YOURTHEME.theme file:

  1. Remove comment links from node for Anonymous user:

    /**
     * Implements hook_preprocess_HOOK().
     */
    function YOURTHEME_preprocess_links__node(&$variables) {
      if (\Drupal::currentUser()->isAnonymous()) {
        unset($variables['links']['comment-forbidden']);
      }
    }

  1. Remove links from the comments for Anonymous user:

/**
 * Implements hook_preprocess_HOOK().
 */
function YOURTHEME_preprocess_links__comment(&$variables) {
  if (\Drupal::currentUser()->isAnonymous()) {
    unset($variables['links']['comment-forbidden']);
  }
}

  • This did the trick. A few clarifications though in case a newbie like me comes across this post (correct me if I am wrong). First, this code should be placed in your theme's MYTHEME.theme file. Second, both functions should be named with your theme's name as the first bit: function MYTHEME_preprocess_link__node(&$variables) { – Tantalus Dec 2 at 3:06
  • Yes you are right , i will update my answer. – berramou Dec 2 at 9:21

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.