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('...
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 = \...
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 ...
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/...
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();
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\...
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;
/**
* ...
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'...
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 ...
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?
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.
Community wiki
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();
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 /...
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-...
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.
...
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}) }}
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 $...
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'
...
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 (...
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 ...
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 ...
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-...
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 ...
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 ...
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 ...
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.
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/...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
path-aliases × 8427 × 244
8 × 148
views × 125
taxonomy-terms × 90
nodes × 77
uri × 75
tokens × 55
i18n-l10n × 54
redirection × 43
6 × 38
routes × 36
entities × 26
users × 19
hooks × 18
theming × 17
navigation × 17
9 × 12
database × 11
forms × 9
commerce × 9
files × 9
migrations × 9
organic-groups × 8
importing-exporting × 8