155 votes
Accepted

How do I get the current path alias or path?

Some of the other answers were only correct during previous alpha/beta versions of Drupal 8, or seemed to be incomplete. As of beta7 (and hopefully permanently), the following rules should apply: For ...
  • 2,229
77 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('...
  • 90.3k
34 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 = \...
  • 26.4k
34 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 ...
  • 5,180
29 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/...
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();
  • 26.9k
19 votes

How do I get the current path alias or path?

For getting the current path in Drupal 7, you use to use current_path(), but in Drupal 8 you use $url = Url::fromRoute('<current>');. Source: https://www.drupal.org/node/2382211
18 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\...
  • 15.3k
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': ...
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; /** * ...
14 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'...
  • 21.1k
13 votes

How do I get the current path alias or path?

To get current path (aliased path): // Returns something like /about $url = \Drupal\Core\Url::fromRoute('<current>'); To get current path (system path): // Returns node/1 $path = $url->...
13 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-...
12 votes

Create path alias programatically when node is created

Instead of using hook, Pathauto module should be used where you can define specific pattern how aliases are generated per content type (at /admin/config/search/path/patterns). In there you can use ...
  • 13.7k
11 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?
  • 471
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();
  • 371
10 votes
Accepted

Getting URL alias of a term

Just use the url() function. It will return aliased path if you have pathauto installed. Example: $link = url('taxonomy/term/1');
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 /...
  • 1,011
10 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.
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-...
  • 233
9 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. ...
  • 81.7k
8 votes

Specific URL for each kind of content

Use Path Auto and Token Module to achieve it. It also supports multilingual functionality!! The Pathauto module automatically generates URL/path aliases for various kinds of content (nodes, ...
  • 3,392
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 $...
  • 95k
8 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}) }}
  • 81
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' ...
  • 508
7 votes
Accepted

How to remove url alias when node is deleted

You can use hook_node_delete: function hook_node_delete($node){ // Delete alias. $path = path_load( array('source' => 'node/'.$node->nid) ); path_delete($path['pid']); }
  • 1,956
7 votes
Accepted

Is there a better way to create an alternative URL Alias Pattern?

I'd create a token for that specific data; I'm not sure it's necessarily better (or worse) than using the alter hook, but it has the advantage of being reusable elsewhere in the site. A simple (...
  • 164k
7 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 ...
  • 90.3k
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 ...
  • 245
6 votes

Domain Access with URL Alias - same content, but with different automated aliases

I believe this is exactly what the Domain Path module is for. According to the module page: The Domain Path module allows the creation of separate path aliases per domain for nodes created using ...

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