Hi all,

I am trying to get the url for a node as I need to put it into a link. I need to do this from code too. I know that the templates have access to the node_url variable but I am not sure how to access this from within nodeapi?

I assume I can get access to the variable from the $page variable that is passed through to nodeapi but don't know how to acces it eg is it $page["node_url"], $page["var"]["node_url"] or some other way?

Maybe there is a function like get_url_for_node($node)?

Any help appreciated.

thanks

Comments

jfxberns’s picture

What information about the node are you starting out with? If you have the Node ID, the simple answer is the path is /node/Node ID

For example, if the node is number 1234 the URL is:

http://www.yourdomain.com/node/1234

Was that all you were looking for?

John Berns
Travel Guide
Travel Photographer

robloach’s picture

I'd use url:

$nodeurl = url('node/'. $node->nid);
mxc4’s picture

Thanks for the replys :) The url function is what I am looking for. Is there a single place where the various functions that are unique to drupal are documented? Searching for "url function reference" just results in a lot of unhelpful posts that have nothing to do with the problem I am trying to solve. (i.e find info on the url function)

http://www.jumpingbean.co.za/php/development
http://www.cyberdesigns.co.za

dman’s picture

Um, yes. You want api.drupal.org
It's the first link when you click the "Documentation" tab here at Drupal.org.

url()

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

kudokatz’s picture

What I have seen in code from contributed modules is the use of the arg() function that breaks down a drupal URL by delimiting "/"s-- I think that is another solution.

mikeschinkel’s picture

@kudokatz: I'd be interested in seeing an example of that posted, if not too much trouble.

jasonch’s picture

For example, at some page in your site the full url is
http://your_site_address/?q=node/3/edit/2 (i made it up)

then the arg() function gives you everything following the ?q= such that
arg(0) = 'node'
arg(1) = 3
arg(2) = 'edit'
arg(3) = 2

hope that helps!

I actually happens to come across this issue when I'm searching for a function that just gives me the current url all together (as a string like "node/3/edit/2").
Just thought it'd be useful. Does anyone know about it?

toddtomlinson’s picture

Here's the easiest way I found to get the URL -- especially if you are using clean URLs and need the actual URL displayed (e.g., /jobpostings and not /node/138)

$displayedURL = $_SERVER['REQUEST_URI'];

-------------
Todd Tomlinson
Managing Partner
Aha Consulting

-------------
Todd Tomlinson
Author and Drupalista

Jeremy Visser’s picture

Can I just say that if you're planning on echoing that variable straight back into the HTML (e.g. for a "permalink" link or something), then that is a security vulnerability.

Why? Because somebody can put HTML into the URL (e.g. if it were being echoed into a href attribute, you could "break out" of it by inserting a quote, like so: http://example.com/?q=node/123"<script>alert(document.cookie);</script>), and do evil things that way.

REQUEST_URI should not be trusted, and failure to understand this is what leads to vulnerabilities such as these.

If you absolutely must echo the contents of it, make sure it is properly sanitised, including being formatted for use inside attributes if that is the case.

If in doubt, it's probably vulnerable.

druplicate’s picture

How about this - I think it's sanitized, yes? Unfortunately it produces a parsing error. I posted this on the API site (http://api.drupal.org/api/function/drupal_get_destination/6#comment-5889) for the function "drupal_get_destination", slightly modified to return the path alias.

Parse error: syntax error, unexpected T_IF, expecting '{' in /usr/local/www/apache22/data/includes/common.inc(1695) : eval()'d code on line 3

I pasted this code into a Views Custom Field, to be displayed as a link button in a block:

<?php
function drupal_get_destination_alias() {
  if (isset($_REQUEST['destination'])) {
    return 'destination='. urlencode(drupal_get_path_alias($_REQUEST['destination']));
  }
  else {
    // Use $_GET here to retrieve the original path in source form.
    $path = isset($_GET['q']) ? $_GET['q'] : '';
    $query = drupal_query_string_encode($_GET, array('q'));
    if ($query != '') {
      $path .= '?'. $query;
    }
    return 'destination='. urlencode(drupal_get_path_alias($path));
  }
}

$path1 = drupal_get_destination_alias();
print ("<a href=\"" . $path1 . "/buildings\" title=\"View\">View</a>");
?>

The idea is to take the path from the page the Views block display is on, like "content/chicago/southside" and when the button is clicked, take the user to a page with a view with a path like "content/chicago/southside/buildings". "Chicago" and "southside" are passed as arguments to the view.

Shouldn't be that hard to do this, but I can't get anything I try to work.

gupta.sandeepcs’s picture

u can use $node->path;

yorgo’s picture

but if you don't have the full node available to your disposal and you don't want to load the node using node_load, then you can try:

drupal_get_path_alias("node/[NID]");

- where [NID] is your node's ID

dotpex’s picture

url('node/'.$node->nid);
jaypan’s picture

or even

url(drupal_get_path_alias('node/' . $node->nid), array('absolute' => TRUE));

(this will give a fully qualified URL to the node in question, with the node alias if one exists).

Contact me to contract me for D7 -> D10/11 migrations.

ajohnpraveen’s picture

Probably u can use,

$node_path = node_uri($node);
drupal_goto($node_path['path']);

This is what happens within the node_uri() function

  return array(
    'path' => 'node/' . $node->nid,
  );
jaypan’s picture

Not in Drupal 5 it doesn't (this thread is about D5).

Contact me to contract me for D7 -> D10/11 migrations.