Arioch
Arioch

Reputation: 324

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 ?

Upvotes: 1

Views: 95

Answers (2)

Arioch
Arioch

Reputation: 324

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".

Upvotes: 2

Arioch
Arioch

Reputation: 324

I found the solution using hook_query_TAG_alter (so hook_query_comment_filter_alter for the comments).

see here : https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21database.api.php/function/hook_query_TAG_alter/8.3.x

Upvotes: 1

Related Questions