Issue #2939917 by plach, jibran, Wim Leers: Field synchronization should run only for Content Translation-enabled entities

merge-requests/1654/head
effulgentsia 2018-01-27 18:19:56 -08:00
parent bc1fc2f7bd
commit fb4d8966c3
3 changed files with 31 additions and 3 deletions

View File

@ -430,6 +430,11 @@ function content_translation_form_field_config_edit_form_alter(array &$form, For
*/
function content_translation_entity_presave(EntityInterface $entity) {
if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && !$entity->isNew()) {
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
$manager = \Drupal::service('content_translation.manager');
if (!$manager->isEnabled($entity->getEntityTypeId(), $entity->bundle())) {
return;
}
// If we are creating a new translation we need to use the source language
// as original language, since source values are the only ones available to
// compare against.
@ -438,8 +443,6 @@ function content_translation_entity_presave(EntityInterface $entity) {
->getStorage($entity->entityType())->loadUnchanged($entity->id());
}
$langcode = $entity->language()->getId();
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
$manager = \Drupal::service('content_translation.manager');
$source_langcode = !$entity->original->hasTranslation($langcode) ? $manager->getTranslationMetadata($entity)->getSource() : NULL;
\Drupal::service('content_translation.synchronizer')->synchronizeFields($entity, $langcode, $source_langcode);
}

View File

@ -13,7 +13,14 @@ use Drupal\Core\Form\FormStateInterface;
function content_translation_test_entity_bundle_info_alter(&$bundles) {
// Store the initial status of the "translatable" property for the
// "entity_test_mul" bundle.
\Drupal::state()->set('content_translation_test.translatable', !empty($bundles['entity_test_mul']['entity_test_mul']['translatable']));
$translatable = !empty($bundles['entity_test_mul']['entity_test_mul']['translatable']);
\Drupal::state()->set('content_translation_test.translatable', $translatable);
// Make it translatable if Content Translation did not. This will make the
// entity object translatable even if it is disabled in Content Translation
// settings.
if (!$translatable) {
$bundles['entity_test_mul']['entity_test_mul']['translatable'] = TRUE;
}
}
/**

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\content_translation\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests the Content Translation bundle info logic.
@ -40,6 +42,8 @@ class ContentTranslationEntityBundleInfoTest extends KernelTestBase {
$this->bundleInfo = $this->container->get('entity_type.bundle.info');
$this->installEntitySchema('entity_test_mul');
ConfigurableLanguage::createFromLangcode('it')->save();
}
/**
@ -69,4 +73,18 @@ class ContentTranslationEntityBundleInfoTest extends KernelTestBase {
$this->assertTrue($state->get('content_translation_test.translatable'));
}
/**
* Tests that field synchronization is skipped for disabled bundles.
*/
public function testFieldSynchronizationWithDisabledBundle() {
$entity = EntityTestMul::create();
$entity->save();
/** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
$translation = $entity->addTranslation('it');
$translation->save();
$this->assertTrue($entity->isTranslatable());
}
}