I created a custom tpl file for the teaser of a node. To print the fields i use render() eg:


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,

<a href="<?php print $node_url; ?>">
   <?php print render($content['field_product_subtitle']); ?>

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?

3 Answers 3


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(&$vars) {

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

  $path = 'node/'.$node->nid;

  if($vars['element']['#field_name'] == 'TARGET_FIELD_NAME'){
    foreach($vars['items'] as $key=>$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:


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

  • I ended up doing something similar but in the tpl file itself instead of using the preprocess_field. $content['field_product_subtitle'][0]['#markup'] = l($content['field_product_subtitle'][0]['#markup'], 'node/' . $node->nid); Could be better with foreach to go over every item but in this case it is just a simple textfield with one value.
    – dazz
    Feb 10, 2012 at 9:49
  • I would suggest keeping in mind the ideas of best practices and future-proofing. You may not always be the person maintaining this site, and even if you are, taking the "long way," so to speak, will result in a more easily maintainable theme and site. Using the foreach method will assure that is less likely to break because of changes to the field's settings and keeping this code in a preprocess function in template.php will keep heavy logic out of your .tpl.php files and make them much easier to read and work with.
    – sheena_d
    Feb 10, 2012 at 18:35

As a follow up to @Lance's answer, you have access to the $node object in your template file so it would be as easy as:

$title = $node->field_product_subtitle[$node->language][0]['value'];
print l($title, "node/$node->nid");

That will print the whole <a> tag with the correct URL alias for the node, with your custom field value inside.


Use the l() function. Example:

print l(t($content['field_product_subtitle']), 'node/'. $node->nid));
  • $content['field_product_subtitle'] would be an array and cannot be used as the 'text' value passed to l(). You could use l(render($content['field_product_subtitle']), 'node/'.$node->nid); but this would result in the same problematic output as OP has already succeeded in creating.
    – sheena_d
    Feb 9, 2012 at 17:02
  • You're right ... bad example :)
    – Lance
    Feb 9, 2012 at 20:19
  • That's pretty much how it would have been done in Drupal 6, though :)
    – sheena_d
    Feb 9, 2012 at 22:41

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.