diff --git a/core/modules/content_translation/src/FieldTranslationSynchronizer.php b/core/modules/content_translation/src/FieldTranslationSynchronizer.php index 09b32d8744..bd85384e7b 100644 --- a/core/modules/content_translation/src/FieldTranslationSynchronizer.php +++ b/core/modules/content_translation/src/FieldTranslationSynchronizer.php @@ -64,8 +64,10 @@ function (FieldDefinitionInterface $field_definition) use ($self) { * {@inheritdoc} */ protected function getFieldSynchronizationSettings(FieldDefinitionInterface $field_definition) { - return $field_definition instanceof ThirdPartySettingsInterface && $field_definition->isTranslatable() && - ($translation_sync = $field_definition->getThirdPartySetting('content_translation', 'translation_sync')) ? $translation_sync : NULL; + if ($field_definition instanceof ThirdPartySettingsInterface && $field_definition->isTranslatable()) { + return $field_definition->getThirdPartySetting('content_translation', 'translation_sync') ?: NULL; + } + return NULL; } /** @@ -88,6 +90,24 @@ public function synchronizeFields(ContentEntityInterface $entity, $sync_langcode return; } + $force_sync = FALSE; + if ($entity->isDefaultTranslationAffectedOnly()) { + // If changes to untranslatable fields affect are configured to affect + // only the default revision translation, we need to skip synchronization + // is pending revisions, otherwise multiple revision translations would be + // affected. + if (!$entity->isDefaultRevision()) { + return; + } + // When saving a default revision for a non-default translation, we need + // to make sure that we reconcile any previous changes in the default + // translation. + elseif (!$entity->isDefaultTranslation()) { + $sync_langcode = $entity->getUntranslated()->language()->getId(); + $force_sync = TRUE; + } + } + /** @var \Drupal\Core\Field\FieldItemListInterface $items */ foreach ($entity as $field_name => $items) { $field_definition = $items->getFieldDefinition(); @@ -131,6 +151,10 @@ public function synchronizeFields(ContentEntityInterface $entity, $sync_langcode $unchanged_items = $entity_unchanged->getTranslation($langcode)->get($field_name)->getValue(); $this->synchronizeItems($values, $unchanged_items, $sync_langcode, array_keys($translations), $columns); + if ($force_sync) { + $this->enforceSynchronization($values, $sync_langcode, $columns); + } + foreach ($translations as $langcode => $language) { $entity->getTranslation($langcode)->get($field_name)->setValue($values[$langcode]); } @@ -140,6 +164,29 @@ public function synchronizeFields(ContentEntityInterface $entity, $sync_langcode } } + /** + * Ensures that field values are actually synchronized. + * + * @param array[] $field_values + * The field values to be synchronized. + * @param string $sync_langcode + * The + * + * @param array $columns + */ + protected function enforceSynchronization(array &$field_values, $sync_langcode, array $columns) { + $source_items = $field_values[$sync_langcode]; + foreach ($field_values as $langcode => &$items) { + if ($langcode !== $sync_langcode) { + foreach ($items as $delta => $item) { + foreach ($columns as $column) { + $items[$delta][$column] = $source_items[$delta][$column]; + } + } + } + } + } + /** * {@inheritdoc} */ diff --git a/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraint.php b/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraint.php index 71c01b16ff..c04335bc16 100644 --- a/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraint.php +++ b/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraint.php @@ -17,6 +17,6 @@ */ class ContentTranslationSynchronizedFieldsConstraint extends Constraint { - public $message = 'Non translatable field elements can only be changed when updating the current revision'; + public $message = 'Non-translatable field elements can only be changed when updating the current revision or the original language.'; } diff --git a/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php b/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php index 6c76baf277..49603e0e09 100644 --- a/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php +++ b/core/modules/content_translation/src/Plugin/Validation/Constraint/ContentTranslationSynchronizedFieldsConstraintValidator.php @@ -13,7 +13,21 @@ use Symfony\Component\Validator\ConstraintValidator; /** - * Checks that synchronized fields are not changed in pending revisions. + * Checks that synchronized fields are handled correctly in pending revisions. + * + * As for untranslatable fields, two modes are supported: + * - When changes to untranslatable fields are configured to affect all revision + * translations, synchronized field properties can be changed only in default + * revisions. + * - When changes to untranslatable fields affect are configured to affect only + * the default revision translation, synchronized field properties can be + * changed only when editing the default translation. This may lead to + * temporarily desynchronized values, when saving a pending revision for the + * default translation that changes a synchronized property. These are + * reconciled when saving changes to the default translation as the new + * default revision. + * + * @see \Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraint * * @internal */ @@ -76,7 +90,12 @@ public function validate($value, Constraint $constraint) { } /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $value; - if ($entity->isNew() || $entity->isDefaultRevision() || !$entity->getEntityType()->isRevisionable()) { + if ($entity->isNew() || !$entity->getEntityType()->isRevisionable()) { + return; + } + // When changes to untranslatable fields are configured to affect all + // revision translations, we always allow changes in default revisions. + if ($entity->isDefaultRevision() && !$entity->isDefaultTranslationAffectedOnly()) { return; } $entity_type_id = $entity->getEntityTypeId(); @@ -90,7 +109,31 @@ public function validate($value, Constraint $constraint) { /** @var \Drupal\Core\Entity\ContentEntityInterface $original */ $original = $this->synchronizer->getOriginalEntity($entity); - if ($this->hasSynchronizedPropertyChanges($entity, $original, $settings)) { + $original_translation = $this->getOriginalTranslation($entity, $original); + $has_changes = $this->hasSynchronizedPropertyChanges($entity, $original_translation, $settings); + + if ($entity->isDefaultTranslationAffectedOnly()) { + if (!$entity->isDefaultTranslation()) { + if ($has_changes) { + $this->context->addViolation($constraint->message); + } + else { + $active_langcode = $entity->language()->getId(); + foreach ($entity->getTranslationLanguages(FALSE) as $langcode => $language) { + // We already checked the active translation above. + if ($langcode !== $active_langcode) { + $translation = $entity->getTranslation($langcode); + $original_translation = $this->getOriginalTranslation($entity, $original); + if ($this->hasSynchronizedPropertyChanges($translation, $original_translation, $settings)) { + $this->context->addViolation($constraint->message); + break; + } + } + } + } + } + } + elseif ($has_changes) { $this->context->addViolation($constraint->message); } } @@ -116,9 +159,16 @@ protected function hasSynchronizedPropertyChanges(ContentEntityInterface $entity if (!$translatable && isset($field_definitions[$field_name])) { $field_type_definition = $this->fieldTypeManager->getDefinition($field_definitions[$field_name]->getType()); foreach ($field_type_definition['column_groups'][$group]['columns'] as $property) { - if ($entity->get($field_name)->{$property} != $original->get($field_name)->{$property}) { + $items = $entity->get($field_name)->getValue(); + $original_items = $original->get($field_name)->getValue(); + if (count($items) !== count($original_items)) { return TRUE; } + foreach ($items as $delta => $item) { + if ($items[$delta][$property] != $original_items[$delta][$property]) { + return TRUE; + } + } } } } @@ -127,4 +177,27 @@ protected function hasSynchronizedPropertyChanges(ContentEntityInterface $entity return FALSE; } + /** + * Returns the original translation. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity being validated. + * @param \Drupal\Core\Entity\ContentEntityInterface $original + * The original entity. + * + * @return \Drupal\Core\Entity\ContentEntityInterface + * The original entity translation object. + */ + protected function getOriginalTranslation(ContentEntityInterface $entity, ContentEntityInterface $original) { + $langcode = $entity->language()->getId(); + if ($original->hasTranslation($langcode)) { + $original_langcode = $langcode; + } + else { + $metadata = $this->contentTranslationManager->getTranslationMetadata($entity); + $original_langcode = $metadata->getSource(); + } + return $original->getTranslation($original_langcode); + } + } diff --git a/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncValidationTest.php b/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncValidationTest.php index ec5a993277..30dee9dc56 100644 --- a/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncValidationTest.php +++ b/core/modules/content_translation/tests/src/Kernel/ContentTranslationFieldSyncValidationTest.php @@ -2,6 +2,9 @@ namespace Drupal\Tests\content_translation\Kernel; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityConstraintViolationListInterface; +use Drupal\Core\Language\LanguageInterface; use Drupal\entity_test\Entity\EntityTestMulRev; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; @@ -62,8 +65,6 @@ protected function setUp() { ConfigurableLanguage::createFromLangcode('it')->save(); ConfigurableLanguage::createFromLangcode('fr')->save(); - $this->storage = $this->entityManager->getStorage($entity_type_id); - /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */ $field_storage_config = FieldStorageConfig::create([ 'field_name' => $this->fieldName, @@ -96,6 +97,8 @@ protected function setUp() { $this->contentTranslationManager = $this->container->get('content_translation.manager'); $this->contentTranslationManager->setEnabled($entity_type_id, $entity_type_id, TRUE); + $this->storage = $this->entityManager->getStorage($entity_type_id); + foreach ($this->getTestFiles('image') as $file) { $entity = File::create((array) $file + ['status' => 1]); $entity->save(); @@ -119,82 +122,150 @@ protected function setUp() { * @covers ::hasSynchronizedPropertyChanges */ public function testPendingRevisionValidation() { - foreach ([TRUE, FALSE] as $untranslatable_field_widget_display) { - $this->doTestPendingRevisionValidation($untranslatable_field_widget_display); - } - } + // Test that when untranslatable field widgets are displayed, synchronized + // field properties can be changed only in default revisions. + $this->setUntranslatableFieldWidgetsDisplay(TRUE); + $entity = $this->saveNewEntity(); + $entity_id = $entity->id(); + $this->assertLatestRevisionFieldValues($entity_id, [1, 1, 1, 'Alt 1 EN']); - /** - * Tests a sequence of validate and save operations. - * - * @param $untranslatable_field_widget_display - * Whether untranslatable field widgets should be displayed or hidden. - */ - protected function doTestPendingRevisionValidation($untranslatable_field_widget_display) { - $this->setUntranslatableFieldWidgetsDisplay($untranslatable_field_widget_display); + /** @var \Drupal\Core\Entity\ContentEntityInterface $en_revision */ + $en_revision = $this->createRevision($entity, FALSE); + $en_revision->get($this->fieldName)->target_id = 2; + $violations = $en_revision->validate(); + $this->assertViolations($violations); - /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - $entity = EntityTestMulRev::create([ - 'uid' => 1, - 'langcode' => 'en', - $this->fieldName => [ - 'target_id' => 1, - 'alt' => 'test', - 'title' => 'test', - ], - ]); - $violations = $entity->validate(); + $it_translation = $entity->addTranslation('it', $entity->toArray()); + /** @var \Drupal\Core\Entity\ContentEntityInterface $it_revision */ + $it_revision = $this->createRevision($it_translation, FALSE); + $metadata = $this->contentTranslationManager->getTranslationMetadata($it_revision); + $metadata->setSource('en'); + $it_revision->get($this->fieldName)->target_id = 2; + $it_revision->get($this->fieldName)->alt = 'Alt 2 IT'; + $violations = $it_revision->validate(); + $this->assertViolations($violations); + $it_revision->isDefaultRevision(TRUE); + $violations = $it_revision->validate(); $this->assertEmpty($violations); - $entity->save(); + $this->storage->save($it_revision); + $this->assertLatestRevisionFieldValues($entity_id, [2, 2, 2, 'Alt 1 EN', 'Alt 2 IT']); - /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */ - $revision = $this->storage->createRevision($entity, FALSE); - $revision->get($this->fieldName)->target_id = 2; - $violations = $revision->validate(); - $this->assertNotEmpty($violations); + $en_revision = $this->createRevision($en_revision, FALSE); + $en_revision->get($this->fieldName)->alt = 'Alt 3 EN'; + $violations = $en_revision->validate(); + $this->assertEmpty($violations); + $this->storage->save($en_revision); + $this->assertLatestRevisionFieldValues($entity_id, [3, 2, 2, 'Alt 3 EN', 'Alt 2 IT']); + + $it_revision = $this->createRevision($it_revision, FALSE); + $it_revision->get($this->fieldName)->alt = 'Alt 4 IT'; + $violations = $it_revision->validate(); + $this->assertEmpty($violations); + $this->storage->save($it_revision); + $this->assertLatestRevisionFieldValues($entity_id, [4, 2, 2, 'Alt 1 EN', 'Alt 4 IT']); + + $en_revision = $this->createRevision($en_revision); + $en_revision->get($this->fieldName)->alt = 'Alt 5 EN'; + $violations = $en_revision->validate(); + $this->assertEmpty($violations); + $this->storage->save($en_revision); + $this->assertLatestRevisionFieldValues($entity_id, [5, 2, 2, 'Alt 5 EN', 'Alt 2 IT']); + + $en_revision = $this->createRevision($en_revision); + $en_revision->get($this->fieldName)->target_id = 6; + $en_revision->get($this->fieldName)->alt = 'Alt 6 EN'; + $violations = $en_revision->validate(); + $this->assertEmpty($violations); + $this->storage->save($en_revision); + $this->assertLatestRevisionFieldValues($entity_id, [6, 6, 6, 'Alt 6 EN', 'Alt 2 IT']); - $revision = $this->storage->createRevision($entity->addTranslation('it'), FALSE); - $metadata = $this->contentTranslationManager->getTranslationMetadata($revision); + $it_revision = $this->createRevision($it_revision); + $it_revision->get($this->fieldName)->alt = 'Alt 7 IT'; + $violations = $it_revision->validate(); + $this->assertEmpty($violations); + $this->storage->save($it_revision); + $this->assertLatestRevisionFieldValues($entity_id, [7, 6, 6, 'Alt 6 EN', 'Alt 7 IT']); + + // Test that when untranslatable field widgets are hidden, synchronized + // field properties can be changed only when editing the default + // translation. This may lead to temporarily desynchronized values, when + // saving a pending revision for the default translation that changes a + // synchronized property (see revision 11). + $this->setUntranslatableFieldWidgetsDisplay(FALSE); + $entity = $this->saveNewEntity(); + $entity_id = $entity->id(); + $this->assertLatestRevisionFieldValues($entity_id, [8, 1, 1, 'Alt 1 EN']); + + /** @var \Drupal\Core\Entity\ContentEntityInterface $en_revision */ + $en_revision = $this->createRevision($entity, FALSE); + $en_revision->get($this->fieldName)->target_id = 2; + $en_revision->get($this->fieldName)->alt = 'Alt 2 EN'; + $violations = $en_revision->validate(); + $this->assertEmpty($violations); + $this->storage->save($en_revision); + $this->assertLatestRevisionFieldValues($entity_id, [9, 2, 2, 'Alt 2 EN']); + + $it_translation = $entity->addTranslation('it', $entity->toArray()); + /** @var \Drupal\Core\Entity\ContentEntityInterface $it_revision */ + $it_revision = $this->createRevision($it_translation, FALSE); + $metadata = $this->contentTranslationManager->getTranslationMetadata($it_revision); + $metadata->setSource('en'); + $it_revision->get($this->fieldName)->target_id = 3; + $violations = $it_revision->validate(); + $this->assertViolations($violations); + $it_revision->isDefaultRevision(TRUE); + $violations = $it_revision->validate(); + $this->assertViolations($violations); + + $it_revision = $this->createRevision($it_translation); + $metadata = $this->contentTranslationManager->getTranslationMetadata($it_revision); $metadata->setSource('en'); - $revision->get($this->fieldName)->target_id = 2; - $violations = $revision->validate(); - $this->assertNotEmpty($violations); - $revision->isDefaultRevision(TRUE); - $revision->validate(); - $violations = $revision->validate(); + $it_revision->get($this->fieldName)->alt = 'Alt 3 IT'; + $violations = $it_revision->validate(); $this->assertEmpty($violations); - $revision->save(); + $this->storage->save($it_revision); + $this->assertLatestRevisionFieldValues($entity_id, [10, 1, 1, 'Alt 1 EN', 'Alt 3 IT']); - $revision = $this->storage->createRevision($revision->getTranslation('en'), FALSE); - $revision->get($this->fieldName)->alt = 'alt 3'; - $violations = $revision->validate(); + $en_revision = $this->createRevision($en_revision, FALSE); + $en_revision->get($this->fieldName)->alt = 'Alt 4 EN'; + $violations = $en_revision->validate(); $this->assertEmpty($violations); - $revision->save(); + $this->storage->save($en_revision); + $this->assertLatestRevisionFieldValues($entity_id, [11, 2, 1, 'Alt 4 EN', 'Alt 3 IT']); - $revision = $this->storage->createRevision($revision->getTranslation('it'), FALSE); - $revision->get($this->fieldName)->alt = 'alt 4'; - $violations = $revision->validate(); + $it_revision = $this->createRevision($it_revision, FALSE); + $it_revision->get($this->fieldName)->alt = 'Alt 5 IT'; + $violations = $it_revision->validate(); $this->assertEmpty($violations); - $revision->save(); + $this->storage->save($it_revision); + $this->assertLatestRevisionFieldValues($entity_id, [12, 1, 1, 'Alt 1 EN', 'Alt 5 IT']); - $revision = $this->storage->createRevision($revision); - $revision->get($this->fieldName)->target_id = 5; - $violations = $revision->validate(); + $en_revision = $this->createRevision($en_revision); + $en_revision->get($this->fieldName)->target_id = 6; + $en_revision->get($this->fieldName)->alt = 'Alt 6 EN'; + $violations = $en_revision->validate(); $this->assertEmpty($violations); - $revision->save(); + $this->storage->save($en_revision); + $this->assertLatestRevisionFieldValues($entity_id, [13, 6, 6, 'Alt 6 EN', 'Alt 3 IT']); + + $it_revision = $this->createRevision($it_revision); + $it_revision->get($this->fieldName)->target_id = 7; + $violations = $it_revision->validate(); + $this->assertViolations($violations); - $revision = $this->storage->createRevision($revision->getTranslation('en')); - $revision->get($this->fieldName)->target_id = 6; - $violations = $revision->validate(); + $it_revision = $this->createRevision($it_revision); + $it_revision->get($this->fieldName)->alt = 'Alt 7 IT'; + $violations = $it_revision->validate(); $this->assertEmpty($violations); - $revision->save(); + $this->storage->save($it_revision); + $this->assertLatestRevisionFieldValues($entity_id, [14, 6, 6, 'Alt 6 EN', 'Alt 7 IT']); } /** * Sets untranslatable field widgets' display status. * * @param bool $display - * Whether untransltable field widgets should be displayed. + * Whether untranslatable field widgets should be displayed. */ protected function setUntranslatableFieldWidgetsDisplay($display) { $entity_type_id = $this->storage->getEntityTypeId(); @@ -205,4 +276,93 @@ protected function setUntranslatableFieldWidgetsDisplay($display) { $bundle_info->clearCachedBundles(); } + /** + * @return \Drupal\Core\Entity\ContentEntityInterface + */ + protected function saveNewEntity() { + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = EntityTestMulRev::create([ + 'uid' => 1, + 'langcode' => 'en', + $this->fieldName => [ + 'target_id' => 1, + 'alt' => 'Alt 1 EN', + ], + ]); + $metadata = $this->contentTranslationManager->getTranslationMetadata($entity); + $metadata->setSource(LanguageInterface::LANGCODE_NOT_SPECIFIED); + $violations = $entity->validate(); + $this->assertEmpty($violations); + $this->storage->save($entity); + return $entity; + } + + /** + * Creates a new revision starting from the latest translation-affecting one. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $translation + * The translation to be revisioned. + * @param bool $default + * (optional) Whether the new revision should be marked as default. Defaults + * to TRUE. + * + * @return \Drupal\Core\Entity\ContentEntityInterface + * An entity revision object. + */ + protected function createRevision(ContentEntityInterface $translation, $default = TRUE) { + if (!$translation->isNewTranslation()) { + $langcode = $translation->language()->getId(); + $revision_id = $this->storage->getLatestTranslationAffectedRevisionId($translation->id(), $langcode); + /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */ + $revision = $this->storage->loadRevision($revision_id); + $translation = $revision->getTranslation($langcode); + } + /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */ + $revision = $this->storage->createRevision($translation, $default); + return $revision; + } + + /** + * Asserts that the expected violations were found. + * + * @param \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations + * A list of violations. + */ + protected function assertViolations(EntityConstraintViolationListInterface $violations) { + $list = []; + foreach ($violations as $violation) { + if ((string) $violation->getMessage() === 'Non-translatable field elements can only be changed when updating the current revision or the original language.') { + $list[] = $violation; + } + } + $this->assertCount(1, $list); + } + + /** + * Asserts that the latest revision has the expected field values. + * + * @param $entity_id + * The entity ID. + * @param array $expected_values + * An array of expected values in the following order: + * - revision ID + * - target ID (en) + * - target ID (it) + * - alt (en) + * - alt (it) + */ + protected function assertLatestRevisionFieldValues($entity_id, array $expected_values) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $this->storage->loadRevision($this->storage->getLatestRevisionId($entity_id)); + @list($revision_id, $target_id_en, $target_id_it, $alt_en, $alt_it) = $expected_values; + $this->assertEquals($revision_id, $entity->getRevisionId()); + $this->assertEquals($target_id_en, $entity->get($this->fieldName)->target_id); + $this->assertEquals($alt_en, $entity->get($this->fieldName)->alt); + if ($entity->hasTranslation('it')) { + $it_translation = $entity->getTranslation('it'); + $this->assertEquals($target_id_it, $it_translation->get($this->fieldName)->target_id); + $this->assertEquals($alt_it, $it_translation->get($this->fieldName)->alt); + } + } + }