2

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

  • What is the value of Url::fromRoute('entity.node.edit_form',['node', $entity->id()])->toString(); – cilefen Feb 14 '16 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... – user2137454 Feb 15 '16 at 0:45
  • Sorry, it should be: Url::fromRoute('entity.node.edit_form',['node' => $entity->id()])->toString(); – cilefen Feb 15 '16 at 18:33
  • The value returned is : /system/node/50/edit. But the view page is always loaded by default – user2137454 Feb 15 '16 at 20:19
5

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.

  • "To find the route names, use the Drupal console drupal router:debug" , Ho ! thank you for this tips :) – user2137454 Feb 14 '16 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 Feb 14 '16 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 – user2137454 Feb 14 '16 at 23:01
2

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.

0

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, privacy policy and cookie policy

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