Score:1

How to hide the "Add new comment" link on the add new comment page

cn flag

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

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

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']);
  }
}
cn flag
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`.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.