Updated all the contrib modules to their latest versions.
[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       if (!$entity->hasTranslation($source_langcode)) {
38         $entity->addTranslation($source_langcode, $entity->toArray());
39       }
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);
47       }
48     }
49
50     if ($entity_langcode != $form_langcode && $entity->hasTranslation($form_langcode)) {
51       // Switch to the needed translation.
52       $entity = $entity->getTranslation($form_langcode);
53     }
54
55     return $entity;
56   }
57
58   /**
59    * Updates the entity langcode to match the form langcode.
60    *
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.
63    *
64    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
65    *   The entity.
66    * @param \Drupal\Core\Form\FormStateInterface $form_state
67    *   The current state of the form.
68    *
69    * @return bool
70    *   TRUE if the entity langcode was updated, FALSE otherwise.
71    */
72   public static function updateEntityLangcode(ContentEntityInterface $entity, $form_state) {
73     $changed = FALSE;
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()) {
78       return $changed;
79     }
80
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);
85       $changed = TRUE;
86     }
87
88     return $changed;
89   }
90
91   /**
92    * Determines whether there's a translation in progress.
93    *
94    * If the root entity is being translated, then all of the inline entities
95    * are candidates for translating as well.
96    *
97    * @param \Drupal\Core\Form\FormStateInterface $form_state
98    *   The form state.
99    *
100    * @return bool
101    *   TRUE if translating is in progress, FALSE otherwise.
102    *
103    * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes()
104    */
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.
110       return FALSE;
111     }
112     else {
113       return $form_langcode != $default_langcode;
114     }
115   }
116
117 }