Sets the redirect for the form.

Sets the redirect for the form.


taggartj’s picture

     $yaml_form_submission_id = 10;
        '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,

    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]);