blog-banner

Removing "Login or Register to Post Comments" Link from Comment View

  • Drupal 7
  • Drupal Planet
  • HOOK_COMMENT_VIEW_ALTER()

Login or Register to Post Comments

Drupal provides a handful of settings to format the comment listing. One thing that we found very irrelevant in the comment view is the link item "Login or register to post comments" especially when the comments are styled to display as flat items. Let's take the snapshot below from the drupal.org issue queue.


[[{"type":"media","view_mode":"media_original","fid":"102","attributes":{"alt":"","class":"media-image","typeof":"foaf:Image"}}]]

It is obvious that the login and register link in every comment is not needed. Fortunately, Drupal 7 provides a hook to alter comment view output hook_comment_view_alter(&$build)

  1. /**
  2.  * Implementation of hook_comment_view_alter().
  3.  */
  4. function kf_module_comment_view_alter(&$build) {
  5.   if (($build['#node']->type == 'blog') && isset($build['links']['comment']['#links']['comment_forbidden'])) {
  6.     unset($build['links']['comment']['#links']['comment_forbidden']);
  7.   }
  8. }

These five lines of code shared above do the trick of removing unwanted stuff from the comment view.

Get awesome tech content in your inbox