I want to get the internal path of a node from its path alias. In Drupal 7 we could use drupal_get_normal_path() directly in the PHP template.

With Drupal 8, Is there a function to get the internal path from an alias inside a Twig template?

In my Twig template I have tried to use the url() function without success...

e.g. {% set path = url('myalias') %} ...

  • I haven't the node id. I want get the internal path (node/nid) from the alias. I need to get the nid of a node from the alias.
    – user28220
    May 15, 2018 at 12:35
  • Ah now I got it! Better do this in a preprocess function and then pass it over to Twig. drupal.stackexchange.com/a/197163/15055
    – leymannx
    May 15, 2018 at 12:45
  • Which template are you currently using?
    – leymannx
    May 15, 2018 at 12:50
  • Ok there is no possibility to use directly a drupal function in my twig template ? I need to generate several nids inside a views table template. I will see to create a custom function to pass an alias and get the related nid. Create a var in a preprocess function isn't very usefull for my case.
    – user28220
    May 15, 2018 at 14:47
  • 1
    I don't really understand what you're trying to do. If you have a view that displays nodes in a table then I would expect that the nid is definitely somewhere that is available to you. Might help if you add the your current template and what exactly you are trying to do with that path/nid. There is certainly a better way to get the nid than unaliasing the alias (which actually needs to a DB query)
    – Berdir
    May 15, 2018 at 18:54

1 Answer 1


You should not be running Drupal functions inside TWIG templates, the best approach is to use a pre-processor as mentioned in the comments. You can do this by setting a variable inside a hook being called either in your theme file or custom module and setting like this:

$vars['your_custom_variable'] = $unaliasedNodePath

And then inside your twig template you can use the variable set in your pre-processor like this:

{{ your_custom_variable }}

More information can be found here:


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.