Skip to main content
87 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
  • 102k
41 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,803
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
  • 18k
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
  • 28k
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
  • 22.2k
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
13 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
  • 501
13 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: \Drupal\Core\Url::fromRoute('<current>', [], ['absolute' => 'true'])->toString()
crobicha's user avatar
  • 371
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.5k
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

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
  • 102k
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

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

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

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,127
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,343
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
  • 102k
5 votes

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

Using drupal 8, this is how I did (after a quick search in the pathauto.module file) : Step 1 : Create the node $node = Node::create([ 'type' => 'article', 'title' => 'Test article', ...
ZyDucksLover'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
5 votes

How do I export the list of URL alias into an excel or csv?

This is easy with a query: select source, alias from url_alias INTO OUTFILE '/tmp/url_aliases.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; If you are good with mysql, you ...
oknate's user avatar
  • 14.5k
5 votes
Accepted

get taxonomy term aliased path when its a referenced field in a $node entity

Sure enough I could use the tid or target_id in entity reference lingo and use it to find the URL fromRoute using as route_name entity.taxonomy_term.canonical $tid = $node->field_category[0]-&...
GiorgosK's user avatar
  • 8,523
5 votes

How do I remove the default node path when using a path alias?

Simple answer: You don't have to. We pretty often are asked about disabling the generic URLs to avoid penalties from search engines. This however is not necessary. Once using URL aliases, the meta ...
Mario Steinitz's user avatar
5 votes
Accepted

How to display the path without alias?

You have to set the option alias: $link = Link::createFromRoute( $this->t('my business'), 'entity.commerce_store.canonical', ['commerce_store' => $store_id], ['alias' => TRUE, '...
4uk4's user avatar
  • 102k
5 votes

Programmatically specifying pathauto alias

Pathauto 8.x is already hooking into the hook_entity_$CRUD_OP() hooks of D8 to do its work. Thus, trying to do this via a hook_entity_insert() means your module hook might fire before/after pathauto ...
Shawn Conn's user avatar
  • 26.9k
5 votes
Accepted

Pathauto pattern for a content type depending on a reference field?

A taxonomy field is referencing entities, it has no value property. See how to print value of a taxonomy field to headline with node.html.twig? Converting this to a token pattern: [node:...
4uk4's user avatar
  • 102k
5 votes
Accepted

How to delete duplicate path aliases?

If there are many duplicates, use the queue system to delete them gradually. Start by enqueuing identified duplicates in an update hook: // file: my_module.install /** * Enqueue duplicate path ...
mona lisa's user avatar
  • 6,344
5 votes
Accepted

How can I add cache information to InboundPathProcessor?

In a path processor you can't set data in the request object, the request object is not preserved. The only exception are query parameters which are extracted from the request object when the ...
4uk4's user avatar
  • 102k

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