diff --git a/core/modules/entity/entity.module b/core/modules/entity/entity.module index 74f2fcf..c63b2f8 100644 --- a/core/modules/entity/entity.module +++ b/core/modules/entity/entity.module @@ -467,12 +467,13 @@ function entity_form_id(EntityInterface $entity, $operation = 'default') { * @return * A $form_state array already filled the entity form controller. */ -function entity_form_state_defaults(EntityInterface $entity, $operation = 'default') { +function entity_form_state_defaults(EntityInterface $entity, $operation = 'default', $langcode = NULL) { $form_state = array(); $controller = entity_form_controller($entity->entityType(), $operation); $form_state['build_info']['callback'] = array($controller, 'build'); $form_state['build_info']['base_form_id'] = $entity->entityType() . '_form'; $form_state['build_info']['args'] = array($entity); + $form_state['langcode'] = $langcode; return $form_state; } @@ -506,8 +507,8 @@ function entity_form_submit(EntityInterface $entity, $operation = 'default', &$f * @return * The processed form for the given entity and operation. */ -function entity_get_form(EntityInterface $entity, $operation = 'default') { - $form_state = entity_form_state_defaults($entity, $operation); +function entity_get_form(EntityInterface $entity, $operation = 'default', $langcode = NULL) { + $form_state = entity_form_state_defaults($entity, $operation, $langcode); $form_id = entity_form_id($entity, $operation); return drupal_build_form($form_id, $form_state); } diff --git a/core/modules/entity/lib/Drupal/entity/EntityFormController.php b/core/modules/entity/lib/Drupal/entity/EntityFormController.php index 4f1263c..72c01de 100644 --- a/core/modules/entity/lib/Drupal/entity/EntityFormController.php +++ b/core/modules/entity/lib/Drupal/entity/EntityFormController.php @@ -208,11 +208,26 @@ class EntityFormController implements EntityFormControllerInterface { * Implements Drupal\entity\EntityFormControllerInterface::getFormLangcode(). */ public function getFormLangcode($form_state) { - // @todo Introduce a new form language type (see hook_language_types_info()) - // to be used as the default active form language, should it be missing, so - // that entity forms can be used to submit multilingual values. - $language = $this->getEntity($form_state)->language(); - return !empty($language->langcode) ? $language->langcode : NULL; + $entity = $this->getEntity($form_state); + $translations = $entity->translations(); + + if (!empty($form_state['langcode'])) { + $langcode = $form_state['langcode']; + } + else { + // If no form langcode was provided we default to the current content + // language and inspect existing translations to find a valid fallback, + // if any. + $langcode = language(LANGUAGE_TYPE_CONTENT)->langcode; + $fallback = language_multilingual() ? language_fallback_get_candidates() : array(); + while (!empty($langcode) && !isset($translations[$langcode])) { + $langcode = array_shift($fallback); + } + } + + // If the site is not multilingual or no translation for the given form + // language is available, fall back to the entity language. + return !empty($langcode) ? $langcode : $entity->language()->langcode; } /**