8.5.x FormState.php FormState::setRedirect($route_name, array $route_parameters = [], array $options = [])
8.0.x FormState.php FormState::setRedirect($route_name, array $route_parameters = array(), array $options = array())
8.1.x FormState.php FormState::setRedirect($route_name, array $route_parameters = array(), array $options = array())
8.2.x FormState.php FormState::setRedirect($route_name, array $route_parameters = array(), array $options = array())
8.3.x FormState.php FormState::setRedirect($route_name, array $route_parameters = [], array $options = [])
8.4.x FormState.php FormState::setRedirect($route_name, array $route_parameters = [], array $options = [])
8.6.x FormState.php FormState::setRedirect($route_name, array $route_parameters = [], array $options = [])

Sets the redirect for the form.

Parameters

string $route_name: The name of the route

array $route_parameters: (optional) An associative array of parameter names and values.

array $options: (optional) An associative array of additional options. See \Drupal\Core\Url for the available keys.

Return value

$this

Overrides FormStateInterface::setRedirect

See also

\Drupal\Core\Form\FormSubmitterInterface::redirectForm()

File

core/lib/Drupal/Core/Form/FormState.php, line 1001

Class

FormState
Stores information about the state of a form.

Namespace

Drupal\Core\Form

Code

public function setRedirect($route_name, array $route_parameters = array(), array $options = array()) {
  $url = new Url($route_name, $route_parameters, $options);
  return $this
    ->setRedirectUrl($url);
}

Comments

taggartj’s picture

     $yaml_form_submission_id = 10;
     //yamlform/{yamlform}/submissions/{yamlform_submission}/edit
      $form_state->setRedirect('entity.yamlform.user.submission.edit',[
        'yamlform' => 'my_yaml_form_id',
        'yamlform_submission' => $yaml_form_submission_id
      ]);
Pascal-’s picture

'type' is the node machine name
'title' is the title you want to give the new node
you can add more 'field_names' that are used in your node

use Drupal\node\Entity\Node;
use Drupal\Core\Url;

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

    //save Company as node
    $newCompanyNode = Node::create([
      'type' => 'company',
      'title' => $form_state->getValue('company'),
      //'field_name' => $value,
    ]);

    $newCompanyNode->save();
    drupal_set_message('Your company has been registered.', 'status');
    $url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $newCompanyNode->id()]);
    return $form_state->setRedirectUrl($url);
  }
miiimooo’s picture

This took me a while to figure out. Let's say you have a view with a contextual argument (e.g. a tid or nid) and want to redirect with an argument - e.g. /example-view/123


list($view_id, $display_id) = ['my_view', 'page_1'];
$route_name = "view.$view_id.$display_id";
$argument = $form_state->getValue('value');
$form_state->setRedirect($route_name, ['arg_0' => $argument]);