Search code examples
multilingualdrupal-8

Filter Comments in the language of the node


I put the comments on nodes but I'm puzzled about the fact that the comments are not filtered by language. And I don't find a solution about that.

When I'm on a french page, I got the comments in french but also in chinese.

Also when you post a new comment on a chinese page, stating that the comment is in chinese, there's a redirection to the default language of the node (here in french). I fixed this problem with a hook...

But I still have a mixed language of contents whatever the language node is. I tried different hook (hook_comment_view_alter or hook_comment_load) and I see the comments before rendering but I'm not able to remove the comment in different language of the page I'm on. So is there a way to filter the comments on the basic Drupal8 comment system ?


Solution

  • My complete solution here :

    function hook_query_comment_filter_alter(Drupal\Core\Database\Query\AlterableInterface $query) {
        $clangcode = \Drupal::service('language_manager')->getCurrentLanguage(\Drupal\Core\Language\LanguageInterface::TYPE_CONTENT);
        $query->condition('langcode', $clangcode->getId());
    }
    

    You can strip the namespace with some "use".