Working with Drupal Fields: Getting Single Field Values For Display and Manipulaton

This post is a quick addition to my previous tutorial: Loading Only One Field From An Entity or Node in Drupal 7. The tricky part there was what to do with node's field data and how to properly display it? Well there are lots of approaches there, but I see three which are of “Drupal Way” kind:

1) Get raw field data via field_get_items(). This approach may be useful if you need field data for other than displaying the field puproses.
2) Display single (with multivalues) field via field_view_field(). Returns a renderable array for the value of a single field in an entity. The resulting output is a fully themed field with label and multiple values.
3) Display single (one value only) field via field_view_value(). Returns a renderable array for a single field value.

So let's imagine we have an entity object: $node.

Approach 1. We want to get node's field_story_image raw value, here's how to do that:

$image_field_raw = field_get_items('node', $node, 'field_story_image');

UPDATE: put this snippet on dropbucket for future reference: http://dropbucket.org/node/55

As a result we have an array of raw field values which aren't anyhow preprocessed and look like they'd come directly from database.

Approach 2. We want to get field ready for display and we're ok with displaying all field's values (In case the field is multivalued):

$image_field  = field_view_field('node', $node, 'field_story_image');

As a result, in $image_field we get a renderable array for all values of the field. Calling render($image_field) will nicelly render the image (or images if there are multiple) for us. By render I mean turning drupal arrays into consumable html.

The cool thing about this approach is that you can get field rendered via any of formatters available for the field, for example, we want to get body field trimmed:

$body_field  = field_view_field('node', $node, 'body', array('type' => 'text_trimmed'));

Now in $body_field you have a rendered array for the body field of your node. Note that we passed formatter name in array('type' => 'text_trimmed') array. But we can do even more. Knowing that 'text_trimmed' formatter has a setting called 'trim_length' we can set this setting on the fly. Let's trim our field on 150 characters limit:

$body_field  = field_view_field('node', $node, 'body', array('type' => 'text_trimmed', 'settings' => array('trim_length' => 150)));

UPDATE: put this snippet on dropbucket for future reference: http://dropbucket.org/node/56

Approach 3. We want to display a node field, which can be multivalued but we are only interested in first value. Let's output our field_story_image and make the task even a bit more complex: let's resize image and link it to its node!

At first we need to get all raw values of a field (see approach 1):

$image_field_raw = field_get_items('node', $node, 'field_story_image');

After we got all values for a field, we want to pass them to field_view_value() like this:

$single_image_field = field_view_value('node', $node, 'field_story_image', $image_field_raw[0],
  array(
  'type' => 'image',
  'settings' => array(
    'image_style' => 'medium',
    'image_link' => 'content',
  ),
);

UPDATE: put this snippet on dropbucket for future reference: http://dropbucket.org/node/57

In the end, we get a renderable array for a single value of 'field_story_image' which is resized according to 'medium' image style and is linked to a node, voila!

To get full idea of what can be done, I really recommend you reading api.drupal.org docs: field_view_value(), field_view_field(), field_get_items().

Comments

Submitted by Rakib on Sat, 2013-05-04 13:40

thanks man a million, :)

Add new comment

You are here