From be073ff8038f5e36368a1f4fca663a2c643419b9 Mon Sep 17 00:00:00 2001 From: webchick Date: Wed, 7 Nov 2012 02:35:40 -0800 Subject: [PATCH] Issue #1832000 by Berdir: Fixed Entity translation metadata should be stored only for translation enabled entities/bundles. --- .../node/Tests/NodeTranslationUITest.php | 19 +++++++++++++++++++ .../taxonomy/Tests/TermTranslationUITest.php | 13 +++++++++++++ .../translation_entity.module | 15 +++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php b/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php index 17c7fecff34..5fda368f18d 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php @@ -67,4 +67,23 @@ class NodeTranslationUITest extends EntityTranslationUITest { return array('title' => $this->title) + parent::getNewEntityValues($langcode); } + /** + * Test that no metadata is stored for a disabled bundle. + */ + public function testDisabledBundle() { + // Create a bundle that does not have translation enabled. + $disabledBundle = $this->randomName(); + $this->drupalCreateContentType(array('type' => $disabledBundle, 'name' => $disabledBundle)); + + // Create a node for each bundle. + $enabledNode = $this->drupalCreateNode(array('type' => $this->bundle)); + $disabledNode = $this->drupalCreateNode(array('type' => $disabledBundle)); + + // Make sure that only a single row was inserted into the + // {translation_entity} table. + $rows = db_query('SELECT * FROM {translation_entity}')->fetchAll(); + $this->assertEqual(1, count($rows)); + $this->assertEqual($enabledNode->id(), reset($rows)->entity_id); + } + } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTranslationUITest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTranslationUITest.php index 907ce879336..90982f40256 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTranslationUITest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTranslationUITest.php @@ -86,4 +86,17 @@ class TermTranslationUITest extends EntityTranslationUITest { return array('name' => $this->name) + parent::getNewEntityValues($langcode); } + /** + * Overrides \Drupal\translation_entity\Tests\EntityTranslationUITest::testTranslationUI(). + */ + public function testTranslationUI() { + parent::testTranslationUI(); + + // Make sure that no row was inserted for taxonomy vocabularies, which do + // not have translations enabled. + $rows = db_query('SELECT * FROM {translation_entity}')->fetchAll(); + $this->assertEqual(2, count($rows)); + $this->assertEqual('taxonomy_term', $rows[0]->entity_type); + $this->assertEqual('taxonomy_term', $rows[1]->entity_type); + } } diff --git a/core/modules/translation_entity/translation_entity.module b/core/modules/translation_entity/translation_entity.module index b0c852673da..f57773be797 100644 --- a/core/modules/translation_entity/translation_entity.module +++ b/core/modules/translation_entity/translation_entity.module @@ -500,6 +500,11 @@ function translation_entity_load_translation_data(array $entities, $entity_type) * Implements hook_entity_insert(). */ function translation_entity_entity_insert(EntityInterface $entity) { + // Only do something if translation support for the given entity is enabled. + if (!translation_entity_enabled($entity->entityType(), $entity->bundle())) { + return; + } + $entity_type = $entity->entityType(); $id = $entity->id(); $query = db_insert('translation_entity') @@ -519,6 +524,11 @@ function translation_entity_entity_insert(EntityInterface $entity) { * Implements hook_entity_delete(). */ function translation_entity_entity_delete(EntityInterface $entity) { + // Only do something if translation support for the given entity is enabled. + if (!translation_entity_enabled($entity->entityType(), $entity->bundle())) { + return; + } + db_delete('translation_entity') ->condition('entity_type', $entity->entityType()) ->condition('entity_id', $entity->id()) @@ -529,6 +539,11 @@ function translation_entity_entity_delete(EntityInterface $entity) { * Implements hook_entity_update(). */ function translation_entity_entity_update(EntityInterface $entity) { + // Only do something if translation support for the given entity is enabled. + if (!translation_entity_enabled($entity->entityType(), $entity->bundle())) { + return; + } + // Delete and create to ensure no stale value remains behind. translation_entity_entity_delete($entity); translation_entity_entity_insert($entity);