6c71e5f054b261dd377ffb75a964d81549cac7d1
[yaffs-website] / web / modules / contrib / inline_entity_form / src / TranslationHelper.php
1 <?php
2
3 namespace Drupal\inline_entity_form;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Provides content translation helpers.
10  */
11 class TranslationHelper {
12
13   /**
14    * Prepares the inline entity for translation.
15    *
16    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
17    *   The inline entity.
18    * @param \Drupal\Core\Form\FormStateInterface $form_state
19    *   The form state.
20    *
21    * @return \Drupal\Core\Entity\ContentEntityInterface
22    *   The prepared entity.
23    *
24    * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes().
25    */
26   public static function prepareEntity(ContentEntityInterface $entity, FormStateInterface $form_state) {
27     $form_langcode = $form_state->get('langcode');
28     if (empty($form_langcode) || !$entity->isTranslatable()) {
29       return $entity;
30     }
31
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);
44       }
45     }
46
47     if ($entity_langcode != $form_langcode && $entity->hasTranslation($form_langcode)) {
48       // Switch to the needed translation.
49       $entity = $entity->getTranslation($form_langcode);
50     }
51
52     return $entity;
53   }
54
55   /**
56    * Updates the entity langcode to match the form langcode.
57    *
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.
60    *
61    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
62    *   The entity.
63    * @param \Drupal\Core\Form\FormStateInterface $form_state
64    *   The current state of the form.
65    *
66    * @return bool
67    *   TRUE if the entity langcode was updated, FALSE otherwise.
68    */
69   public static function updateEntityLangcode(ContentEntityInterface $entity, $form_state) {
70     $changed = FALSE;
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()) {
75       return $changed;
76     }
77
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);
82       $changed = TRUE;
83     }
84
85     return $changed;
86   }
87
88   /**
89    * Determines whether there's a translation in progress.
90    *
91    * If the root entity is being translated, then all of the inline entities
92    * are candidates for translating as well.
93    *
94    * @param \Drupal\Core\Form\FormStateInterface $form_state
95    *   The form state.
96    *
97    * @return bool
98    *   TRUE if translating is in progress, FALSE otherwise.
99    *
100    * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes().
101    */
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.
107       return FALSE;
108     }
109     else {
110       return $form_langcode != $default_langcode;
111     }
112   }
113
114 }