4

I'm using a view template to override how a field is displayed.

I want to include a link to the node in the field.

Given a node, how do I get the URL (relative is fine) for a node?

I've got the node in the overridden template, but not sure where to go from there:

$node = node_load($row->{$field->field_alias});
$wrapper = entity_metadata_wrapper('node', $node);

$path_to_node = ???

?><a href="<?php print $path_to_node ?>">Go to Node</a>

6 Answers 6

2

You don't need entity_metadata_wrapper in this case. You can get path of node using url() function.

$node = node_load($row->{$field->field_alias}); 
$nid = $node->nid; 
$path_to_node = url("node/$nid");

And I agree with Елин Й's answer. You can use l() function. It already uses url() function. So you can build link with node pathauto alias using:

$node = node_load($row->{$field->field_alias}); 
print l(t('Go to Node'), 'node/' . $node->nid);
1

In your view, you need to output the Node: Path field. Then you can access the information in the view using:

$fields["path"]->content;
0

A views template should have the $view variable available, so you could use:

$nid = $view->result[$id]->nid;
$path = "node/".$nid;
0

URL to a node is node/$node->nid. I would strongly recommend you to use l() function to generate links, so that it will always print the right URL.

0

In your code example it would be:

$node = node_load($row->{$field->field_alias});
$wrapper = entity_metadata_wrapper('node', $node);
print l(t('Go to Node'), 'node/' . $node->nid);
0

For a proper seo friendly url use the lookup path function:

$ path = drupal_lookup_path('alias',"node/".$node->nid)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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