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.
| Comment | File | Size | Author |
|---|---|---|---|
| #69 | drupal-core-comments-not-filtered-by-language-2751267-69.patch | 10.31 KB | larskleiner |
Issue fork drupal-2751267
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
Comment #3
ogggg commentedI can confirm this issue.
Comment #7
tvoesenek commentedThis 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.
Comment #9
tvoesenek commentedPrevious patch didn't work correct when no language filter was set, so i've fixed that. Also fixed missing schema settings.
Comment #11
seanbQuick first review.
This indentation seems wrong. I also think the test is failing because
'language_filter' => NULL,should be'language_filter' => [],Are you fetching languages? I guess naming that
$languagesinstead of$settings. And I even think you are fetching language IDs so maybe$language_ids.When can
$languagebe empty?This indentation also seems wrong.
Comment #12
seanbAnd it needs more tests.
Comment #13
tvoesenek commentedThanx for the feedback, I've updated the patch. Still needs tests though.
Comment #14
seanbCould 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.
Comment #15
seanbYay! Back to needs work for #11 and more tests.
Comment #16
tvoesenek commentedUpdated 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.
Comment #17
dravenkI confirmed this bug with simplytest.me. I will try to write this test.
Comment #18
adriancidI 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.
Comment #19
adriancidOh sorry, it seems that was a cache problem, I can confirm that the issue exists and that patch solve the problem.
Before:
After:
Comment #20
andypostthis settings needs default value for BC, and test to cover
Comment #22
finnePatch #16 did not apply to D8.6.0. Rerolled.
Comment #23
dravenkI'm sorry I forgot this issue
Comment #24
drididev commentedThere is a solution without patch.
You have to use ‘hook_entity_type_alter’ to override the storage class of comment.
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.
Comment #25
Phil_bHi,
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.
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.
Comment #26
matsbla commentedShould 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.
Comment #27
tim_djPatch #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 nodeComment #29
nanakPatch modified to work with 8.7.x
Comment #32
avpadernoComment #33
mrinalini9 commentedRerolled patch #29 for 9.1.x branch, please review.
Comment #34
huijse commentedThis 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?
Comment #35
berdirNo, this patch does not affect how comments are stored, only how they are displayed. It does introduce settings that you need to configure.
Comment #36
huijse commentedHere is the form element that you need to configure:
And this storage needs to be filled:
Comment #37
samiullah commentedNot able to apply patch#33 do i need to follow any steps for that
Comment #38
huijse commentedThis was a solution for me together with applying patch #80 from:
https://www.drupal.org/project/drupal/issues/2751269
And than the comment.html.twig wrap the flag around the template:
Comment #40
szeidlerThanks 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?
Or do I miss anything?
Comment #41
ivoo56 commentedHello, what about Drupal 8.9.13. Would the patch work?
Comment #42
cptX commentedHi, 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...)
Comment #43
cptX commentedI 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.
Comment #44
cptX commentedCreated a new topic here to raise general awareness for all these issues the comment module has https://www.drupal.org/project/drupal/issues/3196886
Comment #45
andypostComment #46
cptX commentedPatch #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.
Comment #47
andypostHere's related to permalinks
Comment #48
cptX commentedRegarding 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:
2. in file /core/modules/comment/src/Controller/CommentController.php @ function commentPermalink() @ line 145:
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.
Comment #49
cptX commentedRegarding comment count looks like it is stored in the database with this shema (from file comment.install)
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.
Comment #50
andypost@cptX it becomes more tricky when you enable tracker and history modules, moreover it affects forum...
Comment #51
cptX commented@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?
Comment #54
larowlanshould we just array_filter on the setting and avoid this?
Needs work for tests
Comment #57
bigboy commentedI've applied these patches to multilingual website:
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:
Redirects to external URLs are not allowed by default, use \Drupal\Core\Routing\TrustedRedirectResponse for itDifferences between two sites:
Further investigation brought me to this solution:
This solved the problem with displaying comments in the correct language. But didn't solve redirect error (problem #2 in the first list).
Comment #58
andypost@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
Comment #61
urashima82 commentedHello, 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 :
Comment #62
anchal_gupta commentedI have uploaded the patch.
Fixed CFF
Please review
Comment #63
pcambraComment #64
needs-review-queue-bot commentedThe 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.)
Comment #65
erwangel commentedThanks 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).
Comment #67
idebr commented#62 is now available as a merge request
Comment #69
larskleiner commentedCan 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.
Comment #70
avpaderno@larskleiner There is no merge request. There is a issue fork, but nobody created a merge request out of that.
Comment #72
avpadernoThere are merge conflicts to resolve.