6

I created a custom module, It generate a custom Form in a page. When I submit my form, I create a new node. So, I am redirect to the form page but I would like be redirect to the new node page.

I add this line at the end of my submitForm function :

$form_state->setRedirect('node/'.$mynewnode->id());

But when I submit my form from the website, I get the empty-white-screen-of-the-death. My apache log :

...
Uncaught PHP Exception Symfony\\Component\\Routing\\Exception\\RouteNotFoundException: "Route "node/153" does not exist.
...

And then, when I go manually to the node/153 page, there isn't any problem. The page work well.

My question : where is my mistake ? How can I set the redirection after create the new node ? Is it possible from the submitForm function ?

7

I think if you want to use the setRedirect function then you might want to look at its documentation found https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormState.php/function/FormState%3A%3AsetRedirect/8

You can then base the ff. code from FormSubmitter::redirectForm or FormSubmitterInterface::redirectForm. So your code might look something like:

$form_state->setRedirect('entity.node.canonical',
  array('node' => $mynewnode->id()),
));

You can also find out where is "entity.node.canonical" from by searching in core/modules/node and where it is declared and what it's about.

Hope everything's going great!

  • Awesome! You're welcome! :) – Leolando Tan Dec 6 '15 at 21:43
  • 1
    How to setup $form_state->setRedirect() to a path alias instead of using nid? – ARUN Jul 5 '17 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.