1

I would like to disable reply to comment for anonymous user.

I saw on sereval topics that you have to unset

$variables['content']['links']['comment']['#links']['comment-reply']

So I ended up adding a preprocess to the .theme file (bartik.theme since I'm using Bartik. Btw I'm using Drupal 8.5.3)

function bartik_preprocess_comment(&$variables) {
  if (\Drupal::currentUser()->isAnonymous()) {
    unset($variables['content']['links']['comment']['#links']['comment-reply']);
  }
}

Despite that preprocess, I still have the reply link when I'm anonymous. Does anyone has an idea about what's going on ?

1 Answer 1

2

You can use hook_comment_links_alter() rather than THEME_preprocess_comment() .Where as hook_comment_links_alter() will render all the comments links before build:

function YOURMODULE_comment_links_alter(array &$links, Drupal\comment\CommentInterface $entity, array &$context) {
  if (\Drupal::currentUser()->isAnonymous()) {
    unset($links['comment']['#links']['comment-reply']);
  }
}
3
  • Any clue about why the solution I used was not working ?
    – Tibo
    May 22, 2018 at 12:25
  • I suggest using #access = false instead of unsett $links['comment']['#links']['comment-reply']['#access'] = FALSE;instead of unset. Also you might want to implement some sort of access preventing Anon users from replying .. either access hook or form submit validation hook/event
    – awm
    May 22, 2018 at 21:22
  • @Tibo In your solution, it's only getting all the comment content with already rendered links. So in that hook links array will not be present and links will come as rendered under comment.
    – Ajay Reddy
    May 23, 2018 at 5:52

Your Answer

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

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