Problem/Motivation

The situation, I have a Drupal 8 multilingual node where Dutch is the main language and French is the other.
If I add a comment to the Dutch node translation and approve it, it will show. But it will also show in the French translation.
I think it is rather weird to see a mix of Dutch and French below a Dutch node.

P.S. as a quick workaround I created a view which shows me the comments based on the current node and filtered on a language. But it would be better that this approach is not necessary.

Proposed resolution

Add an option to select the language for show the comments.

Remaining tasks

Add test coverage.

User interface changes

A new option is added in the 'Manage display' tab for the comment which allow to config the language filter to show the comments.

API changes

The language_manager service is now injected to the CommentStorage class.

Data model changes

A new configuration named language_filter is now available to store the language configuration for display the comments.

Issue fork drupal-2751267

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

yvesvanlaer created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ogggg’s picture

I can confirm this issue.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tvoesenek’s picture

Status: Active » Needs review
StatusFileSize
new9.88 KB

This patch adds language filter options to the comment formatter, allowing you to configure in which languages comments should be shown. This can be a fixed language and/or the current interface language.

Status: Needs review » Needs work
tvoesenek’s picture

Status: Needs work » Needs review
StatusFileSize
new10.48 KB

Previous patch didn't work correct when no language filter was set, so i've fixed that. Also fixed missing schema settings.

Status: Needs review » Needs work
seanb’s picture

Quick first review.

  1. +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
    @@ -37,9 +39,10 @@ class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryP
    +        'view_mode' => 'default',
    +        'pager_id' => 0,
    +        'language_filter' => NULL,
    +      ] + parent::defaultSettings();
    

    This indentation seems wrong. I also think the test is failing because 'language_filter' => NULL, should be 'language_filter' => [],

  2. +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
    @@ -167,7 +188,23 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
    +          $settings = $this->getSetting('language_filter');
    +          if (!empty($settings)) {
    +            foreach ($settings as $language) {
    

    Are you fetching languages? I guess naming that $languages instead of $settings. And I even think you are fetching language IDs so maybe $language_ids.

  3. +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
    @@ -167,7 +188,23 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
    +                continue;
    

    When can $language be empty?

  4. +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
    @@ -207,11 +244,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
    +          '#comment_type' => $this->getFieldSetting('comment_type'),
    +          '#comment_display_mode' => $this->getFieldSetting('default_mode'),
    +          'comments' => [],
    +          'comment_form' => [],
    +        ];
    

    This indentation also seems wrong.

seanb’s picture

Issue tags: +Needs tests

And it needs more tests.

tvoesenek’s picture

StatusFileSize
new9.92 KB

Thanx for the feedback, I've updated the patch. Still needs tests though.

seanb’s picture

Status: Needs work » Needs review

Could you provide an interdiff for your changes? I think you fixed the tests but most of the other feedback was not addressed. Let's see what the testbot thinks as well.

seanb’s picture

Status: Needs review » Needs work

Yay! Back to needs work for #11 and more tests.

tvoesenek’s picture

StatusFileSize
new9.77 KB
new2.73 KB

Updated the patch in #13 with the feedback I've missed form #11 and added an interdiff between patches of #9 and the latest version of the patch.
Leaving the issue on 'Needs work' because it still needs tests.

dravenk’s picture

Assigned: Unassigned » dravenk

I confirmed this bug with simplytest.me. I will try to write this test.

adriancid’s picture

I have installed a multilingual Drupal 8.5 site and I can reproduce this issue, but when I add a comment in a translation the comment attached to the node with the default language.

adriancid’s picture

Issue summary: View changes
StatusFileSize
new71.63 KB
new59.6 KB

Oh sorry, it seems that was a cache problem, I can confirm that the issue exists and that patch solve the problem.

Before:

After:

andypost’s picture

+++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
@@ -167,7 +188,24 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
+          $language_ids = $this->getSetting('language_filter');

this settings needs default value for BC, and test to cover

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

finne’s picture

Patch #16 did not apply to D8.6.0. Rerolled.

dravenk’s picture

Assigned: dravenk » Unassigned

I'm sorry I forgot this issue

drididev’s picture

There is a solution without patch.
You have to use ‘hook_entity_type_alter’ to override the storage class of comment.

function module-name_entity_type_alter(&$entity_types) {
   $entity_types['comment']->setStorageClass('Drupal\module-name\customCommentStorage');
}

And in the custom storage class extend CommentStorage to do not override all the function . only override the needed one.
In your case override ‘loadThread’ use the parent class function code and add language condition to the query.


namespace Drupal\module-name;

use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\CommentStorage;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class customCommentStorage extends CommentStorage {

 public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
   parent::__construct($entity_info, $database, $entity_manager, $current_user, $cache, $language_manager);
 }

 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
   return new static(
     $entity_info,
     $container->get('database'),
     $container->get('entity.manager'),
     $container->get('current_user'),
     $container->get('cache.entity'),
     $container->get('language_manager')
   );
 }

 public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
   $langCode = $this->languageManager->getCurrentLanguage()->getId();
   $query = $this->database->select('comment_field_data', 'c');
   $query->addField('c', 'cid');
   $query
     ->condition('c.entity_id', $entity->id())
     ->condition('c.entity_type', $entity->getEntityTypeId())
     ->condition('c.field_name', $field_name)
     ->condition('c.langcode', $langCode) // this is the language condition
     ->condition('c.default_langcode', 1)
     ->addTag('entity_access')
     ->addTag('comment_filter')
     ->addMetaData('base_table', 'comment')
     ->addMetaData('entity', $entity)
     ->addMetaData('field_name', $field_name);

   if ($comments_per_page) {
     $query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')
       ->limit($comments_per_page);
     if ($pager_id) {
       $query->element($pager_id);
     }

     $count_query = $this->database->select('comment_field_data', 'c');
     $count_query->addExpression('COUNT(*)');
     $count_query
       ->condition('c.entity_id', $entity->id())
       ->condition('c.entity_type', $entity->getEntityTypeId())
       ->condition('c.field_name', $field_name)
       ->condition('c.langcode', $langCode)  // this is the language condition
       ->condition('c.default_langcode', 1)
       ->addTag('entity_access')
       ->addTag('comment_filter')
       ->addMetaData('base_table', 'comment')
       ->addMetaData('entity', $entity)
       ->addMetaData('field_name', $field_name);
     $query->setCountQuery($count_query);
   }

   if (!$this->currentUser->hasPermission('administer comments')) {
     $query->condition('c.status', CommentInterface::PUBLISHED);
     if ($comments_per_page) {
       $count_query->condition('c.status', CommentInterface::PUBLISHED);
     }
   }
   if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
     $query->orderBy('c.cid', 'ASC');
   }
   else {
     $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
     $query->orderBy('torder', 'ASC');
   }
   $cids = $query->execute()->fetchCol();
   $comments = [];
   if ($cids) {
     $comments = $this->loadMultiple($cids);
   }
   return $comments;
 }
}
Phil_b’s picture

