欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

根据node得到路径

shiping1 的头像
见 https://drupal.org/node/164585 有大用
变量 $node_url 只有在node.tpl.php中起作用


<?php
$nodeurl
= url('node/'. $node->nid);
?>

I can use $node->path;
drupal_get_path_alias("node/[NID]");

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

<?php
$node_path
= node_uri($node);//这个好像是不行的
drupal_goto($node_path['path']);
?>

 



来自 https://drupal.org/node/164585
How to get a url for a node?

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

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

I'd use url:

<?php
$nodeurl
= url('node/'. $node->nid);
?>

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

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/ |

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.

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

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?

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

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.

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.

u can use $node->path;

Sandeep Gupta

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

<?php
url
('node/'.$node->nid);
?>

or even

<?php
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).

Jaypan
Our newest Drupal site: Ramenities.com

Probably u can use,

<?php
$node_path
= node_uri($node);
drupal_goto($node_path['path']);
?>

This is what happens within the node_uri() function

<?php
 
return array(
   
'path' => 'node/' . $node->nid,
  );

?>

Cheers,
AJP

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

普通分类: