2

I have come up with a need to show a node term's URL alias using plain php in drupal 8 but I can't figure out how to do it. In my custom module, I want to get /fruits/apples in a variable upon loading a node which is linked with Fruits term.

Here you can see the Edit Term settings page in which I have set the URL alias of the term Fruits to /fruits/apples. I want to get this URL alias whenever a user loads a page linked to this term.

term url alias

4
  • 1
    I'm not sure I understand what you are trying to do but have a look at $path = \Drupal::service('path.alias_storage')->load([ 'source' => '/' . $node->urlInfo()->getInternalPath(), 'langcode' => $langcode ]);
    – user21641
    Nov 15, 2017 at 23:24
  • @IvanJaros I want to get the URL alias of the term. The picture shows the Edit Term Page in which you can see the URL alias assigned to the term Fruits.
    – Shuji
    Nov 15, 2017 at 23:38
  • @IvanJaros I have updated the question with some more details.
    – Shuji
    Nov 15, 2017 at 23:46
  • 1
    But why is the path necessary on a node form? When the terms display on a node page, they’ll be linked.
    – Kevin
    Nov 16, 2017 at 5:25

2 Answers 2

10

You can get it by this way as well (assuming you know $id in advance in code) as of Drupal 8.5.x: 1. For taxonomy term:

Term::load($id); //not important
$aliasManager = \Drupal::service('path.alias_manager');
$alias = $aliasManager->getAliasByPath('/taxonomy/term/'.$id);

2. For node:

$node = \Drupal::entityManager()->getStorage('node')->load($id); //not important
$aliasManager = \Drupal::service('path.alias_manager');
$alias = $aliasManager->getAliasByPath('/node/'.$id);

OUTPUT: $alias = /fruits/apples

EDIT: As of Drupal 8.8.x, path.alias_manager has been deprecated, use path_alias.manager in future versions of Drupal.

3
  • Thank you for the answer. One thing that helps is to use code formatting on code blocks. You can do this manually by indenting the code or highlight all the code and click the code button (looks like curly braces: { }).
    – mradcliffe
    Jan 14, 2019 at 13:46
  • 2
    This has been deprecated, per drupal.org/node/3092086.
    – John
    Jul 20, 2020 at 21:39
  • Service name now is 'path_alias.manager', not 'path.alias_manager'.
    – MilanG
    May 20, 2022 at 12:21
3

More right, easy and universal way:

$node = \Drupal::entityTypeManager()->getStorage('node')->load($id);
$node_alias = $node->get('path')->alias;

You can also do extra checks if you are not sure if an alias exists:

if (!$node->get('path')->isEmpty()) {
    $node_alias = $node->get('path')->alias;
}

UPDATED

This will work for any entity type. But here is example for taxonomy terms:

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
if (!$term->get('path')->isEmpty()) {
    $term_alias = $term->get('path')->alias;
}
2
  • The questions asks for a taxonomy term path alias. Your answer is for a node path alias.
    – John
    Jul 20, 2020 at 21:36
  • @John Thanks for your comment. I have updated the answer regarding this.
    – Para-Diz
    Aug 7, 2020 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.