Hi,
we faced this problem too and found a working solution without any kind of patch. Maybe this will work for others too.

You have to use ‘hook_entity_type_alter’ to override the storage class of comment.

function ​ YOUR_MODULE_entity_type_alter(&$entity_types) {
  $entity_types['comment']->setStorageClass('Drupal\YOUR_MODULE\customCommentStorage');
}

And in the custom storage class extend CommentStorage to not override all the functions, only override the needed one.
In our case we override ‘loadThread’. We use the parent class function code and add language condition to the query.
That´s it.

namespace ​ Drupal\YOUR_MODULE;

use ​ Drupal\comment\CommentInterface;
use ​ Drupal\comment\CommentManagerInterface;
use ​ Drupal\comment\CommentStorage;
use ​ Drupal\Core\Cache\CacheBackendInterface;
use ​ Drupal\Core\Database\Connection;
use ​ Drupal\Core\Entity\EntityManagerInterface;
use ​ Drupal\Core\Entity\EntityTypeInterface;
use ​ Drupal\Core\Entity\EntityInterface;
use ​ Drupal\Core\Session\AccountInterface;
use ​ Drupal\Core\Language\LanguageManagerInterface;
use ​ Symfony\Component\DependencyInjection\ContainerInterface;

class ​ customCommentStorage ​ extends ​ CommentStorage {
  public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
    parent::__construct($entity_info, $database, $entity_manager, $current_user , $cache, $language_manager);
  }
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
    return new static(
      $entity_info,
      $container->get('database'),
      $container->get('entity.manager'),
      $container->get('current_user'),
      $container->get('cache.entity'),
      $container->get('language_manager')
    );
  }
  public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
    $langCode = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $query = $this->database->select('comment_field_data', 'c');
    $query->addField('c', 'cid');
    $query
      ->condition('c.entity_id', $entity->id())
      ->condition('c.entity_type', $entity->getEntityTypeId())
      ->condition('c.field_name', $field_name)
      ->condition('c.langcode', $langCode)
      ->condition('c.default_langcode', 1)
      ->addTag('entity_access')
      ->addTag('comment_filter')
      ->addMetaData('base_table', 'comment')
      ->addMetaData('entity', $entity)
      ->addMetaData('field_name', $field_name);

    if ($comments_per_page) {
      $query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')
        ->limit($comments_per_page);
      if ($pager_id) {
        $query->element($pager_id);
      }

      $count_query = $this->database->select('comment_field_data', 'c');
      $count_query->addExpression('COUNT(*)');
      $count_query
        ->condition('c.entity_id', $entity->id())
        ->condition('c.entity_type', $entity->getEntityTypeId())
        ->condition('c.field_name', $field_name)
        ->condition('c.langcode', $langCode)
        ->condition('c.default_langcode', 1)
        ->addTag('entity_access')
        ->addTag('comment_filter')
        ->addMetaData('base_table', 'comment')
        ->addMetaData('entity', $entity)
        ->addMetaData('field_name', $field_name);
      $query->setCountQuery($count_query);
    }

    if (!$this->currentUser->hasPermission('administer comments')) {
      $query->condition('c.status', CommentInterface::PUBLISHED);
      if ($comments_per_page) {
        $count_query->condition('c.status', CommentInterface::PUBLISHED);
      }
    }
    if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
      $query->orderBy('c.cid', 'ASC');
    }
    else {
      // See comment above. Analysis reveals that this doesn't cost too
      // much. It scales much much better than having the whole comment
      // structure.
      $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
      $query->orderBy('torder', 'ASC');
    }

    $cids = $query->execute()->fetchCol();

    $comments = [];
    if ($cids) {
      $comments = $this->loadMultiple($cids);
    }

    return $comments;
  }
