Skip to content
Snippets Groups Projects
Commit c4e5c497 authored by catch's avatar catch
Browse files

Issue #2835869 by hchonov, gease, tstoeckler: Prepare content translation for...

Issue #2835869 by hchonov, gease, tstoeckler: Prepare content translation for nested inline entity forms
parent 69e0b9fe
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -314,18 +314,18 @@ public function getSourceLangcode(FormStateInterface $form_state) { ...@@ -314,18 +314,18 @@ public function getSourceLangcode(FormStateInterface $form_state) {
public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) { public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$metadata = $this->manager->getTranslationMetadata($entity);
$form_object = $form_state->getFormObject(); $form_object = $form_state->getFormObject();
$form_langcode = $form_object->getFormLangcode($form_state); $form_langcode = $form_object->getFormLangcode($form_state);
$entity_langcode = $entity->getUntranslated()->language()->getId(); $entity_langcode = $entity->getUntranslated()->language()->getId();
$source_langcode = $this->getSourceLangcode($form_state);
$new_translation = !empty($source_langcode); $new_translation = $entity->isNewTranslation();
$translations = $entity->getTranslationLanguages(); $translations = $entity->getTranslationLanguages();
if ($new_translation) { if ($new_translation) {
// Make sure a new translation does not appear as existing yet. // Make sure a new translation does not appear as existing yet.
unset($translations[$form_langcode]); unset($translations[$form_langcode]);
} }
$is_translation = !$form_object->isDefaultFormLangcode($form_state); $is_translation = $new_translation || ($entity->language()->getId() != $entity_langcode);
$has_translations = count($translations) > 1; $has_translations = count($translations) > 1;
// Adjust page title to specify the current language being edited, if we // Adjust page title to specify the current language being edited, if we
...@@ -336,7 +336,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En ...@@ -336,7 +336,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
// When editing the original values display just the entity label. // When editing the original values display just the entity label.
if ($is_translation) { if ($is_translation) {
$t_args = ['%language' => $languages[$form_langcode]->getName(), '%title' => $entity->label(), '@title' => $title]; $t_args = ['%language' => $languages[$form_langcode]->getName(), '%title' => $entity->label(), '@title' => $title];
$title = empty($source_langcode) ? t('@title [%language translation]', $t_args) : t('Create %language translation of %title', $t_args); $title = $new_translation ? t('Create %language translation of %title', $t_args) : t('@title [%language translation]', $t_args);
} }
$form['#title'] = $title; $form['#title'] = $title;
} }
...@@ -344,6 +344,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En ...@@ -344,6 +344,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
// Display source language selector only if we are creating a new // Display source language selector only if we are creating a new
// translation and there are at least two translations available. // translation and there are at least two translations available.
if ($has_translations && $new_translation) { if ($has_translations && $new_translation) {
$source_langcode = $metadata->getSource();
$form['source_langcode'] = [ $form['source_langcode'] = [
'#type' => 'details', '#type' => 'details',
'#title' => t('Source language: @language', ['@language' => $languages[$source_langcode]->getName()]), '#title' => t('Source language: @language', ['@language' => $languages[$source_langcode]->getName()]),
...@@ -435,7 +436,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En ...@@ -435,7 +436,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
'#title' => t('Translation'), '#title' => t('Translation'),
'#tree' => TRUE, '#tree' => TRUE,
'#weight' => 10, '#weight' => 10,
'#access' => $this->getTranslationAccess($entity, $source_langcode ? 'create' : 'update')->isAllowed(), '#access' => $this->getTranslationAccess($entity, $new_translation ? 'create' : 'update')->isAllowed(),
'#multilingual' => TRUE, '#multilingual' => TRUE,
]; ];
...@@ -450,7 +451,6 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En ...@@ -450,7 +451,6 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
} }
// A new translation is enabled by default. // A new translation is enabled by default.
$metadata = $this->manager->getTranslationMetadata($entity);
$status = $new_translation || $metadata->isPublished(); $status = $new_translation || $metadata->isPublished();
// If there is only one published translation we cannot unpublish it, // If there is only one published translation we cannot unpublish it,
// since there would be nothing left to display. // since there would be nothing left to display.
...@@ -680,11 +680,6 @@ public function entityFormEntityBuild($entity_type, EntityInterface $entity, arr ...@@ -680,11 +680,6 @@ public function entityFormEntityBuild($entity_type, EntityInterface $entity, arr
$metadata->setPublished(!empty($values['status'])); $metadata->setPublished(!empty($values['status']));
$metadata->setCreatedTime(!empty($values['created']) ? strtotime($values['created']) : REQUEST_TIME); $metadata->setCreatedTime(!empty($values['created']) ? strtotime($values['created']) : REQUEST_TIME);
$source_langcode = $this->getSourceLangcode($form_state);
if ($source_langcode) {
$metadata->setSource($source_langcode);
}
$metadata->setOutdated(!empty($values['outdated'])); $metadata->setOutdated(!empty($values['outdated']));
if (!empty($values['retranslate'])) { if (!empty($values['retranslate'])) {
$this->retranslate($entity, $form_langcode); $this->retranslate($entity, $form_langcode);
......
...@@ -71,8 +71,9 @@ public static function create(ContainerInterface $container) { ...@@ -71,8 +71,9 @@ public static function create(ContainerInterface $container) {
* The language to be used as target. * The language to be used as target.
*/ */
public function prepareTranslation(ContentEntityInterface $entity, LanguageInterface $source, LanguageInterface $target) { public function prepareTranslation(ContentEntityInterface $entity, LanguageInterface $source, LanguageInterface $target) {
$source_langcode = $source->getId();
/* @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */ /* @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */
$source_translation = $entity->getTranslation($source->getId()); $source_translation = $entity->getTranslation($source_langcode);
$target_translation = $entity->addTranslation($target->getId(), $source_translation->toArray()); $target_translation = $entity->addTranslation($target->getId(), $source_translation->toArray());
// Make sure we do not inherit the affected status from the source values. // Make sure we do not inherit the affected status from the source values.
...@@ -88,6 +89,7 @@ public function prepareTranslation(ContentEntityInterface $entity, LanguageInter ...@@ -88,6 +89,7 @@ public function prepareTranslation(ContentEntityInterface $entity, LanguageInter
// creation time. // creation time.
$metadata->setAuthor($user); $metadata->setAuthor($user);
$metadata->setCreatedTime(REQUEST_TIME); $metadata->setCreatedTime(REQUEST_TIME);
$metadata->setSource($source_langcode);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment