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 if
s 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) {
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:
<?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 &$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.