matsbla’s picture

Should this behavior maybe be optional? If you have a site in Danish, Norwegian and Swedish, quite similar languages, you might want to allow mixed languages in comments under node. It would be good to be able to choose if it should be filtered by language or not.

tim_dj’s picture

Patch #22 seems to working fine. But when I try to get a comment count:

$node->get('field_comments')->comment_countWill give total comments for all languages when viewing original translation and will return 0 when viewing translated node

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nanak’s picture

Patch modified to work with 8.7.x

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

avpaderno’s picture

Status: Needs work » Needs review
mrinalini9’s picture

Rerolled patch #29 for 9.1.x branch, please review.

huijse’s picture

This patch only works when reinstalling the comment module right? This is a problem when there is already comment content in the db.
Perhaps there should be a db update as well?

berdir’s picture

No, this patch does not affect how comments are stored, only how they are displayed. It does introduce settings that you need to configure.

huijse’s picture

Here is the form element that you need to configure:

$element['language_filter'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Filter by language'),
      '#options' => $this->getLanguageOptions(),
      '#default_value' => $this->getSetting('language_filter'),
      '#description' => $this->t('Show comments in the selected languages. If none selected, all comments will be showed.'),
    ];

And this storage needs to be filled:

field.formatter.settings.comment_default
  language_filter:
      type: sequence
      label: 'Filter comments by language'
      sequence:
        type: string
        label: 'Language'
samiullah’s picture

Not able to apply patch#33 do i need to follow any steps for that

huijse’s picture

This was a solution for me together with applying patch #80 from:
https://www.drupal.org/project/drupal/issues/2751269

/**
 * Implements hook_preprocess_comment().
 * Makes a flag for filtering on language. Custom solution for this issue:
 */

function MODULE_NAME_preprocess_comment(&$variables, $hook) {
  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $langcode = $variables['comment']->get('langcode')->getValue()[0]['value'];

  if ($language == $langcode) {
    $langfilter = TRUE;
  }
  else {
    $langfilter = FALSE;
  }

  unset($variables['content']['langcode']);
  $variables['langfilter'] = $langfilter;
}

And than the comment.html.twig wrap the flag around the template:

