3 namespace Drupal\inline_entity_form;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Form\FormStateInterface;
9 * Provides content translation helpers.
11 class TranslationHelper {
14 * Prepares the inline entity for translation.
16 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
18 * @param \Drupal\Core\Form\FormStateInterface $form_state
21 * @return \Drupal\Core\Entity\ContentEntityInterface
22 * The prepared entity.
24 * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes()
26 public static function prepareEntity(ContentEntityInterface $entity, FormStateInterface $form_state) {
27 $form_langcode = $form_state->get('langcode');
28 if (empty($form_langcode) || !$entity->isTranslatable()) {
32 $entity_langcode = $entity->language()->getId();
33 if (self::isTranslating($form_state) && !$entity->hasTranslation($form_langcode)) {
34 // Create a translation from the source language values.
35 $source = $form_state->get(['content_translation', 'source']);
36 $source_langcode = $source ? $source->getId() : $entity_langcode;
37 if (!$entity->hasTranslation($source_langcode)) {
38 $entity->addTranslation($source_langcode, $entity->toArray());
40 $source_translation = $entity->getTranslation($source_langcode);
41 $entity->addTranslation($form_langcode, $source_translation->toArray());
42 $translation = $entity->getTranslation($form_langcode);
43 $translation->set('content_translation_source', $source_langcode);
44 // Make sure we do not inherit the affected status from the source values.
45 if ($entity->getEntityType()->isRevisionable()) {
46 $translation->setRevisionTranslationAffected(NULL);
50 if ($entity_langcode != $form_langcode && $entity->hasTranslation($form_langcode)) {
51 // Switch to the needed translation.
52 $entity = $entity->getTranslation($form_langcode);
59 * Updates the entity langcode to match the form langcode.
61 * Called on submit to allow the user to select a different language through
62 * the langcode form element, which is then transferred to form state.
64 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
66 * @param \Drupal\Core\Form\FormStateInterface $form_state
67 * The current state of the form.
70 * TRUE if the entity langcode was updated, FALSE otherwise.
72 public static function updateEntityLangcode(ContentEntityInterface $entity, $form_state) {
74 // This method is first called during form validation, at which point
75 // the 'langcode' form state flag hasn't been updated with the new value.
76 $form_langcode = $form_state->getValue(['langcode', 0, 'value'], $form_state->get('langcode'));
77 if (empty($form_langcode) || !$entity->isTranslatable()) {
81 $entity_langcode = $entity->language()->getId();
82 if ($entity_langcode != $form_langcode && !$entity->hasTranslation($form_langcode)) {
83 $langcode_key = $entity->getEntityType()->getKey('langcode');
84 $entity->set($langcode_key, $form_langcode);
92 * Determines whether there's a translation in progress.
94 * If the root entity is being translated, then all of the inline entities
95 * are candidates for translating as well.
97 * @param \Drupal\Core\Form\FormStateInterface $form_state
101 * TRUE if translating is in progress, FALSE otherwise.
103 * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes()
105 public static function isTranslating(FormStateInterface $form_state) {
106 $form_langcode = $form_state->get('langcode');
107 $default_langcode = $form_state->get('entity_default_langcode');
108 if (empty($form_langcode) && empty($default_langcode)) {
109 // The top-level form is not a content entity form.
113 return $form_langcode != $default_langcode;