85 votes
Accepted

How can I get the path alias from a node ID or node object?

To get the path for a node id use AliasManager::getAliasByPath: Drupal 8 $alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$nid); Drupal 9 $alias = \Drupal::service('...
4uk4's user avatar
  • 99.6k
37 votes
Accepted

How can I get the node ID from a path alias?

I'm not sure if there's a direct function, but one route is to use the path alias manager service to lookup the internal path then regex it for a valid node id to perform an object load on: $path = \...
Shawn Conn's user avatar
  • 26.9k
37 votes

How can I get the path alias from a node ID or node object?

If you only have the nid, 4k4's solution of $alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$nid); Works great. But if you already have a node object you can skip a step ...
Amy's user avatar
  • 5,680
30 votes

How do I get the current path alias or path?

In Drupal 8 you can do this with Twig: {{ url('<current>') }} example: <a href="{{ url('<current>') }}">{{ 'Reload'|t }}</a> from: https://www.drupal.org/docs/8/theming/...
Nicola De Lazzari's user avatar
23 votes

Is it possible to programmatically add a path alias to a programmatically created node?

As of Drupal 8.8, path aliases are now entities, and the method of creating a new alias has changed. To programmatically save an alias, you can do the following: $path_alias = \Drupal\path_alias\...
Jaypan's user avatar
  • 17.5k
23 votes

How can I get the path alias from a node ID or node object?

What about using Url: use Drupal\Core\Url; ... $url = Url::fromRoute('entity.node.canonical', ['node' => $nid])->toString();
Kevin's user avatar
  • 27.8k
16 votes

Get node alias in template

You can use path('entity.node.canonical', {'node': dd.nid}) in the template file. The markup in the template file would look like this. <li><a href="{{path('entity.node.canonical', {'node': ...
Nitesh Sethia's user avatar
15 votes

Is it possible to programmatically add a path alias to a programmatically created node?

To create the alias, I first check if the alias was set in the user interface, and if it is empty then I set it manually using the label(). use Drupal\Core\Entity\EntityInterface; /** * ...
Jason Pascoe's user avatar
15 votes
Accepted

Get node alias in template

In Summary if you want to generate Relative Url use path and if you want to generate Absolute Url use url So the solution for this question is for relative URL path('entity.node.canonical', {'node'...
Yuseferi's user avatar
  • 22k
14 votes

How can I get the node ID from a path alias?

You can use the following code to get and load an entity and have access to its values. use \Drupal\Core\Url; $alias = \Drupal::service('path.alias_manager')->getPathByAlias('/etapes-de-la-...
Adrian Cid Almaguer's user avatar
12 votes
Accepted

How do I translate view's page path alias?

On my multilingual Drupal 8 sites, I go to /admin/config/search/path/add for adding URL alias, which asks you to select a language for this alias. Is this what you were looking for?
Gaius's user avatar
  • 491
12 votes

How can I get the path alias from a node ID or node object?

Best way to get node URL from node object- Absolute Path : $node->toUrl()->setAbsolute()->toString(); Relative Path : $node->toUrl()->toString(); It will also handle node language.
10 votes

How do I get the current path alias or path?

To get full current url not just the path (Drupal 8), this is the cleanest solution I was able to find: Url::fromRoute('<current>',array(),array('absolute'=>'true'))->toString();
crobicha's user avatar
  • 371
10 votes
Accepted

How do you change the URL alias of a contact form?

I went under Configuration --> URL Aliases, and added a new alias for /contact-us that points to /contact/contact_us I expected the result to be that anyone typing in /contact-us would end up at /...
rockstardev's user avatar
  • 1,039
10 votes
Accepted

Tokens for taxonomy term parents in Pathauto

I just tested the tokens that I suggested in the comments with field_tags on articles and it works perfectly, no idea what your problem is, make sure you use the latest versions of pathauto and token. ...
Berdir's user avatar
  • 83.2k
9 votes

How do I get the current path alias or path?

In Twig template / Drupal 8 : URL of the current page {{ url('<current>') }} only PATH {{ path('<current>') }} See: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-...
elektrorl's user avatar
  • 232
9 votes

How to get term alias in twig templates

A bit late, but just figured this one out in case it helps someone else: {{ path('entity.taxonomy_term.canonical', {'taxonomy_term': tid}) }}
Nick's user avatar
  • 91
8 votes
Accepted

What is the replacement of drupal_match_path()?

New PathMatcher service reports that the path.matcher service was introduced to deprecate most procedural functions in the path.inc file with its methods. In particular, the equivalent of $...
apaderno's user avatar
  • 97.4k
8 votes
Accepted

How to check if path alias exists

This service was deprecated in Drupal 8 and removed in Drupal 9. Path aliases were converted to an entity. You can use the entity storage and additionally the path_alias.repository service for some ...
4uk4's user avatar
  • 99.6k
7 votes

Bulk update for unchecked "Generate automatic URL alias"

VBO doesn't have an option to change the status of 'Generate automatic URL alias' You could execute the following mysql query: UPDATE `pathauto_state` SET `pathauto`=1 WHERE `entity_type` = 'node' ...
Rick B's user avatar
  • 548
7 votes

Mapping or migrating path alias using using Migrate Source CSV

@Jigar's answer is close but not quite. Here is what I eventually figured out. It should be path/alias and not just path. Also, if you have pathauto module installed it will create the path based on ...
oli-chowdhury's user avatar
6 votes

Is it possible to programmatically add a path alias to a programmatically created node?

To get a clean string, you can use this function from the Pathauto module. $clean_string = \Drupal::service('pathauto.alias_cleaner')->cleanString('Your very dirty string, with many URL un-...
alperbalkaya's user avatar
6 votes

Views: URL Alias as a field

Just an update for those than land here for Drupal 8.5+ The Path field no longer exists in views, you now need the "Link To Content" field instead. If you need to output the path only, then make sure ...
tobynew's user avatar
  • 245
6 votes
Accepted

How to bulk update existing path alias after changing the URL alias pattern?

it works only to URL which does not have path alias – Vamsi Then first go to /admin/config/search/path/delete_bulk and delete the aliases in question. Then update /admin/config/search/path/...
No Sssweat's user avatar
  • 31.2k
6 votes
Accepted

Programatically redirect to node/nid doesn't redirect to url alias

Try using Url. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()]); new RedirectResponse($url->toString());
Bart's user avatar
  • 6,934
6 votes

Mapping or migrating path alias using using Migrate Source CSV

Just tried it out while trying to find an answer to a similar question. Here's how it should look: source: ... constants: slash: '/' process: ... // This parameter should be "path" and ...
Jigarius's user avatar
  • 3,109
6 votes

Use taxonomy path for a node's path alias

Using pathauto, it should be quiet easy with a pattern like : Normal way : news/[node:{NAME_OF_TAXONOMY_FIELD}:entity:name]/[node:title] With hierarchy : news/[node:{NAME_OF_TAXONOMY_FIELD}:entity:...
Oulalahakabu's user avatar
  • 1,313
6 votes
Accepted

How to get the raw value of the url in a link field

Instead of rendering the URL object with toString() use the method Url::getInternalPath: $url = $link->getUrl(); if ($url->isRouted()) { $path = $url->getInternalPath(); }
4uk4's user avatar
  • 99.6k
5 votes

URL-Alias for language neutral content

You can use the Pathauto i18n module: This module provides functionality to create aliases for each language. Also this module will be helpful, if you use field translation and you want show ...
Adrian Cid Almaguer's user avatar
5 votes

How do I get the current path alias or path?

If you want the absolute URL with the querystring too: use Drupal\Core\Url; // ... Url::fromRoute('<current>', [], ['query' => \Drupal::request()->query->all(), 'absolute' => 'true'...
David Thomas's user avatar
  • 21.9k

Only top scored, non community-wiki answers of a minimum length are eligible