I think the question is self explanatory. I got a node ID and I need to create its absolute URL somehow. I can do this but (I think) this won't work every where in the site:

<a href="?q=node/node-id">Node Title</a>

Please correct me if I am wrong.

Is there a function that I can give the node id and returns me back the absolute path of it?

7 Answers 7


Yes, the url() function:

$url = url("node/$nid");
  • It partially worked. I have written exactly what you wrote, but in my address bar it doesn't show the node id. I get this ?q=node/
    – Bora Semiz
    Dec 24, 2015 at 10:38
  • Ohh sorry! My bad. I mis calculated something. I fixed this. Thanks a lot :)
    – Bora Semiz
    Dec 24, 2015 at 10:39

Drupal 8

$nid = 5;
// Or $nid = $node->id();
$node_title = 'Node Title'
// Or $node->getTitle();

Create absolute URL:

// Will output http://example.com/path-to-my-node
$options = ['absolute' => TRUE];
$url_object = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
// In Twig use {{ url_object }}.
// In PHP (outside a render array) use $url_object->toString().

Create absolute link object (and inner span):

// Will output <a href="http://example.com/path-to-my-node" class="this-class"><span>Node Title</span></a>
$options = ['absolute' => TRUE, 'attributes' => ['class' => 'this-class']];
$node_title = \Drupal\Core\Render\Markup::create('<span>' . $node_title . '</span>');
$link_object = \Drupal\Core\Link::createFromRoute($node_title, 'entity.node.canonical', ['node' => $nid], $options);
// In Twig use {{ link_object }}.
// In PHP (outside a render array) use $link_object->toString().

In Drupal 8 you can use toLink function if you already load the node to generate an absolute link.

$event = \Drupal::entityTypeManager()->getStorage('node')->load($event_id);
if (!$event)
  return false;

// add ->toString() or other if needed
$abs_link = $event->toLink(NULL, 'canonical', ['absolute' => true]);

For example.

This solution use same principle that previous answers but its seems to me more practical if you need other info from this node or if the node is already loaded.


Yes, the l() function:

$link = l("Node Title", $nid);
print $link;

Above code, prints the following output:

<a href="/node/XXX">Node Title</a>

If you want to get absolute URL as you stated in your question:

$link = l('Node Title', $nid, array('absolute' => TRUE));

which will generate the link like:

<a href="http://example.com/node/XXX">Node Title</a>

  • Good answer BUT to be honest it doesn't really answers question as asked. Would be good if you could add some info why op actually wants to use something else than he wrote.
    – Mołot
    Dec 24, 2015 at 12:47

Drupal 7

You can get path alias using drupal_get_path_alias()

it will return:

An aliased path if one was found, or the original path if no alias was found.

This function will give you pretty path if it is created by admin or by pathauto module. After that you can use path alias to build absolute url.

There are many ways to do that, you can use url() or l() or you can even use dirtier approach and concatenate global $base_url to path alias which will give you just absolute url as a string and print it in whatever template you want.

using drupal_get_path_alias:

$path_alias = drupal_get_path_alias("node/{node_id}", $language_code);

Drupal 8

There are not above functions in Drupal 8 but you can do same using path.alias service.


$path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($system_path, $langcode);


Please check this link for more information on both versions

  • 3
    In d8, this will not generate an absolute url. Aug 9, 2016 at 10:20

Drupal 9

$entity->toUrl('canonical', ['absolute' => TRUE])->toString();
  • 1
    The OP only has the nid, so it would be good to include the step for loading of the entity to this answer.
    – berliner
    Apr 14 at 13:56

Drupal 9

Best practice is the AliasManager service: path_alias.manager

$alias = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $nid);

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.