Problem/Motivation

It is impossible to add a new translation in case the user wants to keep the same settings for comments across all translations of the node.

Steps to reproduce

  1. Install standard
  2. Install content translation
  3. Add language (in my case Polish)
  4. Go to content translation settings and make Article translatable. Only check Title and Body field, unselect the rest, especially the Comments field. Also select "Hide non translatable fields on translation forms"
  5. Create a node in default language (English)
  6. Go to the Translate tab and add Polish translation.
  7. Fill the form and submit.
  8. "Non-translatable fields can only be changed when updating the original language."

Proposed resolution

CommentFieldItemList needs to implement it's own hasAffectingChanges that only check if status property has changed. If not it will check all properties, included the computed once like:

  • status
  • cid
  • last_comment_timestamp
  • last_comment_name
  • last_comment_uid
  • comment_count

And these properties will all be set to 0 until the new entity revision has been saved.

hasAffectingChanges in FieldItemList simply check !$this->equals($original_items);
https://git.drupalcode.org/project/drupal/-/blob/8.8.x/core/lib/Drupal/C...

However in the hasAffectingChanges doc it is written:

/**
* Determines whether the field has relevant changes.
*
* This is for example used to determine if a revision of an entity has
* changes in a given translation. Unlike
* \Drupal\Core\Field\FieldItemListInterface::equals(), this can report
* that for example an untranslatable field, despite being changed and
* therefore technically affecting all translations, is only internal metadata
* or only affects a single translation.

Issue fork drupal-3015675

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

zaporylie created an issue. See original summary.

matsbla’s picture

Issue summary: View changes

To reproduce it I also had to select "Hide non translatable fields on translation forms" so I added that to the steps to reproduce.

Still I'm not really sure what you try to achieve. What do you mean with "keep the same settings for comments across all translations of the node"? What settings are you talking about?

Do you want to make the comments non-translatable? In that case, maybe instead make the comment type you use non-translatable.

zaporylie’s picture

Thanks for updating issue summary @matsbla.

Please note that "Comments" property, that can be found in /admin/config/regional/content-language under Content refers to "Comment Settings" section in the Node edit form (Open/Closed/Hidden) not to Comment entity.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

matsbla’s picture

StatusFileSize
new820 bytes

I think CommentFieldItemList needs to implement it's own hasAffectingChanges that only check if status property has changed. If not it will check all properties, included the computed once like:

  • status
  • cid
  • last_comment_timestamp
  • last_comment_name
  • last_comment_uid
  • comment_count

And these properties will all be set to 0 until the new entity revision has been saved.

hasAffectingChanges in FieldItemList simply check !$this->equals($original_items);
https://git.drupalcode.org/project/drupal/-/blob/8.8.x/core/lib/Drupal/C...
However in the hasAffectingChanges doc it is written:

/**
* Determines whether the field has relevant changes.
*
* This is for example used to determine if a revision of an entity has
* changes in a given translation. Unlike
* \Drupal\Core\Field\FieldItemListInterface::equals(), this can report
* that for example an untranslatable field, despite being changed and
* therefore technically affecting all translations, is only internal metadata
* or only affects a single translation.

https://git.drupalcode.org/project/drupal/-/blob/8.8.x/core/lib/Drupal/Core/Field/FieldItemListInterface.php#L280-288

matsbla’s picture

Status: Active » Needs review
matsbla’s picture

Component: content_translation.module » comment.module

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

matsbla’s picture

StatusFileSize
new1.82 KB
new2.63 KB

Adding test.

The last submitted patch, 9: 3015675-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

matsbla’s picture

Issue tags: +Bug Smash Initiative
matsbla’s picture

jungle’s picture

Status: Needs review » Needs work
StatusFileSize
new149.13 KB

@matsbla, thanks for working on this!

Reproduced following the "Steps to reproduce" in IS with Simplified Chinese. And confirmed the patch fixed it.

  1. +++ b/core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
    @@ -238,4 +238,36 @@ protected function doTestTranslationEdit() {
    +   * Test making changes on entity with hidden non-translatable comment field.
    

    Should start with "Tests", "Tests making changes on entity with hidden non-translatable comment field."

  2. +++ b/core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
    @@ -238,4 +238,36 @@ protected function doTestTranslationEdit() {
    +    $this->assertSession()->pageTextNotContains('Non-translatable fields can only be changed when updating the original language.');
    ...
    +    $this->assertSession()->statusCodeEquals(200);
    

    Micro-optimization: $this->assertSession() called twice, each time it returns a new instance. So save it as a local variable would be better.

Otherwise, it's RTBC to me.

jungle’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new2.66 KB
new1.46 KB

Change the target version to 9.1.x. Addressing #13

jungle’s picture

StatusFileSize
new2.66 KB
new732 bytes

A comma should follow the last multiline array item. Found: 'article'

+++ b/core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
@@ -238,4 +238,37 @@ protected function doTestTranslationEdit() {
+      'title' => 'Node with non-translatable comment field',
+      'type' => 'article'

Fixing one CS violation. Missing ','

sam152’s picture

Issue summary: View changes

Updating the issue summary with the resolution that @matsbla wrote up in #5.

sam152’s picture

Status: Needs review » Needs work

Reviewed for the bug smash initiative :). I'm not a translation expert, but based on your analysis and the docblock for hasAffectingChanges, this looks like an unambiguous slam dunk.

Setting back to NW, but will keep an eye on the issue to RTBC.

  1. +++ b/core/modules/comment/src/CommentFieldItemList.php
    @@ -63,4 +64,11 @@ public function access($operation = 'view', AccountInterface $account = NULL, $r
    +    return $this->status != $original_items->status;
    

    Maybe strict equals here? Especially when dealing with all the weirdness that comes with zeros.

  2. +++ b/core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
    @@ -238,4 +238,37 @@ protected function doTestTranslationEdit() {
    +  /**
    +   * Tests making changes on entity with hidden non-translatable comment field.
    +   */
    +  public function testNonTranslatableCommentFieldHasAffectingChanges() {
    

    I love this test to cover the bug, but since this is also a public method/API, it might be nice to have a more specific test (maybe in CommentFieldItemList) that calls the method directly, with a few basic test cases? Broad functional tests are super valuable for coverage, but I always appreciate a few integration tests to narrow down the exact problematic code-paths when tracking down test fails.

  3. +++ b/core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
    @@ -238,4 +238,37 @@ protected function doTestTranslationEdit() {
    +    // Create article translator
    

    Missing full stop?

  4. +++ b/core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
    @@ -238,4 +238,37 @@ protected function doTestTranslationEdit() {
    +    // Create translation on entity with hidden non-translatable comment field.
    +    $this->drupalPostForm("/node/$nid/translations/add/en/fr", [], t('Save (this translation)'));
    +    $assert_session = $this->assertSession();
    +    $assert_session->pageTextNotContains('Non-translatable fields can only be changed when updating the original language.');
    +    $this->drupalGet("fr/node/$nid");
    +    $assert_session->statusCodeEquals(200);
    

    I wonder if we should assert the positive case here, aka "The page was saved successfully!", since if for whatever reason this message changed, that change wouldn't ever fail this test and thus it may never get updated.

    Alternatively (or additionally), it might be helpful to submit the form WITH a "status" change and assert the validation message does come up.

ravi.shankar’s picture

StatusFileSize
new2.66 KB
new1.35 KB

Here I have fixed point no. 1 and 3 of comment #17.

It still needs work for the remaining points of comment #17.

matsbla’s picture

StatusFileSize
new910 bytes
new2.77 KB

Thanks for the reviews and help!

assert the positive case here, aka "The page was saved successfully!"

Here is fix for point 4 in comment #17. However, the test already view the translation and assert that is returns code 200, which should confirm that the translation has been successfully saved.

it might be helpful to submit the form WITH a "status" change and assert the validation message does come up.

I don't think it is possible, as the comment field settings are untranslatable and the form is configured with "Hide non translatable fields on translation forms", the field is hidden and can't be changed on the translation form.

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.

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.

berdir’s picture

+++ b/core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
@@ -238,4 +238,38 @@ protected function doTestTranslationEdit() {
+    $this->drupalGet("fr/node/$nid");
+    $assert_session->statusCodeEquals(200);

I think it would be better to assert an actual change, you could change the title in the translation and make sure that shows up.

that this is an access denied on HEAD if there is no translation is imho a bug and I have an issue that changes it so it does the fallback, so that would then no longer assert much.

berdir’s picture

Alternatively, we can also remove it now that we assert the success message, which I guess is also sufficient.

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.

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new1.52 KB
new2.72 KB

Took out the deprecated methods in the test and removed the 200 assert per #23 (which I agree with)

lendude’s picture

Quick manual test on the site where we ran into this seems to show that this only helps for published nodes. If the original is unpublished, the translation still gives the error.

Didn't dig into this and didn't try to reproduce on a clean install yet, so might be something on this particular project.

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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.71 KB

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

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.

herved’s picture

Version: 11.x-dev » main

I just encountered this on a project.
I tested on the main branch and it is still valid/relevant so I will create a MR from patch #25.

I was not able to reproduce #26 so I suspect it is project-specific.

herved’s picture

Status: Needs work » Needs review

I addressed the remaining point 2 from #17, a unit test involved a lot of mocking so I went with a kernel one.
For phpstan, not sure if (or how) we should actually address this, it simply uses the magic methods...

herved’s picture

I believe #3025039: New non translatable field on translatable content throws error would also cover this here.
I guess if that one goes in, we should ask ourselves what CommentFieldItemList::hasAffectingChanges really does and if we really want to change it.
OTOH I would not count on that issue to go in... the current solution there is probably not good enough at the moment.

smustgrave’s picture

Status: Needs review » Needs work

Left some comments on the MR.

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

idebr’s picture

Status: Needs work » Needs review
StatusFileSize
new829 bytes

Added the following changes to the merge request:

  1. Updated the branch with the latest changes to main
  2. Configuration setup in CommentTranslationUITest is now set through the API instead of through the user interface
  3. Inlined code for $user in CommentTranslationUITest to avoid needing a $user variable
  4. Updated the comment for CommentItemTest::testCommentHasAffectingChanges, but the scope of test is very narrow so a comment quickly becomes redundant.

Patch attached without the changes to test code for composer patches.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

Think since this is introducing a new function to a used server probably need a CR.

Not sure if this counts as an API change (think not) but the section in the summary appears to be missing saying if it is or not.