You are here

public function WorkflowTransitionRevertForm::submitForm in Workflow 8

This is the default entity object builder function. It is called before any 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. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides EntityForm::submitForm

File

src/Form/WorkflowTransitionRevertForm.php, line 80

Class

WorkflowTransitionRevertForm
Implements a form to revert an entity to a previous state.

Namespace

Drupal\workflow\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // If Rules is available, signal the reversion.
  // @todo Move this Rules_invoke_event to hook outside this module.
  if (\Drupal::moduleHandler()
    ->moduleExists('rules')) {
    rules_invoke_event('workflow_state_reverted', $this->entity);
  }
  $transition = $this
    ->prepareRevertedTransition($this->entity);

  // The entity will be updated when the transition is executed. Keep the
  // original one for the confirmation message.
  $previous_sid = $transition
    ->getToSid();

  // Force the transition because it's probably not valid.
  $transition
    ->force(TRUE);
  $new_sid = workflow_execute_transition($transition, TRUE);
  $comment = $previous_sid == $new_sid ? $this
    ->t('State is reverted.') : $this
    ->t('State could not be reverted.');
  $this
    ->messenger()
    ->addWarning($comment);
  $form_state
    ->setRedirectUrl($this
    ->getUrl($transition));
}