commit 4780fb9873bde0006ec9d3f531a6a673071db62c Author: Francesco Placella Date: Sat Dec 13 17:24:19 2014 +0100 Fixed #137 and #138. diff --git a/core/lib/Drupal/Core/Entity/ContentEntityForm.php b/core/lib/Drupal/Core/Entity/ContentEntityForm.php index 181e954..5815799 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityForm.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityForm.php @@ -51,6 +51,9 @@ public static function create(ContainerInterface $container) { public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $this->getFormDisplay($form_state)->buildForm($this->entity, $form, $form_state); + // There are use cases for modules to act both before and after form + // language being updated, thus we perform the update in an entity builder + // so both cases can be supported. $form['#entity_builders']['update_form_langcode'] = [$this, 'updateFormLangcode']; return $form; } @@ -69,15 +72,17 @@ public function validate(array $form, FormStateInterface $form_state) { } /** - * Initialize the form state and the entity before the first form build. + * Initializes the form state and the entity before the first form build. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. */ protected function init(FormStateInterface $form_state) { // Ensure we act on the translation object corresponding to the current form // language. + $this->initFormLangcodes($form_state); $langcode = $this->getFormLangcode($form_state); $this->entity = $this->entity->getTranslation($langcode); - // Initialize default form langcode status. - $this->isDefaultFormLangcode($form_state); $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation()); $this->setFormDisplay($form_display, $form_state); @@ -86,15 +91,33 @@ protected function init(FormStateInterface $form_state) { } /** - * {@inheritdoc} + * Initializes form language code values. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. */ - public function getFormLangcode(FormStateInterface $form_state) { + protected function initFormLangcodes(FormStateInterface $form_state) { + // Store the entity default language to allow checking whether the form is + // dealing with the original entity or a translation. + if (!$form_state->has('entity_default_langcode')) { + $form_state->set('entity_default_langcode', $this->entity->getUntranslated()->language()->getId()); + } + // This value might have been explicitly populated to work with a particular + // entity translation. If not we fall back to the most proper language based + // on contextual information. if (!$form_state->has('langcode')) { // Imply a 'view' operation to ensure users edit entities in the same // language they are displayed. This allows to keep contextual editing // working also for multilingual entities. $form_state->set('langcode', $this->entityManager->getTranslationFromContext($this->entity)->language()->getId()); } + } + + /** + * {@inheritdoc} + */ + public function getFormLangcode(FormStateInterface $form_state) { + $this->initFormLangcodes($form_state); return $form_state->get('langcode'); } @@ -102,11 +125,8 @@ public function getFormLangcode(FormStateInterface $form_state) { * {@inheritdoc} */ public function isDefaultFormLangcode(FormStateInterface $form_state) { - if (!$form_state->has('default_form_langcode')) { - $value = $this->getFormLangcode($form_state) == $this->entity->getUntranslated()->language()->getId(); - $form_state->set('default_form_langcode', $value); - } - return $form_state->get('default_form_langcode'); + $this->initFormLangcodes($form_state); + return $form_state->get('langcode') == $form_state->get('entity_default_langcode'); } /** diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index acd8845..2ac9477 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -267,8 +267,9 @@ public function form(array $form, FormStateInterface $form_state) { '#languages' => LanguageInterface::STATE_CONFIGURABLE, '#default_value' => $user_preferred_langcode, '#description' => $user_language_added ? $this->t("This account's preferred language for emails and site presentation.") : $this->t("This account's preferred language for emails."), - // This is used to explain the user preferred language and entity language - // are synchronized and can be removed if a different behavior is desired. + // This is used to explain that user preferred language and entity + // language are synchronized. It can be removed if a different behavior is + // desired. '#pre_render' => ['user_langcode' => [$this, 'alterPreferredLangcodeDescription']], ); @@ -310,10 +311,10 @@ public function form(array $form, FormStateInterface $form_state) { * @return array * The preferred language form element. */ - public function alterPreferredLangcodeDescription($element) { + public function alterPreferredLangcodeDescription(array $element) { // Only add to the description if the form element has a description. if (isset($element['#description'])) { - $element['#description'] .= ' ' . t("This is also assumed to be the primary language of this account's profile information."); + $element['#description'] .= ' ' . $this->t("This is also assumed to be the primary language of this account's profile information."); } return $element; } @@ -330,7 +331,7 @@ public function alterPreferredLangcodeDescription($element) { * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ - public function syncUserLangcode($entity_type_id, UserInterface $user, &$form, &$form_state) { + public function syncUserLangcode($entity_type_id, UserInterface $user, array &$form, FormStateInterface &$form_state) { $user->langcode = $user->preferred_langcode; }