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 -%}
      <{{ heading.level }}{{ heading.attributes }}>{{ heading.text }}</{{ heading.level }}>
    {%- else -%}
      <h2{{ heading.attributes }}>{{ heading.text }}</h2>
    {%- endif -%}
  {%- endif -%}
  <ul{{ attributes }}>
    {%- for key, item in links -%}
      <li{{ item.attributes.addClass(key|clean_class) }}>
        {%- if item.link -%}
          {{ item.link }}
        {%- elseif item.text_attributes -%}
          <span{{ item.text_attributes }}>{{ item.text }}</span>
        {%- else -%}
          {{ item.text }}
        {%- endif -%}
    {%- endfor -%}
{%- 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.

  • Sorry but this would take a large chunk of the documentation to explain, it's going to be too broad for a simple Q+A. The docs for render arrays are here: drupal.org/docs/8/api/render-api/render-arrays, the theming guide is here: drupal.org/docs/8/theming, the Twig docs are here: drupal.org/docs/8/theming/twig. Those and their sub-pages are all pretty much required reading before you get to development. If you have a specific requirement just ask a separate question with the details. But instead of asking how to edit business logic, which is ambiguous, be...
    – Clive
    Feb 21, 2018 at 9:38
  • ...very clear on exactly what you want to change and when/where - it'll make it easier to provide an answer that doesn't need to explain all of the over-arching concepts in full to be understood. Many thanks
    – Clive
    Feb 21, 2018 at 9:39
  • @Clive so the first question was how to remove a comment link from node teaser (nodes on homepage) Feb 21, 2018 at 9:41
  • The docs cover that in quite broad depth, where are you stuck exactly? What does your current code attempt look like? If you could edit the post to remove the broad questions and focus on the specifics that would be great. A quick search for "drupal 8 remove comment link from node teaser" yielded this in the first couple of results: stackoverflow.com/questions/35680997/…, that's a good place to start your research
    – Clive
    Feb 21, 2018 at 10:25
  • @Clive updated the question Feb 21, 2018 at 12:14

2 Answers 2


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 &$links, NodeInterface $node, array &$context) {

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:

    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:


namespace Drupal\kapusta;

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

class CommentLinkBuilder extends Base{
  public function buildCommentedEntityLinks(FieldableEntityInterface $entity, array &$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.


Great explanation, what I did was simpler and dirtier by putting in the css display: none. But I should try your suggestion. What I think drupal will be necessary to separate the types of links to be able to use them in different ways in different places. I think the design is for a web design older than how the card links is implemented which is a more modern design, and how it is implemented inside the article are different.

Not the answer you're looking for? Browse other questions tagged or ask your own question.