2

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)?

3 Answers 3

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. Commented Feb 18, 2023 at 0:54
0

To hide the link at the bottom of a forum topic node:

/** * Implements hook_node_links_alter(). */ function MODULE_node_links_alter(array &$links, NodeInterface $node, array &$context): void { // Remove the "Add new comment" link from the node links. unset($links['comment__comment_forum']['#links']['comment-add']); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.