How to create a link on a field in a custom template file I created a custom tpl file for the teaser of a node.
To print the fields i use render() eg:

render($content['field_subtitle']);


This all works fine but i would like for example that the rendered output of field_subtitle has a link to the actual node.

My workaround for the moment is to do something like this,

&lt;a href="&lt;?php print $node_url; ?&gt;"&gt;
   &lt;?php print render($content['field_product_subtitle']); ?&gt;
&lt;/a&gt;


This works but produces some nasty looking html with div's inside the .
So i was wondering if you could just add a parameter to the render function that produces a link to the node?
You will want to use a preprocess function in your theme's template.php file, so that you can manipulate the element's attributes before it is rendered.

For example, if you wanted to link a particular field to the node's page, you could implement template_preprocess_field() as such:

function THEMENAME_preprocess_field(&amp;$vars) {

  $items = $vars['items'];
  $node = $vars['element']['#object'];

  $path = 'node/'.$node-&gt;nid;

  if($vars['element']['#field_name'] == 'TARGET_FIELD_NAME'){
    foreach($vars['items'] as $key=&gt;$item){
      $vars['items'][$key]['#markup'] = l($item['#markup'], $path);
    }
  }
}


the l() function is a structured way to build a link within the Drupal API. In this example I have assumed that the field is only allowing plain text, but if your field allows HTML, you can pass extra variables to the l() function that will allow that.

You can access other node data in the $node variable I created, such as the node type, published status, etc. If you want to investigate all the variables available to you in this (or any) preprocess function or template, I would suggest installing the Devel module and printing out the variable you are interested in with the kpr() function. For example, you could add the following code to the preprocess function I created above:

kpr($vars);


and you would be able to see all of the data available for use in that function in an easy-to-read format.