Commit 7e023865 authored by alexpott's avatar alexpott

Issue #2123867 by plach, vijaycs85, kfritsche, YesCT, LinL, larowlan | yched:...

Issue #2123867 by plach, vijaycs85, kfritsche, YesCT, LinL, larowlan | yched: Simplify/cleanup language handling in EntityFormController.
parent 69c5468b
......@@ -135,4 +135,17 @@ public function setFormDisplay(EntityFormDisplayInterface $form_display, FormSta
return $this;
}
/**
* Updates the form language to reflect any change to the entity language.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateFormLangcode(FormStateInterface $form_state) {
// Update the form language as it might have changed.
if ($form_state->hasValue('langcode') && $this->isDefaultFormLangcode($form_state)) {
$form_state->set('langcode', $form_state->getValue('langcode'));
}
}
}
......@@ -39,4 +39,26 @@ public function getFormDisplay(FormStateInterface $form_state);
*/
public function setFormDisplay(EntityFormDisplayInterface $form_display, FormStateInterface $form_state);
/**
* Returns the code identifying the active form language.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return string
* The form language code.
*/
public function getFormLangcode(FormStateInterface $form_state);
/**
* Checks whether the current form language matches the entity one.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return boolean
* Returns TRUE if the entity form language matches the entity one.
*/
public function isDefaultFormLangcode(FormStateInterface $form_state);
}
......@@ -131,15 +131,6 @@ public function form(array $form, FormStateInterface $form_state) {
// Add a process callback.
$form['#process'][] = '::processForm';
if (!isset($form['langcode'])) {
// If the form did not specify otherwise, default to keeping the existing
// language of the entity or defaulting to the site default language for
// new entities.
$form['langcode'] = array(
'#type' => 'value',
'#value' => !$entity->isNew() ? $entity->language()->id : language_default()->id,
);
}
return $form;
}
......@@ -232,7 +223,6 @@ protected function actions(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
$this->updateFormLangcode($form_state);
// @todo Remove this.
// Execute legacy global validation handlers.
$form_state->setValidateHandlers([]);
......@@ -269,34 +259,6 @@ public function save(array $form, FormStateInterface $form_state) {
return $this->entity->save();
}
/**
* {@inheritdoc}
*/
public function getFormLangcode(FormStateInterface $form_state) {
return $this->entity->language()->id;
}
/**
* {@inheritdoc}
*/
public function isDefaultFormLangcode(FormStateInterface $form_state) {
// The entity is not translatable, this is always the default language.
return TRUE;
}
/**
* Updates the form language to reflect any change to the entity language.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function updateFormLangcode(FormStateInterface $form_state) {
// Update the form language as it might have changed.
if ($form_state->hasValue('langcode') && $this->isDefaultFormLangcode($form_state)) {
$form_state->set('langcode', $form_state->getValue('langcode'));
}
}
/**
* {@inheritdoc}
*/
......
......@@ -17,28 +17,6 @@
*/
interface EntityFormInterface extends BaseFormIdInterface {
/**
* Returns the code identifying the active form language.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return string
* The form language code.
*/
public function getFormLangcode(FormStateInterface $form_state);
/**
* Checks whether the current form language matches the entity one.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return boolean
* Returns TRUE if the entity form language matches the entity one.
*/
public function isDefaultFormLangcode(FormStateInterface $form_state);
/**
* Sets the operation for this form.
*
......
......@@ -7,7 +7,7 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
......@@ -364,7 +364,7 @@ function content_translation_access(EntityInterface $entity, $op) {
*/
function content_translation_form_alter(array &$form, FormStateInterface $form_state) {
$form_object = $form_state->getFormObject();
if (!($form_object instanceof EntityFormInterface)) {
if (!($form_object instanceof ContentEntityFormInterface)) {
return;
}
$entity = $form_object->getEntity();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment