40

Goal: To send an email with a list of URLs generated from nodes.

In my custom module I have managed to get the node id which the user wants and I now want to get the URL of each node to put into my email.

I searched the db and used google but I can't seem to find the right solution.

It seems we need to construct the URL something like this:

<?php
global $base_url;
$link=$base_url."// few more parameters 

5 Answers 5

91

You can use the url() function:

$options = array('absolute' => TRUE);
$nid = 1; // Node ID
$url = url('node/' . $nid, $options);

That will give you the absolute path (i.e. with http://example.com/ in front of it), with the URL aliased path to the node page.

0
15

You can also try drupal_lookup_path('alias',"node/".$node->nid).

4
  • 3
    Is there some problem with this method? This seem more useful, and less obvious, than the approved answer.
    – robomc
    Commented Sep 4, 2012 at 0:22
  • What if the node has no alias? also, this will return a relative path.
    – cambraca
    Commented Oct 10, 2014 at 14:29
  • Use in conjunction with $base_url global variable.
    – scotself
    Commented Jul 27, 2015 at 15:58
  • drupal_lookup_path() isn't always directly available, use url() instead. Commented Aug 19, 2019 at 19:07
7

Also you can get it by

$path=drupal_get_path_alias('node/'.$nid);

absolute path for nid

url('node/' . $node->id(), ["absolute" => TRUE]);
2
  • What if the node has no alias? also, this will return a relative path.
    – cambraca
    Commented Oct 10, 2014 at 14:30
  • @cambraca yes it is return raw path something like node/3
    – Yuseferi
    Commented May 17, 2015 at 5:32
4

You can also use the l() function.

  l(t('Link text'), 'node/123', array('options' => array('absolute' => TRUE)));
-5

use

$node_url;

it will give you the current node url

1
  • 2
    You are not getting the question! please read the question carefully before answering Commented Dec 13, 2013 at 8:20

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.