Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By vivefree on
How do I get the node url alias from the nid?
I have the nid: 37
I don't want: 'node/37' but 'my-custom-alias'
Comments
You can use the
You can use the drupal_get_path_alias function e.g.
http://api.drupal.org/api/drupal/includes--path.inc/function/drupal_get_...
thanks!
thanks!
I know it's a pretty old
I know it's a pretty old question but since I just found it (6.5 years later!!!), I would like to add that at least in Drupal 7, nid = 0123 and nid = 123 are not the same for drupal_get_path_alias. So cast your variable to an integer before using it in the path. i.e.
(In my site users can create "automatic" links to nodes by simply writing (#123), and I was trying to avoid that adding a leading zero broke the nice path aliases)
thanks
Small Typo correction.
Drupal 8?
How to do in Drupal 8?
You can below code to get
You can below code to get alias in Drupal 8:
Drupal 8 example
function mythemename_preprocess_node(&$variables) {
$variables['path_alias'] = \Drupal::service('path.alias_manager')->getAliasByPath($system_path, $langcode);
}
I did that to reach the path in the twig {{ path_alias }}
but I have a error
InvalidArgumentException: Source path has to start with a slash. in Drupal\Core\Path\AliasManager->getAliasByPath() (line 186 of core/lib/Drupal/Core/Path/AliasManager.php).
espacedomicile_preprocess_node(Array, 'node', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 435)
Try passing '/node/nid' as
Try passing '/node/nid' as $system_path instead of 'node/nid'.
Full example:
Full working example for Drupal 8:
Its important to know that if you use the wrong language, the result will be different and does not trigger any warning or error.
So for example: