X-Git-Url: https://yaffs.net/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Finline_entity_form%2Fsrc%2FTranslationHelper.php;fp=web%2Fmodules%2Fcontrib%2Finline_entity_form%2Fsrc%2FTranslationHelper.php;h=6c71e5f054b261dd377ffb75a964d81549cac7d1;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/inline_entity_form/src/TranslationHelper.php b/web/modules/contrib/inline_entity_form/src/TranslationHelper.php new file mode 100644 index 000000000..6c71e5f05 --- /dev/null +++ b/web/modules/contrib/inline_entity_form/src/TranslationHelper.php @@ -0,0 +1,114 @@ +get('langcode'); + if (empty($form_langcode) || !$entity->isTranslatable()) { + return $entity; + } + + $entity_langcode = $entity->language()->getId(); + if (self::isTranslating($form_state) && !$entity->hasTranslation($form_langcode)) { + // Create a translation from the source language values. + $source = $form_state->get(['content_translation', 'source']); + $source_langcode = $source ? $source->getId() : $entity_langcode; + $source_translation = $entity->getTranslation($source_langcode); + $entity->addTranslation($form_langcode, $source_translation->toArray()); + $translation = $entity->getTranslation($form_langcode); + $translation->set('content_translation_source', $source_langcode); + // Make sure we do not inherit the affected status from the source values. + if ($entity->getEntityType()->isRevisionable()) { + $translation->setRevisionTranslationAffected(NULL); + } + } + + if ($entity_langcode != $form_langcode && $entity->hasTranslation($form_langcode)) { + // Switch to the needed translation. + $entity = $entity->getTranslation($form_langcode); + } + + return $entity; + } + + /** + * Updates the entity langcode to match the form langcode. + * + * Called on submit to allow the user to select a different language through + * the langcode form element, which is then transferred to form state. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return bool + * TRUE if the entity langcode was updated, FALSE otherwise. + */ + public static function updateEntityLangcode(ContentEntityInterface $entity, $form_state) { + $changed = FALSE; + // This method is first called during form validation, at which point + // the 'langcode' form state flag hasn't been updated with the new value. + $form_langcode = $form_state->getValue(['langcode', 0, 'value'], $form_state->get('langcode')); + if (empty($form_langcode) || !$entity->isTranslatable()) { + return $changed; + } + + $entity_langcode = $entity->language()->getId(); + if ($entity_langcode != $form_langcode && !$entity->hasTranslation($form_langcode)) { + $langcode_key = $entity->getEntityType()->getKey('langcode'); + $entity->set($langcode_key, $form_langcode); + $changed = TRUE; + } + + return $changed; + } + + /** + * Determines whether there's a translation in progress. + * + * If the root entity is being translated, then all of the inline entities + * are candidates for translating as well. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + * + * @return bool + * TRUE if translating is in progress, FALSE otherwise. + * + * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes(). + */ + public static function isTranslating(FormStateInterface $form_state) { + $form_langcode = $form_state->get('langcode'); + $default_langcode = $form_state->get('entity_default_langcode'); + if (empty($form_langcode) && empty($default_langcode)) { + // The top-level form is not a content entity form. + return FALSE; + } + else { + return $form_langcode != $default_langcode; + } + } + +}