4

In Drupal 7, you can render a field in a tpl.php file like this:

But that prints it out with it's label and HTML. Is there a way to get just the raw value of the field.

I know you can do the following:

  • Override the field markup in the field.tpl.php file (or field--FIELDNAME.tpl.php)
  • Turn off labels in the admin interface
  • Use a module like Fences to control mark up in the admin interface.

However, all of those involve extra steps. I was wondering if there is a way to do it directly in the node.tpl.php file. That way, all the changes are contained in a single place. (Plus if you change themes to one that requires the labels, you have to go back in the admin interface and turn them back on).

5 Answers 5

3

If you just want the value of a given field, it will be in the $node object in your node.tpl.php file (or node--type.tpl.php).

Normally it's under something like:

$node->field_name['und'][0]['value']
5
  • Is 'und' good practise to use? I think I read somewhere that is bad, but I can't find the source.
    – big_smile
    Commented Feb 5, 2015 at 17:34
  • @big_smile, I've seen it used quite a bit, it's a language indicator, there was a discussion of it at drupal.stackexchange.com/questions/25839/…
    – Jance
    Commented Feb 5, 2015 at 18:28
  • The LANGUAGE_NONE constant is preferred; the value isn't likely to be changed from 'und', but magic vars should be avoided wherever possible. You should also check that the field value has been sanitised before output
    – Clive
    Commented Feb 5, 2015 at 18:39
  • 1
    It's widely used, but not good practice. The best way is to use the field api: $node = node_load($nid); $field = field_get_items('node', $node, 'field_name'); $output = field_view_value('node', $node, 'field_name', $field[$delta]); (Source: computerminds.co.uk/articles/… ) Or use the entity api module and use their wrappers.
    – bloke_zero
    Commented Feb 6, 2015 at 9:49
  • Or you could use $node->field_name[$node->language][0]['value'] Commented May 7, 2015 at 14:00
2

You're on the right track!

Rather than a node template file though, the individual markup of a field is controlled by its field.tpl.php file. You can control the display of a field in a very granular way by making a custom one - copy your theme's default implementation of it, name the copy field--field-name.tpl.php, and remove all the excess markup.

2
  • I think this is the right answer in this case as its theme specific and doesn't involve too many function calls in the template file which always looks hacky to me.
    – bloke_zero
    Commented Feb 6, 2015 at 9:54
  • 2
    The way I always do it is this: tpl files for presentation/markup, functions in the template for logic (or for cases where you need to pass in a variable that wouldn't be available in the template file).
    – nikobelia
    Commented Feb 6, 2015 at 10:22
2

You might want to try

<?php print render($content['field_your_field']); ?>

in node.tpl.php combined with context and entity view modes modules or simply modify the display settings of the field in the content type configuration

1

If you are in the node template you can load the field, then get the content from the loaded field. Check out the field_get_items documentation for more info. In short it returns the values you want in the current language.

$field_name = 'your_field_here';
$field= field_get_items('node', $node, $field_name);
print $field[0]['value']

should do it. It will return an element for each item if it is a multi value field. They can be accessed via $field[$key]['value']

1

wolffer-east's answer satisfies the original question goals of:

  • Printing a field
  • Doing it in a single file (so everything is in one place).

However, the code is quite complex for a node.tpl.php file which are only supposed to contain simple PHP statements.

So in the end, I reworked it as follows. In my template PHP file, I put the following:

function print_field($field_name) {

if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);

  // Load the node if you need to
  $node = node_load($nid);
}

$field= field_get_items('node', $node, $field_name);
print $field[0]['value'];
} 

This is the same as wolffer-east, except it loads the node.

In my node file, I can then use the following:

<?php print_field('field_body_text') ?>

This renders out the field without any labels and HTML, but still clean.


A more advance version is this:

In template.php function print_field($field_name, $format = 'safevalue') {

if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);

  // Load the node if you need to
  $node = node_load($nid);
}

$field= field_get_items('node', $node, $field_name);

if ($format = 'url') {
    print file_create_url($field[0]['uri']);
} else {

print $field[0][$format];
}
}

In node.tpl.php

You can swap 'filename' with any of the following:

  • value: Prints field without any extra HTML for paragraphs and line breaks
  • safe_value: Prints field HTML for paragraphs and line breaks
  • filename: Prints field's filename uri: Prints field's path in public:// format[*]
  • url: Prints field's path in traditional URL format[*]
  • alt: Prints field's alt value[*]
  • title: Prints field's title value[*]
  • width: Prints field's width value[*]
  • height: Prints field's height value[*]

[*] only applies to fields that have files/images

The only downside is that it displays the following error message:

Notice: Undefined index: safevalue in print_field() (line 16 of template.php)

I am not sure how to fix this error message, but I guess it is a separate question.

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.