83 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
  • 96.9k
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.7k
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 ...
Matt's user avatar
  • 5,592
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

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.5k
22 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.2k
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
  • 21.8k
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->...
Rahul Baisane's user avatar
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-...
Adrian Cid Almaguer's user avatar
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 ...
kenorb's user avatar
  • 14.1k
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?
Gaius's user avatar
  • 471
11 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
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
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
  • 82.7k
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
  • 96.8k
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
  • 538
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 (...
Clive's user avatar
  • 166k
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
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 ...
4uk4's user avatar
  • 96.9k
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

How to programmatically set node alias on node save

Path alias could be set using the path module in the core. Just call path_save the following from a function (or hook) of your choice. In your case I'd recommend you to implement the following code in ...
AjitS's user avatar
  • 10.9k
6 votes

How to programmatically set node alias on node save

None of the hook_nodeapi implementations will work, that hook was removed in Drupal 7. Your other attempts are failing because you're trying to implement the hooks in a theme - themes can implement ...
Clive's user avatar
  • 166k
6 votes
Accepted

Saving existing node doesn't update automatic alias

Have you checked your settings at "admin/config/search/path/settings"? There you can config what to do with your alias, when an entity is updated.
Huelfe's user avatar
  • 183
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

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