13

In node.tpl.php I am trying to get the raw value but the problem is that when I use render:

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

it does not just return field_image value but formats and outputs the whole image.

I need a way to get only the field's raw value. How can I achieve this?

3 Answers 3

11

To get the real value and not a themed one...

You can use http://api.drupal.org/api/drupal/modules!field!field.module/function/field_get_items/7

 $my_field_items = field_get_items('node', $node, 'field_my_field');

Or use Entity API module and entity_metadata_wrapper

 $node_wrapper = entity_metadata_wrapper('node', $node);
 $field_val = $node_wrapper->field_test->value();

Take a loot also at http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

1
  • I'd like to point out that the metadata wrapper technique is particularly useful when the field's value is not directly stored in the field itself but is somehow derived or provided by the field's module. This is rare, but occasionally you will stumble on such a field. For example the mailchimp_lists module provides a Mailchimp Subscription field with that behaviour. The only way to find out if the user is a subscriber or not is to access the field that way. Sep 6, 2020 at 14:54
5

In a node template, for raw values you can access the $node object.

<?php print $node->field_price; ?>

If you need to see the structure, can do <?php print_r($node); ?>.

Also, if this is for a specific content type you should use a variation of node.tpl.php instead of node.tpl.php itself or else it will try to display fields that are non-existent on other content types.

3

enable Devel module and in node-{content-type}.tpl.php use

<?php print dsm($node); ?>

on top of code.

you can see all $node fields in readable format

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.