Redirect after submitting custom entity form

by Coder1   Last Updated December 15, 2017 22:07 PM

I'm working with a custom entity and would like to programmatically set the redirect based on submitted values.

When I set the redirect path however, the user is redirected to the canonical path instead of the path I'm setting in the redirect.

In the file mymodule/src/Form/MyEntityForm.php, I'm doing something like this:

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    // Note: Same results with the order of the next 2 lines reversed.
    parent::submitForm($form, $form_state);
    $form_state->setRedirect('mymodule.overview', ['state' => 'CA']);
  }

So how would I set the destination of an entity form submission dynamically?

Tags : 8 entities


Answers 1


Probably you need to override EntityFormInterface::save():

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

submitForm() is the first submit handler called and should not be used to override submit actions.

4k4
4k4
December 15, 2017 23:06 PM

Related Questions




How to create a custom template for entities?

Updated April 07, 2015 03:03 AM


Drupal 8 Custom Entities from a UI?

Updated July 14, 2015 15:03 PM