{% if langfilter == true %}
{%
  set classes = [
    'comment',
 ................................................................................
    {{ content }}
  </div>
</article>

{% endif %}

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

szeidler’s picture

Thanks for the patches. I'm also struggling with the condition mentioned in #27. In case only the translation has comments, but the original language not - it will fail.

I'm wondering if we need this condition at all? Later on it's checked anyhow, if there are comments?

$comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'), $langcodes);
        if ($comments) {

Or do I miss anything?

ivoo56’s picture

Hello, what about Drupal 8.9.13. Would the patch work?

cptX’s picture

Hi, I'm creating a new multilingual site based on D9. I was expecting the multilingual capabilities of Drupal to be much more enhanced and mature already. The last site I have created was based on D6 and at that time multilingual system was not so robust. Honestly for my new site I selected Drupal exactly for it's multilingual capability but to my surprise comment filtering per language is not supported by default. This is major problem because if I have a node translated in 2 languages there is no point showing the comments of one language to the page of the other language. So my questions are:

1. Is this going to be impemented in the core in the future?
2. How can I apply this patch without affecting the core? In my older drupal site I was forced to patch things in the core and that was a terrible experience in every update. I don't want to touch the core this time...
3. If I patch the core, what will happen in the future if this will come in the official release? If the site has multiple comments already stored in the database with the settings of this patch will it be possible to upgrade to the official version?
4. What other options do I have to achieve comment filtering per language at the moment?

As you understand this issue is so major for me that actually blocked my development. I had the site almost ready for production until I realized the issue...

I see many references in the internet for a checkbox called "Filter comments per language" but I cannot find it anyware in D9. Was it only in D7 or D6? If yes why this functionality was removed in D8/D9?

(After all is it better to have entity translation instead of one node per language? In the second case comments of that node are independent of the first. I see some positives and some negatives in this implementation. Negative is that you don't have the old text visible in front of you during translation and there is no single reference to one node only but in the other hand it simplifies things like comments, content available in every language, content access per language etc...)

cptX’s picture

I have implemented suggestion #38

Now comments are filtered based on their declared language. Although, in every node the number of available comments reported is the total and this can confuse users (eg. maybe in the translated language there are 0 comments but user sees that comments exist but they are in the other language)

My major issue now is the redirect. Everytime somebody writes a comment in the translated page gets redirected to the non translated page.

cptX’s picture

Created a new topic here to raise general awareness for all these issues the comment module has https://www.drupal.org/project/drupal/issues/3196886

andypost’s picture

cptX’s picture

Patch #33 works for filtering the comments per language but comment permalinks in the translated pages are not working -> giving page not found.
In the non translated page a random permalink gives
base_url/comment/224#comment-224 which works
and in the translated page
base_url/gr/comment/226#comment-226 which gives "page not found"

Patch should take care to produce a valid permalink including the language code in case the admin has selected to filter comments per langugage.

Also, when viewing a node summary in the home page in the main language you see the total of all comments from both languages and in the translated home page you don't see any comments counter at all.

andypost’s picture

cptX’s picture

Regarding comments permalinks I did some small progress and now they include the correct language code in the url and not producing "page not found" errors.
I did the following changes in Drupal 9.1.2 core:
1. in file /core/modules/comment/src/Entity/Comment.php @ function permalink() @ line 216:

- $uri = $this->toUrl();
+ $uri = $this->toUrl('canonical', ['language' => \Drupal::languageManager()->getCurrentLanguage()]);

2. in file /core/modules/comment/src/Controller/CommentController.php @ function commentPermalink() @ line 145:

- $subrequest_url = $entity->toUrl()->setOption('query', ['page' => $page])->toString(TRUE);
+ $subrequest_url = $entity->toUrl('canonical', ['language' => \Drupal::languageManager()->getCurrentLanguage()])->setOption('query', ['page' => $page])->toString(TRUE);

Please review my solution and test it.

After the above, remaining issues currently are:
1. Clicking on a permalink still doesn't land to the correct page which includes the corresponding comment because the pager is calculated including all the comments and not the filtered ones according to the filter patch #33 introduces
I think the following functions are responsible for this:
/core/modules/comment/src/CommentStorage.php -> function getDisplayOrdinal() and function loadThread()
2. Calculating and showing the correct sum of comments in every node according to the language filter applied.

cptX’s picture

Regarding comment count looks like it is stored in the database with this shema (from file comment.install)

    'primary key' => ['entity_id', 'entity_type', 'field_name'],
    'indexes' => [
      'last_comment_timestamp' => ['last_comment_timestamp'],
      'comment_count' => ['comment_count'],
      'last_comment_uid' => ['last_comment_uid'],
    ]

This parameter then is called by function buildCommentedEntityLinks() in file CommentLinkBuilder.php

The fact that this number is stored in the DB complicates things too much. How are we going to change the shown number of comments per language if the only parameter stored in the database is the total number of comments? Should we run a query every time? Isn't that too expensive? Should we store number of comments per language in the database? If so this will break forward and backward compatibility.

andypost’s picture

@cptX it becomes more tricky when you enable tracker and history modules, moreover it affects forum...

cptX’s picture

@andypost I have installed forum and looks ok at the moment. The reason it works fine is that I have separate forums for every language so all the comments are the total number in every language. If every topic had mixed language comments yes that would be an issue there as well.

Currently I see more issues with the node comments. I haven't tried history yet.

What do you suggest then for producing the correct comments count? Should we store in the database the comment count per language?
Everything in this thread is moving really slow. Is there any chance we could work on a solution more actively?

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative
+++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
@@ -170,7 +191,23 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
+              if (!$language_id) {
+                continue;
+              }

should we just array_filter on the setting and avoid this?

Needs work for tests

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bigboy’s picture

I've applied these patches to multilingual website:

  1. https://www.drupal.org/project/drupal/issues/2751269#comment-14305602 : https://www.drupal.org/files/issues/2021-11-29/2751269-106.patch
  2. https://www.drupal.org/project/drupal/issues/2958935#comment-13884161 : https://www.drupal.org/files/issues/2020-10-27/2958935-31.patch
  3. https://www.drupal.org/project/drupal/issues/2751267#comment-13729678 : https://www.drupal.org/files/issues/2020-07-02/comments-comments_are_not...

It was some time ago, I didn't remember some details, but it was hard as truck... Anyway, everything eventually started to work.

And now I wanted to reproduce my steps on another website. Applied all mentioned above patches... and nothing changed.

Problems:

  1. Comments displaying in any language on all pages.
  2. When deleting comment on non-default language page, WSOD appears with this error:

Redirects to external URLs are not allowed by default, use \Drupal\Core\Routing\TrustedRedirectResponse for it

Differences between two sites:

  1. 7 languages, path prefix detection (subfolders), English is default;
  2. 2 languages, different domains, English is not default.

Further investigation brought me to this solution:

  1. Enable Chaos tools blocks.
  2. On manage node display settings deactivate Comments (move it to the bottom).
  3. Enable comments block.

This solved the problem with displaying comments in the correct language. But didn't solve redirect error (problem #2 in the first list).

andypost’s picture

@bigboy thank you for feedback - as I got the difference in which type of language is used in redirect leads to wsod.

Required tests needs to cover 2+ languages, all forms of comment entity's forms

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dietric@gmail.com made their first commit to this issue’s fork.

urashima82’s picture

Hello, I'm sending a new patch because I was facing an issue when I tried to display comments. Due to a strange behavior of the comment_count property, the comments are not always shown.

I replace that by an entity query and it works.

To respond to @bigboy, I was facing the same redirect problem. To solve that, I used the hook_form to add my custom submit method.
Here is en example, feel free to improved it :

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $comment_forms = [
    'comment_comment_form',
    'comment_comment_edit_form',
    'comment_comment_delete_form',
  ];
  if (in_array($form_id, $comment_forms)) {
    $form['actions']['submit']['#submit'][] = 'my_module_comment_fix_form_submit';
  }
}

function my_module_comment_fix_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $route_params = \Drupal::routeMatch()->getParameters();
  if ($route_params->has('entity')) {
    $node = \Drupal::routeMatch()->getParameter('entity');
  }
  else {
    // Get the comment entity.
    $comment = \Drupal::routeMatch()->getParameter('comment');
    // Get the node entity.
    $node = $comment->getCommentedEntity();
  }
  // Get translated url.
  $url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $node->id()]);
  $form_state->setRedirectUrl($url);
}
anchal_gupta’s picture

StatusFileSize
new39.66 KB
new1.96 KB

I have uploaded the patch.
Fixed CFF
Please review

pcambra’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

erwangel’s picture

Thanks to cptX (#58), I have no more "page not found" errors. My site is multilingual but comments translation is not enabled. It was a disaster for SEO as every permalink was giving a "page not found" when not in the default interface language. I don't see a solution for this in the last patch (#62).

idebr made their first commit to this issue’s fork.

idebr’s picture

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

larskleiner’s picture

Can you please post the link to the merge request here, I can't find it I'm afraid.

In the meantime, I've got a patch re-rolled for Drupal 11.4.

avpaderno’s picture

@larskleiner There is no merge request. There is a issue fork, but nobody created a merge request out of that.

avpaderno’s picture

There are merge conflicts to resolve.