diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index 9ab0bfd..2b26dd2 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -52,12 +52,18 @@ function content_translation_help($route_name, RouteMatchInterface $route_match) */ function content_translation_module_implements_alter(&$implementations, $hook) { switch ($hook) { - // Move some of our hook implementations to the end of the list. + // Move our entity_type_alter hook implementation to the end of the list. case 'entity_type_alter': $group = $implementations['content_translation']; unset($implementations['content_translation']); $implementations['content_translation'] = $group; break; + // Move our entity_prepare_form hook implementation to the beginning of the list. + case 'entity_prepare_form': + $group = $implementations['content_translation']; + unset($implementations['content_translation']); + $implementations = array_merge(array('content_translation' => $group), $implementations); + break; } } @@ -536,3 +542,28 @@ function content_translation_page_attachments(&$page) { return; } } + +/** + * Implements hook_entity_prepare_form(). + * + * Provides an ability to edit an entity in a different language than the current content language. + */ +function content_translation_entity_prepare_form(EntityInterface $entity, $operation, FormStateInterface $form_state) { + if ($entity instanceof ContentEntityInterface) { + + $content_translation_target = \Drupal::request()->get('content_translation_target'); + + if ($content_translation_target !== NULL) { + /** @var \Drupal\Core\Entity\EntityFormInterface $form_object*/ + $form_object = $form_state->getFormObject(); + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity_from_form_object*/ + $entity_from_form_object = $form_object->getEntity(); + + if ($entity_from_form_object->language()->getId() != $content_translation_target && $entity_from_form_object->hasTranslation($content_translation_target)) { + $entity_from_form_object = $entity_from_form_object->getTranslation($content_translation_target); + $form_object->setEntity($entity_from_form_object); + $form_state->set('langcode', $content_translation_target); + } + } + } +} \ No newline at end of file