In a preprocess function I want to access the destination node id via the URL object

I cannot find any method that will return the node id for me but I can clearly see that it holds the node id. (see attached)

enter image description here

Any idea how I can get the node id without going the long way of generating it from the URL path?

  • 1
    $your_url_object->getRouteParameters() should do it I think from memory
    – Leigh
    Nov 10, 2020 at 9:48

1 Answer 1


Looks like you're using the latest Devel, 4.0.1, which sadly currently doesn't show the available methods tab unless you patch it.

If you had the available methods tab, you would've seen that you can retrieve using the public method getRouteParameters

$nid = $url_object->getRouteParameters()['node'];
  • 1
    hey thanks, that worked although just ['node'] at the end did the job just fine,
    – supersize
    Nov 10, 2020 at 9:58
  • Also thanks for the patch, do you know why this has been removed in the newest version? It is pretty handy.
    – supersize
    Nov 10, 2020 at 11:31

Your Answer

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

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