1

In Drupal 8+, the "Add comment" form can be shown on a separate page by going to Manage fields for the content type that has comments and unchecking the box Show reply form on the same page as comments.

This shows an Add new comment link on the node (in this case, an article):

article with default display settings

The UX problem is that when you click the Add new comment link, you are taken to a new path (for example, comment/reply/node/2/comment#comment-form) but the Add new comment link is still there:

add new comment link on add new comment form

This is confusing because clicking the link merely returns you to the page you are already on, so it may appear that nothing has happened.

How can I remove this "Add new comment" link only on entities that have the comment form embedded (/comment/reply/entity/entity_id)?

2 Answers 2

1
/**
 * Implements hook_preprocess_HOOK().
 */
function mymodule_preprocess_links__node(&$variables) {
  if (\Drupal::routeMatch()->getParameter('field_name') === 'comment') {
    unset($variables['links']['comment-add']);
  }
}
0

Something like this should work (assuming you dont want it on the teaser too if its there):

function hook_preprocess_node(&$variables) {
  // Remove the "Add new comment" link on teasers or when the comment form is
  // displayed on the page.
  if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
    unset($variables['content']['links']['comment']['#links']['comment-add']);
  }
}
1
  • The screenshot shows the default view mode at /node/1. So (as I understand it) what makes this difficult to fix is that the default view mode is used for both /node/1 and /comment/reply/node/1. Feb 18 at 0:54

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.