8.2.x FormState.php public FormState::setRedirect($route_name, array $route_parameters = array(), array $options = array())
8.0.x FormState.php public FormState::setRedirect($route_name, array $route_parameters = array(), array $options = array())
8.1.x FormState.php public FormState::setRedirect($route_name, array $route_parameters = array(), array $options = array())
8.3.x FormState.php public 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
      ]);
Jodan’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);
  }