4

From a hook (not form_alter), I want redirect the user to a node edit page (like mywebsite/node/[nid]/edit) How can I do that with the RedirectResponse class?

After searches, I found the following code.

new RedirectResponse(\Drupal::url('route.name'));

How can I get/find the route.name for a node edit page?

Update :

According the cilefen's answer =) From my entity_insert hook :

function article_entity_insert(EntityInterface $entity) {
  if ($entity->bundle() == "article"){
    Url::fromRoute('entity.node.edit_form',['node', $entity->id()]); //But when I create a new node, the "view" page is loaded and not the edit page
    dpm($entity->id()); //show the node id
  }
}

Update 2 :

The command

dpm(Url::fromRoute('entity.node.edit_form',['node', $entity->id()]));

return

Drupal\Core\Url Object
(
    [urlGenerator:protected] => 
    [urlAssembler:protected] => 
    [accessManager:protected] => 
    [routeName:protected] => entity.node.edit_form
    [routeParameters:protected] => Array
        (
            [0] => node
            [1] => 31
        )

    [options:protected] => Array
        (
        )

    [external:protected] => 
    [unrouted:protected] => 
    [uri:protected] => 
    [internalPath:protected] => 
    [_serviceIds:protected] => Array
        (
        )

)

But the command

dpm(Url::fromRoute('entity.node.edit_form',['node', $entity->id()])->toString());

Return this error :

[Mon Feb 15 01:38:30.081002 2016] [:error] [pid 10219] [client 192.168.1.48:49246] Uncaught PHP Exception Drupal\Core\Entity\EntityStorageException: "Some mandatory parameters are missing ("node") to generate a URL for route "entity.node.edit_form"." at /var/www/system/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php line 757, referer: http://192.168.1.44/system/node/add/article

4
  • What is the value of Url::fromRoute('entity.node.edit_form',['node', $entity->id()])->toString();
    – mona lisa
    Commented Feb 14, 2016 at 23:57
  • Thank you for your help :) I update my main post (update 2), I get a strange error when I use toString(), I don't understand why... Commented Feb 15, 2016 at 0:45
  • Sorry, it should be: Url::fromRoute('entity.node.edit_form',['node' => $entity->id()])->toString();
    – mona lisa
    Commented Feb 15, 2016 at 18:33
  • The value returned is : /system/node/50/edit. But the view page is always loaded by default Commented Feb 15, 2016 at 20:19

3 Answers 3

9

Note that \Drupal::url() is deprecated. You should use the static methods of the Url class to create URLs from routes.

To find the route names, use the Drupal console drupal router:debug. From that I found:

 entity.node.edit_form  /node/{node}/edit

I think what you need is Url::fromRoute('entity.node.edit_form', ['node' => 1]) where 1 is the node ID to be edited.

3
  • "To find the route names, use the Drupal console drupal router:debug" , Ho ! thank you for this tips :) Commented Feb 14, 2016 at 19:00
  • If you have the node object that you want to redirect to, you can also do this: $node->toUrl('edit-form')->toString()
    – Berdir
    Commented Feb 14, 2016 at 20:32
  • Hmmm I used "Url::fromRoute" from an entity_insert hook, but when I create a new node, Im not redirect to the edit page but the view page :/ I update my main post with my code Commented Feb 14, 2016 at 23:01
4

From your controller class, you can use the redirect() method like so :

return $this->redirect('entity.node.edit_form', ['node' => $node_id]);

For other ways of doing this, see drupal_goto() has been removed.

There's no need to fiddle with Urls or string conversion as discussed in the other answer.

1

You have:

Url::fromRoute('entity.node.edit_form', ['node', $entity->id()])->toString()
                                               ^^

I think you want:

Url::fromRoute('entity.node.edit_form', ['node' => $entity->id()])->toString();
                                               ^^^^

The first has an indexed array as the second parameter to Url::fromRoute(), the second has a named array as the second parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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