diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 3834dae..5522c3f 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -357,18 +357,14 @@ protected function setStaticCache(array $entities) { } /** - * Invokes a hook on behalf of the entity. - * - * @param $hook - * One of 'presave', 'insert', 'update', 'predelete', or 'delete'. - * @param $entity - * The entity object. + * {@inheritdoc} */ - protected function invokeHook($hook, EntityInterface $entity) { + protected function invokeHook($hook, EntityInterface $entity, array $args = []) { + array_unshift($args, $entity, $this->entityTypeId); // Invoke the hook. - $this->moduleHandler->invokeAll($this->entityTypeId . '_' . $hook, array($entity)); + $this->moduleHandler->invokeAll($this->entityTypeId . '_' . $hook, $args); // Invoke the respective entity-level hook. - $this->moduleHandler->invokeAll('entity_' . $hook, array($entity, $this->entityTypeId)); + $this->moduleHandler->invokeAll('entity_' . $hook, $args); } /** diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index afdce40..644337b 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -128,13 +128,26 @@ protected function initFieldValues(ContentEntityInterface $entity, array $values * {@inheritdoc} */ public function createTranslation(ContentEntityInterface $entity, $langcode, array $values = []) { + // The source translation language could be estimated from the $values in + // the case that the translation has been added through + // $entity->addTranslation($langcode, $entity_src_translation->toArray()). + if (isset($values[$this->langcodeKey])) { + if (isset($values[$this->langcodeKey][0]['value'])) { + $src_langcode = $values[$this->langcodeKey][0]['value']; + } + elseif (!is_array($values[$this->langcodeKey])) { + $src_langcode = $values[$this->langcodeKey]; + } + } + $src_langcode = isset($src_langcode) && $entity->hasTranslation($src_langcode) ? $src_langcode : NULL; + $translation = $entity->getTranslation($langcode); $definitions = array_filter($translation->getFieldDefinitions(), function(FieldDefinitionInterface $definition) { return $definition->isTranslatable(); }); $field_names = array_map(function(FieldDefinitionInterface $definition) { return $definition->getName(); }, $definitions); $values[$this->langcodeKey] = $langcode; $values[$this->getEntityType()->getKey('default_langcode')] = FALSE; $this->initFieldValues($translation, $values, $field_names); - $this->invokeHook('translation_create', $translation); + $this->invokeHook('translation_create', $translation, [$src_langcode]); return $translation; } @@ -398,7 +411,7 @@ protected function invokeStorageLoadHook(array &$entities) { /** * {@inheritdoc} */ - protected function invokeHook($hook, EntityInterface $entity) { + protected function invokeHook($hook, EntityInterface $entity, array $args = []) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ switch ($hook) { @@ -415,7 +428,7 @@ protected function invokeHook($hook, EntityInterface $entity) { break; } - parent::invokeHook($hook, $entity); + parent::invokeHook($hook, $entity, $args); } /** diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index f583121..d2a5361 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -161,12 +161,15 @@ protected function setStaticCache(array $entities) { * 'revision_delete'. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object. + * @param array $args + * Other arguments to pass to the hook beside the entity. */ - protected function invokeHook($hook, EntityInterface $entity) { + protected function invokeHook($hook, EntityInterface $entity, array $args = []) { + array_unshift($args, $entity); // Invoke the hook. - $this->moduleHandler()->invokeAll($this->entityTypeId . '_' . $hook, array($entity)); + $this->moduleHandler()->invokeAll($this->entityTypeId . '_' . $hook, $args); // Invoke the respective entity-level hook. - $this->moduleHandler()->invokeAll('entity_' . $hook, array($entity)); + $this->moduleHandler()->invokeAll('entity_' . $hook, $args); } /** diff --git a/core/lib/Drupal/Core/Entity/entity.api.php b/core/lib/Drupal/Core/Entity/entity.api.php index 9f6ef8b..c8e981d 100644 --- a/core/lib/Drupal/Core/Entity/entity.api.php +++ b/core/lib/Drupal/Core/Entity/entity.api.php @@ -1029,11 +1029,15 @@ function hook_ENTITY_TYPE_update(Drupal\Core\Entity\EntityInterface $entity) { * * @param \Drupal\Core\Entity\EntityInterface $translation * The entity object. + * @param string|NULL $source_langcode + * The source language code from which the entity has been translated. It + * will be set only if the source translation could be determined by the + * storage. * * @ingroup entity_crud * @see hook_ENTITY_TYPE_translation_create() */ -function hook_entity_translation_create(\Drupal\Core\Entity\EntityInterface $translation) { +function hook_entity_translation_create(\Drupal\Core\Entity\EntityInterface $translation, $source_langcode = NULL) { \Drupal::logger('example')->info('Entity translation created: @label', ['@label' => $translation->label()]); } @@ -1045,11 +1049,15 @@ function hook_entity_translation_create(\Drupal\Core\Entity\EntityInterface $tra * * @param \Drupal\Core\Entity\EntityInterface $translation * The entity object. + * @param string|NULL $source_langcode + * The source language code from which the entity has been translated. It + * will be set only if the source translation could be determined by the + * storage. * * @ingroup entity_crud * @see hook_entity_translation_create() */ -function hook_ENTITY_TYPE_translation_create(\Drupal\Core\Entity\EntityInterface $translation) { +function hook_ENTITY_TYPE_translation_create(\Drupal\Core\Entity\EntityInterface $translation, $source_langcode = NULL) { \Drupal::logger('example')->info('ENTITY_TYPE translation created: @label', ['@label' => $translation->label()]); }