Problem/Motivation

In #2940204: Translatable fields with synchronization enabled should behave as untranslatable fields with respect to pending revisions we realized that it would be handy to be able specify a $properties parameter to compare only a subset of the item properties.

Proposed resolution

Add it

Remaining tasks

  • Validate the proposed solution
  • Write a patch
  • Reviews

User interface changes

None

API changes

Only BC additions

Data model changes

None

Issue fork drupal-2941092

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

plach created an issue. See original summary.

idflood’s picture

I found this issue because I had an error while trying to translate a page with an image field. The image field has the "alt" and "title" translatable, and the page content type has the option to hide untranslatable fields turned on.

When trying to save the translation I get the following error:

Notice: Undefined index: width in Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraintValidator->hasSynchronizedPropertyChanges() (line 154 of core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php).

There are in fact two "issues" with the current check:

- The loop check if any of the property changed and return TRUE if any of them has changed. In this case the "alt" changes but it should still be ok as this is translatable.
- The dimension is missing in the `$items` variable but are present in the `$original_items`. In the error I have it mention the `width` property but it the error would be identical for the `height`.

james.williams’s picture

Status: Active » Needs work
StatusFileSize
new1.3 KB

I ran into the same problem that idflood ran into. It actually stopped my client being able to make any changes to translations of content.

I end up seeing this error message (which I'm putting here mostly in the hope that others hitting it might find this comment): "Non-translatable field elements can only be changed when updating the original language." This may be in part because I have implemented the tweaks suggested in the change record around 'untranslatable_fields.default_translation_affected'. It's also possible to run into the "Non-translatable field elements can only be changed when updating the current revision." error message for similar reasons.

The site has an image field, which allows the alt & title text to be translated, but not the file itself (i.e. the target_id). The entity_browser's FileBrowserWidget populates the alt & title properties alongside the target_id in its massageFormValues() method, but not height & width - core's image widget also leaves the height & weight properties out too though, so I don't believe this is unique to entity_browser. The ImageItem field class populates height & width on preSave() (which happens after validation, so these two properties could be skipped from the translation/synchronization validation when they are empty.

I don't know which of the following solutions ought to be implemented:

1) Force widgets to populate all the necessary columns for fields (e.g. is the widget at fault for not populating those columns? surely not, if ImageItem is expecting to handle empty height & weight properties anyway.)
2) Move ImageItem's population of height & width to something that runs before validation constraints (but they do run rather early, I'm not sure there is anywhere appropriate yet, is there?)
3) Implement this $properties argument to FieldItemList::equals, though I'm not sure if that would help in this case? I didn't quite follow the motivation.
4) Something else??

I feel terribly dirty about this, but since this was a showstopper for my client, I've produced the attached HACKY patch to get around this very specific instance of the problem. As far as I can see, image fields are the only ones in core that use the 'column_groups' thing that is used for field synchronization between translations, so the hack is limited to 'fixing' image fields. I am 100% aware that this patch should not be what ends up in core, but some of us need a pragmatic fix in the meantime!

Even with this fix, I believe there is a limitation that only one translation can be in draft state at a time -- that could really do with being clearer, both to the user and in code or documentation. Is it even a limitation that we can work towards removing?

All the code around the ContentTranslationSynchronizedFieldsConstraintValidator and FieldTranslationSynchronizer, and the concepts of draft & default revisions combined with translations, make my head spin, so I haven't been able to get any closer to a 'better' solution, sorry. I wish I could!

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.

mistrae’s picture

#3 saved my life. Thanks!

woutervu’s picture

Can confirm, #3 works. Thanks james.williams!

hchonov’s picture

Somehow it sounds like another problem addressed in #2988309: Ensure that all field types return TRUE on equals() for the same values. Would you please try the patch from the other issue instead?

florianmuellerch’s picture

I extended @james.williams patch a bit because it needed to be excluded also in FieldItemList comparisons. Thanks a lot for the initial hints!

mrharolda’s picture

@hchonov, the patch in #2988309: Ensure that all field types return TRUE on equals() for the same values does not fix out issue; it seems that FieldItemList::equals is never called from hasSynchronizedPropertyChanges.

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.

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.

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.

alan-ps’s picture

+1 the same issue (Drupal 8.9.13)

This is how I fixed it by own code:

/**
 * Implements hook_field_widget_form_alter().
 */
function MY_MODULE_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  // Temporary fix due to Drupal core content translation issue. For more
  // detail see: https://www.drupal.org/project/drupal/issues/2941092
  if ($element['#type'] === 'managed_file') {
    $element['#process'][] = 'MY_MODULE_content_translation_process';
  }
}

/**
 * Process callback for managed_file form element.
 */
function MY_MODULE_content_translation_process($element, FormStateInterface $form_state, $form) {
  $field_name = $element['#field_name'];

  foreach (['width', 'height'] as $value) {
    if (!empty($form[$field_name]['widget'][0]['#default_value'][$value])) {
      $element[$value] = [
        '#type' => 'value',
        '#value' => $form[$field_name]['widget'][0]['#default_value'][$value],
      ];
    }
  }

  return $element;
}
paulvb’s picture

+1 the same issue (Drupal 8.9.13)

Updated from 8.9.11 to 8.9.13

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

stefaniev’s picture

I had the same issue as #2 on Drupal 9.1.5.
The patch in #8 fixed my issue. Thanks!

rcodina’s picture

Patch on #8 works for me. The message "Non-translatable field elements can only be changed when updating the original language" has disappeared and I'm now able to save the translation node.

kapilv’s picture

StatusFileSize
new2.63 KB

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.

hctom’s picture

@KapilV, what is the purpose of your new patch? I tried to create an interdiff, but it is completely empty, so I guess it is the same patch as in #8? Please always provide some information in the comment about what you did (plus an interdiff, if you update/reroll a patch), so everybody can follow along.

andrewsuth’s picture

Status: Needs work » Reviewed & tested by the community

Patch #8 also works for my case (a lot like james.williams outlined in #3)

I was unable to translate alt fields due to the error "Non-translatable field elements can only be changed when updating the original language."

mlzr’s picture

Subscribe! Same problem + patch #8 is the solution for me.
Thanks!!

gauravvvv’s picture

StatusFileSize
new2.63 KB
new1.07 KB

Re-rolled patch #19. Attached interdiff for the same.

#8 was re-rolled by KapilV for drupal9.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

This is a problem, but the issue is very confusing. The title has nothing to do with what the patch is doing, I also feel like there are other existing issues that deal with image fields, but haven't found them yet.

The patch is failing as there are grammar issues, it will need test and hardcoding one specific field type in there is most likely not going to be accepted.

berdir’s picture

See #3218426: Using partially synchronized image fields causes validation errors and PHP warnings, this seems to be a regression in 8.9.12 and equally old drupal 9 releases.

gngn’s picture

#8 worked for me.

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.

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.

hitchshock’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: +PHP 8.1

Need to say that patch #24 also fixed PHP8.1 compatibility issue for me. Exactly fixed the warning:
Warning: Undefined array key "width" in Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraintValidator->hasSynchronizedPropertyChanges() (line 159 of core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php).
So +1 RTBC

catch’s picture

Status: Reviewed & tested by the community » Closed (duplicate)

There's no committable patch here. Also marking duplicate of #3218426: Using partially synchronized image fields causes validation errors and PHP warnings.

joseph.olstad’s picture

PHP 8.1 upgrade on Drupal 10 noticed the Undefined index on width as reported in #32

I used the patch from the other issue #3218426: Using partially synchronized image fields causes validation errors and PHP warnings

It resolved the undefined width notice