20

I have a node object, and I am trying to use it to get a given path in Drupal 8.

There was drupal_lookup_path() in Drupal 7 but I'm not seeing any equivalent or documentation for Drupal 8.

What's the new "Drupal" way to get a path from a node object?

Would it be something simple like $node->get('path')?

This seems like a simple question, but I'm just absolutely not finding any documentation on it.

Notice I do not want the path alias for the current path; I want the path alias for an arbitrary node, given a node object or a node ID.

  • 3
    Possible duplicate of Get the current path alias or path? – Kevin Mar 8 '17 at 17:06
  • I think this might be a duplicate, check the linked question there are lots of answers. – Kevin Mar 8 '17 at 17:07
  • The other question has answers saying how to get the path alias for the current path, not for a node for which the OP knows the node ID. – kiamlaluno Mar 8 '17 at 17:59
  • It's not a duplicate - I don't want the current path. I want the path given an arbitrary node object or NID. – Jack Ryan Mar 8 '17 at 18:03
  • Yes, this is not a duplicate, see my answer. – 4k4 Mar 8 '17 at 19:10
40

To get the path for a node id use AliasManager::getAliasByPath:

$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$nid);
  • 3
    Yep, this looks right and is simpler than my solution. Thank you! – Jack Ryan Mar 8 '17 at 19:14
16

What about using Url:

use Drupal\Core\Url;    

...

$url = Url::fromRoute('entity.node.canonical', ['node' => $nid])->toString();
  • 5
    IIRC, the primary difference between this and using the AliasManager, is that this will also ensure that outbound path processors run, while AliasManager just does the alias lookup. You can also get the Url object directly as $node->toUrl(). – mpdonadio Mar 9 '17 at 0:57
  • 1
    Much better solution. – Patrick Oct 21 '17 at 23:35
12

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 or two. Nodes actually have built in methods for getting urls.

$node->toUrl()->toString();

Would return the aliased url of the node.

toUrl() returns a url object. See here https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.2.x

and toString() is a method of the url object that returns the string representation of the url.

  • 6
    Note that $node->toUrl()->setAbsolute()->toString() will return the absolute URL, useful for sharing etc. – Tim Nov 18 '17 at 14:57
2

You can also use the load method from path.alias_storage to return an array of the alias details or boolean false if there wasn't a path found for that node.

$aliasArray = \Drupal::service('path.alias_storage')->load(['source' => '/node/' . $nid]);

2

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.

-4

I was hoping for a more "Drupalesque" solution, but here's what I made myself to do the job:

  function getPathFromNID($nid) {
    $db = \Drupal::database();

    $data = $db
        ->select('url_alias', 'u')
        ->fields('u', ['alias'])
        ->condition('source', '/node/' . $nid)
        ->execute()
        ->fetchField();
    return $data;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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