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 $source_translation = $entity->getTranslation($source_langcode);
38 $entity->addTranslation($form_langcode, $source_translation->toArray());
39 $translation = $entity->getTranslation($form_langcode);
40 $translation->set('content_translation_source', $source_langcode);
41 // Make sure we do not inherit the affected status from the source values.
42 if ($entity->getEntityType()->isRevisionable()) {
43 $translation->setRevisionTranslationAffected(NULL);
47 if ($entity_langcode != $form_langcode && $entity->hasTranslation($form_langcode)) {
48 // Switch to the needed translation.
49 $entity = $entity->getTranslation($form_langcode);
56 * Updates the entity langcode to match the form langcode.
58 * Called on submit to allow the user to select a different language through
59 * the langcode form element, which is then transferred to form state.
61 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
63 * @param \Drupal\Core\Form\FormStateInterface $form_state
64 * The current state of the form.
67 * TRUE if the entity langcode was updated, FALSE otherwise.
69 public static function updateEntityLangcode(ContentEntityInterface $entity, $form_state) {
71 // This method is first called during form validation, at which point
72 // the 'langcode' form state flag hasn't been updated with the new value.
73 $form_langcode = $form_state->getValue(['langcode', 0, 'value'], $form_state->get('langcode'));
74 if (empty($form_langcode) || !$entity->isTranslatable()) {
78 $entity_langcode = $entity->language()->getId();
79 if ($entity_langcode != $form_langcode && !$entity->hasTranslation($form_langcode)) {
80 $langcode_key = $entity->getEntityType()->getKey('langcode');
81 $entity->set($langcode_key, $form_langcode);
89 * Determines whether there's a translation in progress.
91 * If the root entity is being translated, then all of the inline entities
92 * are candidates for translating as well.
94 * @param \Drupal\Core\Form\FormStateInterface $form_state
98 * TRUE if translating is in progress, FALSE otherwise.
100 * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes().
102 public static function isTranslating(FormStateInterface $form_state) {
103 $form_langcode = $form_state->get('langcode');
104 $default_langcode = $form_state->get('entity_default_langcode');
105 if (empty($form_langcode) && empty($default_langcode)) {
106 // The top-level form is not a content entity form.
110 return $form_langcode != $default_langcode;