Remove comment link from node teaser I want to remove comment link (actually it is li element) from node teaser view on homepage and term listing. I want ways to do in controller/service or in template or if possible in admin.

I have created links--node.html.twig in my template and place the following code:

{% if links -%}
  {%- if heading -%}
    {%- if heading.level -%}
      &lt;{{ heading.level }}{{ heading.attributes }}&gt;{{ heading.text }}&lt;/{{ heading.level }}&gt;
    {%- else -%}
      &lt;h2{{ heading.attributes }}&gt;{{ heading.text }}&lt;/h2&gt;
    {%- endif -%}
  {%- endif -%}
  &lt;ul{{ attributes }}&gt;
    {%- for key, item in links -%}
      &lt;li{{ item.attributes.addClass(key|clean_class) }}&gt;
        {%- if item.link -%}
          {{ item.link }}
        {%- elseif item.text_attributes -%}
          &lt;span{{ item.text_attributes }}&gt;{{ item.text }}&lt;/span&gt;
        {%- else -%}
          {{ item.text }}
        {%- endif -%}
      &lt;/li&gt;
    {%- endfor -%}
  &lt;/ul&gt;
{%- endif %}


as here was told. But it did not help my as links go in loop. I could insert if condition. But I do not know variables. I tried dump item from loop but a page was loading without the end. I do not want to use twig in to search for needed item. Actually I need the right way(s) to remove comment link.  If I can check for key, item in links if item.name != "comment" it could be great. But I prefer do it in code like in proper way without hacks.
Firstly, dirty way. In the template links--node.html.twig change this line {%- for key, item in links -%} to {%- for key, item in links  if key != 'comment-add' and key != 'comment-forbidden' -%}. This is for removing 'comment-add' and 'comment-forbidden' link. If you also want to remove links count apend the line with and key != 'comment-comments'. A simplier way to achieve this is to replace all ifs with if 'comment' not in key. Twig's in.

The second is more right way. The point is in using the hook hook_node_links_alter(doc). This hook alter node links behaviour. There is a core comment module. It realy worth looking if you want to know how node comment works. It requires drupal 8 module knowledge. The service responsible for node comments links is comment.link_builder and defined in core/modules/comment/comment.services.yml. And while observing core/modules/comment/src/CommentLinkBuilder.php you can find that it add different links to render array and all logic. So comment is just a module. So back to hook. It alters node links and add comment links. So you need to remove it again. To do it you can define your own module to create a hook. In module directory you need to create MODULE.module file and place the following code there:



use Drupal\node\NodeInterface;


function kapusta_node_links_alter(array &amp;$links, NodeInterface $node, array &amp;$context) {
  unset($links['comment__comment']);
}


Here nothing special, just implementing hook_node_links_alter. It removes comment link.

The third way is more elegant but need more work. It is based on similar things as the second way. The point is to rewrite the service core/modules/comment/src/CommentLinkBuilder.php. For this you need to define your own module. Then you will create a service with the similar name to CommentLinkBuilder.php. The symfony (framework drupal is using) will rewrite the service from comment module. And you will get your profit. So firsly you duplicate the service. To do this you create MODULE.services.yml in your module folder. The file content is the following:

services:
  comment.link_builder:
    class: Drupal\kapusta\CommentLinkBuilder
    arguments: ['@current_user', '@comment.manager', '@module_handler', '@string_translation', '@entity.manager']


Where do I get this? From core/modules/comment/comment.services.yml. Just replaced class with my own. Then you create that class. You will get the file modules/custom/kapusta/src/CommentLinkBuilder.php with the following content:

&lt;?php

namespace Drupal\kapusta;

use Drupal\comment\CommentLinkBuilder as Base;
use Drupal\Core\Entity\FieldableEntityInterface;

class CommentLinkBuilder extends Base{
  public function buildCommentedEntityLinks(FieldableEntityInterface $entity, array &amp;$context) {
    return [];
  }
}


'kapusta` is my module name. So that's it.

P.S
How I found it. I debugged variables in the twig {%- for key, item in links -%}. I discovered tha value of key in each iteration. It were comment-* keys I wrote about in the beginning. Then I search the project files by comment-add and found CommentLinkBuilder. With xdebug I came to the hook. Then I realize that it is just simple module. Hope this helps.