Issue #2649602 by hchonov: hook_translation_create is not invoked with the new entity translation but with the previous entity object

8.1.x
Nathaniel Catchpole 2016-02-03 16:08:07 +09:00
parent 0c127c5491
commit b53d68dda3
3 changed files with 62 additions and 3 deletions

View File

@ -139,7 +139,7 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
$values[$this->langcodeKey] = $langcode;
$values[$this->getEntityType()->getKey('default_langcode')] = FALSE;
$this->initFieldValues($translation, $values, $field_names);
$this->invokeHook('translation_create', $entity);
$this->invokeHook('translation_create', $translation);
return $translation;
}

View File

@ -371,6 +371,10 @@ class EntityTranslationTest extends EntityLanguageTestBase {
// Save the translation and check that the expected hooks are fired.
$translation->save();
$hooks = $this->getHooksInfo();
$this->assertEqual($hooks['entity_translation_create'], $langcode, 'The generic entity translation creation hook has fired.');
$this->assertEqual($hooks[$entity_type . '_translation_create'], $langcode, 'The entity-type-specific entity translation creation hook has fired.');
$this->assertEqual($hooks['entity_translation_insert'], $langcode, 'The generic entity translation insertion hook has fired.');
$this->assertEqual($hooks[$entity_type . '_translation_insert'], $langcode, 'The entity-type-specific entity translation insertion hook has fired.');
@ -436,6 +440,10 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$this->assertEqual($entity->language()->getId(), $default_langcode, 'The original language has been preserved.');
$translation->save();
$hooks = $this->getHooksInfo();
$this->assertEqual($hooks['entity_translation_create'], $langcode2, 'The generic entity translation creation hook has fired.');
$this->assertEqual($hooks[$entity_type . '_translation_create'], $langcode2, 'The entity-type-specific entity translation creation hook has fired.');
$this->assertEqual($hooks['entity_translation_insert'], $langcode2, 'The generic entity translation insertion hook has fired.');
$this->assertEqual($hooks[$entity_type . '_translation_insert'], $langcode2, 'The entity-type-specific entity translation insertion hook has fired.');
@ -483,7 +491,13 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$entity->removeTranslation($langcode2);
$entity->save();
$hooks = $this->getHooksInfo();
$this->assertFalse($hooks, 'No hooks are run when adding and removing a translation without storing it.');
$this->assertTrue(isset($hooks['entity_translation_create']), 'The generic entity translation creation hook is run when adding and removing a translation without storing it.');
unset($hooks['entity_translation_create']);
$this->assertTrue(isset($hooks[$entity_type . '_translation_create']), 'The entity-type-specific entity translation creation hook is run when adding and removing a translation without storing it.');
unset($hooks[$entity_type . '_translation_create']);
$this->assertFalse($hooks, 'No other hooks beyond the entity translation creation hooks are run when adding and removing a translation without storing it.');
// Check that hooks are fired only when actually storing data.
$entity = $this->reloadEntity($entity);
@ -554,6 +568,9 @@ class EntityTranslationTest extends EntityLanguageTestBase {
);
$this->assertEqual($translation->description->getValue(), $expected, 'Language-aware default values correctly populated.');
$this->assertEqual($translation->description->getLangcode(), $langcode2, 'Field object has the expected langcode.');
// Reset hook firing information.
$this->getHooksInfo();
}
/**

View File

@ -471,6 +471,13 @@ function entity_test_entity_operation_alter(array &$operations, EntityInterface
}
}
/**
* Implements hook_entity_translation_create().
*/
function entity_test_entity_translation_create(EntityInterface $translation) {
_entity_test_record_hooks('entity_translation_create', $translation->language()->getId());
}
/**
* Implements hook_entity_translation_insert().
*/
@ -485,6 +492,13 @@ function entity_test_entity_translation_delete(EntityInterface $translation) {
_entity_test_record_hooks('entity_translation_delete', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_create() for 'entity_test_mul'.
*/
function entity_test_entity_test_mul_translation_create(EntityInterface $translation) {
_entity_test_record_hooks('entity_test_mul_translation_create', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_insert() for 'entity_test_mul'.
*/
@ -500,7 +514,14 @@ function entity_test_entity_test_mul_translation_delete(EntityInterface $transla
}
/**
* Implements hook_ENTITY_TYPE_translation_insert() for 'entity_test_mulrev'.
* Implements hook_ENTITY_TYPE_translation_create() for 'entity_test_mul_changed'.
*/
function entity_test_entity_test_mul_changed_translation_create(EntityInterface $translation) {
_entity_test_record_hooks('entity_test_mul_changed_translation_create', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_insert() for 'entity_test_mul_changed'.
*/
function entity_test_entity_test_mul_changed_translation_insert(EntityInterface $translation) {
_entity_test_record_hooks('entity_test_mul_changed_translation_insert', $translation->language()->getId());
@ -513,6 +534,13 @@ function entity_test_entity_test_mul_changed_translation_delete(EntityInterface
_entity_test_record_hooks('entity_test_mul_changed_translation_delete', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_create().
*/
function entity_test_entity_test_mulrev_translation_create(EntityInterface $translation) {
_entity_test_record_hooks('entity_test_mulrev_translation_create', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_insert().
*/
@ -527,6 +555,13 @@ function entity_test_entity_test_mulrev_translation_delete(EntityInterface $tran
_entity_test_record_hooks('entity_test_mulrev_translation_delete', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_create() for 'entity_test_mulrev_changed'.
*/
function entity_test_entity_test_mulrev_changed_translation_create(EntityInterface $translation) {
_entity_test_record_hooks('entity_test_mulrev_changed_translation_create', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_insert() for 'entity_test_mulrev'.
*/
@ -541,6 +576,13 @@ function entity_test_entity_test_mulrev_changed_translation_delete(EntityInterfa
_entity_test_record_hooks('entity_test_mulrev_changed_translation_delete', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_create() for 'entity_test_mul_langcode_key'.
*/
function entity_test_entity_test_mul_langcode_key_translation_create(EntityInterface $translation) {
_entity_test_record_hooks('entity_test_mul_langcode_key_translation_create', $translation->language()->getId());
}
/**
* Implements hook_ENTITY_TYPE_translation_insert() for 'entity_test_mul_langcode_key'.
*/