Search code examples
drupaldrupal-7drupal-theming

render fields individually drupal


I'm looking to render each field individually within page.tpl.php. My situation is, I have a image field, unlimited values, and a slideshow format. I want to take this field rendered with the slideshow formatting and place it where ever I want.

I've tried the following, but this doesn't apply the correct formatting, it just outputs with the default format:

$field_view_field_field_banners = field_view_field('node', $node, 'field_banners');
$field_view_field_field_banners['#formatter'] = 'slideshow'; // I added this because the formatter was 'image'
echo render($field_view_field_field_banners);

Any help on this is greatly appreciated!


Solution

  • The fourth parameter that field_view_field takes can be the name of a view mode or it can be an array of the display setting keys: label, type, settings, and weight.

    It looks as if you're trying to specify the field's format, but what's happening is that field_view_field is looking for a View Mode (like Default or Teaser) called "Slideshow."

    Instead of

    $foo = field_view_field('node', $node, 'field_banners', 'slideshow');
    

    Try:

    $foo = field_view_field('node', $node, 'field_banners', array('type' => 'slideshow'));
    

    The answer to your second question is based on the one above really. The difference is between a view mode and a field formatter. It's highly likely that when you navigate to

    /admin/structure/types/manage/foo/display 
    

    that you have two default view modes: Default and Teaser. If you've set the field_banners field to use slideshow formatter under the Default view mode, then there isn't any need to even use a fourth parameter since the "default" view mode will be used when none is specified.

    In that scenario, you could just do: $foo = field_view_field('node', $node, 'field_banners');

    and $foo would have the slideshow format when rendered.

    Check out the Drupal API doc for field_view_field.