Commit 4c8576c3 authored by catch's avatar catch

Issue #2022875 by Berdir, alexpott, Xano, swentel, tim.plunkett, vladan.me:...

Issue #2022875 by Berdir, alexpott, Xano, swentel, tim.plunkett, vladan.me: Resolve difference between submitForm(), submit(), and save() in EntityFormController.
parent 86da882d
......@@ -82,14 +82,40 @@ public function form(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']);
return array(
'submit' => array(
'#type' => 'submit',
'#value' => $this->getConfirmText(),
'#validate' => array(
array($this, 'validate'),
),
'#submit' => array(
array($this, 'submitForm'),
),
),
'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
);
}
// Prepare cancel link.
$actions['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
/**
* {@inheritdoc}
*
* The save() method is not used in ContentEntityConfirmFormBase. This
* overrides the default implementation that saves the entity.
*
* Confirmation forms should override submitForm() instead for their logic.
*/
public function save(array $form, FormStateInterface $form_state) {}
return $actions;
}
/**
* {@inheritdoc}
*
* The delete() method is not used in ContentEntityConfirmFormBase. This
* overrides the default implementation that redirects to the delete-form
* confirmation form.
*
* Confirmation forms should override submitForm() instead for their logic.
*/
public function delete(array $form, FormStateInterface $form_state) {}
}
......@@ -77,13 +77,40 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']);
// Prepare cancel link.
$actions['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
return $actions;
return array(
'submit' => array(
'#type' => 'submit',
'#value' => $this->getConfirmText(),
'#validate' => array(
array($this, 'validate'),
),
'#submit' => array(
array($this, 'submitForm'),
),
),
'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
);
}
/**
* {@inheritdoc}
*
* The save() method is not used in EntityConfirmFormBase. This overrides the
* default implementation that saves the entity.
*
* Confirmation forms should override submitForm() instead for their logic.
*/
public function save(array $form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*
* The delete() method is not used in EntityConfirmFormBase. This overrides
* the default implementation that redirects to the delete-form confirmation
* form.
*
* Confirmation forms should override submitForm() instead for their logic.
*/
public function delete(array $form, FormStateInterface $form_state) {}
}
......@@ -105,12 +105,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
/**
* Initialize the form state and the entity before the first form build.
*/
......@@ -203,12 +197,14 @@ protected function actionsElement(array $form, FormStateInterface $form_state) {
* many entity types.
*/
protected function actions(array $form, FormStateInterface $form_state) {
// @todo Rename the action key from submit to save.
// @todo Consider renaming the action key from submit to save. The impacts
// are hard to predict. For example, see
// language_configuration_element_process().
$actions['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save'),
'#validate' => array('::validate'),
'#submit' => array('::submit', '::save'),
'#submit' => array('::submitForm', '::save'),
);
if (!$this->entity->isNew() && $this->entity->hasLinkTemplate('delete-form')) {
......@@ -247,33 +243,30 @@ public function validate(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*
* This is the default entity object builder function. It is called before any
* other submit handler to build the new entity object to be passed to the
* other submit handler to build the new entity object to be used by the
* following submit handlers. At this point of the form workflow the entity is
* validated and the form state can be updated, this way the subsequently
* invoked handlers can retrieve a regular entity object to act on.
* invoked handlers can retrieve a regular entity object to act on. Generally
* this method should not be overridden unless the entity requires the same
* preparation for two actions, see \Drupal\comment\CommentFormController for
* an example with the save and preview actions.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
// Remove button and internal Form API values from submitted values.
form_state_values_clean($form_state);
$this->entity = $this->buildEntity($form, $form_state);
return $this->entity;
}
/**
* Form submission handler for the 'save' action.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
// @todo Perform common save operations.
return $this->entity->save();
}
/**
......
......@@ -116,18 +116,20 @@ public function buildEntity(array $form, FormStateInterface $form_state);
public function validate(array $form, FormStateInterface $form_state);
/**
* Updates the form's entity by processing this submission's values.
* Form submission handler for the 'save' action.
*
* Note: Before this can be safely invoked the entity form must have passed
* validation, i.e. only add this as form #submit handler if validation is
* added as well.
* Normally this method should be overridden to provide specific messages to
* the user and redirect the form after the entity has been saved.
*
* @param array $form
* A nested array form elements comprising the form.
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return int
* Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
*/
public function submit(array $form, FormStateInterface $form_state);
public function save(array $form, FormStateInterface $form_state);
/**
* Sets the string translation service for this form.
......
......@@ -134,13 +134,12 @@ public function validate(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
parent::submit($form, $form_state);
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
if ($this->plugin instanceof PluginFormInterface) {
$this->plugin->submitConfigurationForm($form, $form_state);
}
return $this->entity;
}
/**
......
......@@ -40,7 +40,7 @@ public function getCancelUrl() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->logger('user')->notice('Deleted action %aid (%action)', array('%aid' => $this->entity->id(), '%action' => $this->entity->label()));
......
......@@ -40,7 +40,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->logger('aggregator')->notice('Feed %feed deleted.', array('%feed' => $this->entity->label()));
drupal_set_message($this->t('The feed %feed has been deleted.', array('%feed' => $this->entity->label())));
......
......@@ -40,7 +40,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->deleteItems();
$form_state->setRedirectUrl($this->getCancelUrl());
......
......@@ -161,8 +161,8 @@ public function validate(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
parent::submit($form, $form_state);
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$entity = $this->entity;
// The Block Entity form puts all block plugin form elements in the
......
......@@ -40,7 +40,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message($this->t('The block %name has been removed.', array('%name' => $this->entity->label())));
$form_state->setRedirectUrl($this->getCancelUrl());
......
......@@ -34,6 +34,13 @@ class BlockContentForm extends ContentEntityForm {
*/
protected $languageManager;
/**
* The block content entity.
*
* @var \Drupal\block_content\BlockContentInterface
*/
protected $entity;
/**
* Constructs a BlockContentForm object.
*
......@@ -167,31 +174,16 @@ public function form(array $form, FormStateInterface $form_state) {
}
/**
* Overrides \Drupal\Core\Entity\EntityForm::submit().
*
* Updates the custom block object by processing the submitted values.
*
* This function can be called by a "Next" button of a wizard to update the
* form state's entity with the current step's values before proceeding to the
* next step.
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
// Build the block object from the submitted values.
$block = parent::submit($form, $form_state);
public function save(array $form, FormStateInterface $form_state) {
$block = $this->entity;
// Save as a new revision if requested to do so.
if (!$form_state->isValueEmpty('revision')) {
$block->setNewRevision();
}
return $block;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$block = $this->entity;
$insert = $block->isNew();
$block->save();
$context = array('@type' => $block->bundle(), '%info' => $block->label());
......
......@@ -86,7 +86,7 @@ public function form(array $form, FormStateInterface $form_state) {
}
/**
* Overrides \Drupal\Core\Entity\EntityForm::save().
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$block_type = $this->entity;
......
......@@ -54,7 +54,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message($this->t('Custom block %label has been deleted.', array('%label' => $this->entity->label())));
$this->logger('block_content')->notice('Custom block %label has been deleted.', array('%label' => $this->entity->label()));
......
......@@ -83,7 +83,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(t('Custom block type %label has been deleted.', array('%label' => $this->entity->label())));
$this->logger('block_content')->notice('Custom block type %label has been deleted.', array('%label' => $this->entity->label()));
......
......@@ -98,10 +98,8 @@ protected function actions(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*
* @see book_remove_button_submit()
*/
public function submit(array $form, FormStateInterface $form_state) {
public function save(array $form, FormStateInterface $form_state) {
$form_state->setRedirect(
'entity.node.canonical',
array('node' => $this->entity->id())
......
......@@ -245,7 +245,7 @@ protected function actions(array $form, FormStateInterface $form_state) {
'#value' => $this->t('Preview'),
'#access' => $preview_mode != DRUPAL_DISABLED,
'#validate' => array('::validate'),
'#submit' => array('::submit', '::preview'),
'#submit' => array('::submitForm', '::preview'),
);
return $element;
......@@ -301,12 +301,12 @@ public function buildEntity(array $form, FormStateInterface $form_state) {
}
/**
* Overrides Drupal\Core\Entity\EntityForm::submit().
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @var \Drupal\comment\CommentInterface $comment */
$comment = parent::submit($form, $form_state);
$comment = $this->entity;
// If the comment was posted by a registered user, assign the author's ID.
// @todo Too fragile. Should be prepared and stored in comment_form()
// already.
......
......@@ -141,7 +141,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$form_state->setRedirect('comment.type_list');
drupal_set_message($this->t('Comment type %label has been deleted.', array('%label' => $this->entity->label())));
......
......@@ -47,7 +47,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
// Delete the comment and its replies.
$this->entity->delete();
drupal_set_message($this->t('The comment and all its replies have been deleted.'));
......
......@@ -40,7 +40,7 @@ public function getCancelUrl() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(String::format('%label configuration has been deleted.', array('%label' => $this->entity->label())));
$form_state->setRedirectUrl($this->getCancelUrl());
......
......@@ -40,7 +40,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message($this->t('Contact form %label has been deleted.', array('%label' => $this->entity->label())));
$this->logger('contact')->notice('Contact form %label has been deleted.', array('%label' => $this->entity->label()));
......
......@@ -157,7 +157,7 @@ public function actions(array $form, FormStateInterface $form_state) {
$elements['preview'] = array(
'#value' => $this->t('Preview'),
'#validate' => array('::validate'),
'#submit' => array('::submit', '::preview'),
'#submit' => array('::submitForm', '::preview'),
);
return $elements;
}
......
......@@ -49,11 +49,9 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
parent::submit($form, $form_state);
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_type = $this->entity->getEntityType();
drupal_set_message(t('Deleted the %label @entity-type.', array('%label' => $this->entity->label(), '@entity-type' => $entity_type->getLowercaseLabel())));
drupal_set_message(t('Deleted the %label @entity-type.', array('%label' => $this->entity->label(), '@entity-type' => strtolower($entity_type->getLabel()))));
$this->entity->delete();
\Drupal::entityManager()->clearCachedFieldDefinitions();
$form_state->setRedirectUrl($this->getCancelUrl());
......
......@@ -68,7 +68,7 @@ public function getCancelUrl() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$field_storage = $this->entity->getFieldStorageDefinition();
$bundles = entity_get_bundles();
$bundle_label = $bundles[$this->entity->entity_type][$this->entity->bundle]['label'];
......
......@@ -24,8 +24,8 @@ public function form(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
parent::submit($form, $form_state);
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
drupal_set_message($this->t('Added text format %format.', array('%format' => $this->entity->label())));
return $this->entity;
}
......
......@@ -32,8 +32,8 @@ public function form(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
parent::submit($form, $form_state);
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
drupal_set_message($this->t('The text format %format has been updated.', array('%format' => $this->entity->label())));
return $this->entity;
}
......
......@@ -234,8 +234,8 @@ public function validate(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
parent::submit($form, $form_state);
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
// Add the submitted form values to the text format, and save it.
$format = $this->entity;
......
......@@ -47,7 +47,7 @@ public function getDescription() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->disable()->save();
drupal_set_message($this->t('Disabled text format %format.', array('%format' => $this->entity->label())));
......
......@@ -42,10 +42,10 @@ public function form(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$is_new = $this->entity->isNew();
$this->entity->forum_container = TRUE;
$term = parent::save($form, $form_state);
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = parent::buildEntity($form, $form_state);
$entity->forum_container = TRUE;
return $entity;
}
}
......@@ -17,8 +17,8 @@ class ImageStyleAddForm extends ImageStyleFormBase {
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
drupal_set_message($this->t('Style %name was created.', array('%name' => $this->entity->label())));
}
......
......@@ -62,7 +62,7 @@ public function form(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->set('replacementID', $form_state->getValue('replacement'));
$this->entity->delete();
drupal_set_message($this->t('Style %name was deleted.', array('%name' => $this->entity->label())));
......
......@@ -166,8 +166,8 @@ public function form(array $form, FormStateInterface $form_state) {
'add' => array(
'#type' => 'submit',
'#value' => $this->t('Add'),
'#validate' => array(array($this, 'effectValidate')),
'#submit' => array(array($this, 'effectSave')),
'#validate' => array('::effectValidate'),
'#submit' => array('::submitForm', '::effectSave'),
),
),
),
......@@ -202,20 +202,7 @@ public function effectValidate($form, FormStateInterface $form_state) {
* Submit handler for image effect.
*/
public function effectSave($form, FormStateInterface $form_state) {
// Update image effect weights.
if (!$form_state->isValueEmpty('effects')) {
$this->updateEffectWeights($form_state->getValue('effects'));
}
$this->entity->set('name', $form_state->getValue('name'));
$this->entity->set('label', $form_state->getValue('label'));
$status = parent::save($form, $form_state);
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('Changes to the style have been saved.'));
}
$this->save($form, $form_state);
// Check if this field has any configuration options.
$effect = $this->imageEffectManager->getDefinition($form_state->getValue('new'));
......@@ -249,13 +236,20 @@ public function effectSave($form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
// Update image effect weights.
if (!$form_state->isValueEmpty('effects')) {
$this->updateEffectWeights($form_state->getValue('effects'));
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
drupal_set_message($this->t('Changes to the style have been saved.'));
}
......
......@@ -47,7 +47,7 @@ public function getCancelUrl() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->flush();
drupal_set_message($this->t('The image style %name has been flushed.', array('%name' => $this->entity->label())));
$form_state->setRedirectUrl($this->getCancelUrl());
......
......@@ -77,7 +77,7 @@ public function form(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity->save();
parent::save($form, $form_state);
$form_state->setRedirectUrl($this->entity->urlInfo('edit-form'));
}
......
......@@ -106,7 +106,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
// @todo This should be replaced with $this->entity->delete() when the
// additional logic in language_delete() is ported.
$success = language_delete($this->entity->id());
......
......@@ -66,7 +66,7 @@ public function getCancelUrl() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$t_args = array('%title' => $this->entity->getTitle());
$this->entity->delete();
drupal_set_message($this->t('The menu link %title has been deleted.', $t_args));
......
......@@ -172,7 +172,7 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// Remove button and internal Form API values from submitted values.
parent::submit($form, $form_state);
parent::submitForm($form, $form_state);
$this->save($form, $form_state);
}
......
......@@ -92,7 +92,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRedirect('menu_ui.overview_page');
// Locked menus may not be deleted.
......
......@@ -72,7 +72,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->logger('content')->notice('@type: deleted %title.', array('@type' => $this->entity->bundle(), '%title' => $this->entity->label()));
$node_type_storage = $this->entityManager->getStorage('node_type');
......
......@@ -86,7 +86,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$t_args = array('%name' => $this->entity->label());