3

I have a template file for my blog page that renders a single image when the blog posts are listed. Originally, the field was for a single image, but it has been changed to accept more than one image and render only the first image. I would like to do within a template file instead of a view.

In that template file I used the following code, which doesn't work anymore since the image field has been changed.

<div class="image"><?php print render($content['field_blog_images']); ?></div>

Instead of that, I tried the following code, which doesn't work too.

<div class="image"><?php print render($content['field_blog_images'][0]); ?></div>

How should I render the first image of a field that accepts multiple images?

7
  • If it's the multiple image field, first you need to get the array with the images $images = field_get_items('node', $node, 'field_blog_images'); and then render the image: $blog_image = field_view_value('node', $node, 'field_blog_images', $images[0]); print render($blog_image); Take a look at the example. Moreover, as @webkenny wrote in his answer, you should keep the logic away from the template files.
    – Jack-PL
    Jul 28, 2013 at 21:29
  • I tried to do it as @user18400 suggested but it throws an error: Notice: Undefined variable: node in designmdd_preprocess_node() (line 68 of /home/heft57/public_html/sites/all/themes/designmdd/template.php). EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7663 of /home/heft57/public_html/includes/common.inc).
    – user18400
    Jul 30, 2013 at 11:55
  • This is a drupal 7 blog page modified with a template by the way; so my goal is to show the first value of the blog entry's 'field_blog_images' in the blog entry list (/blog) page as well.
    – user18400
    Jul 30, 2013 at 12:02
  • define the node object in your preprocess function $node = $variables['node']; after that you'll be able to get the first image as I wrote in previous comment.
    – Jack-PL
    Jul 30, 2013 at 19:23
  • You are right sorry I missed that. But it still doesnt work.
    – user18400
    Jul 31, 2013 at 16:32

2 Answers 2

2

It's all about the structure of the image field. You're automatically going to the 0 index, but I bet if you were to do a dump of that variable, you'd find you were at the wrong position in the array. You'll need to loop through it or target the very specific element you're looking to render.

Check out this documentation comment for details and modify for your example. That comment shows the following code to loop through the images.

$node = node_load($nid);
$image = field_get_items('node', $node, 'field_NAME');
foreach ($image as $key => $value) {
  $output = field_view_value('node', $node, 'field_NAME', $image[$key], array(
    'type' => 'image',
    'settings' => array(
      'image_style' => 'thumbnail', //place your image style here
      'image_link' => 'content',
    ),
  ));
  print render($output);
}

That code is showing how to loop through the images in a different context, but the foreach() loop is the same that should be used in your code.

For that reason, it could be worth taking this up to template.php inside hook_preprocess_node() to avoid adding logic in a template file.

function mytheme_preprocess_node(&$variables) {
  if($variables['node']->type == 'blog' {
    // My crazy logic to assign my image variable.
    $variables['blog_image'] = render($somevalue);
  }
}

The template file then just needs a single code line.

print $blog_image;
0
0

If you insist on not to use Views for theming your blog teasers, you can use template preprocess_node function to create the hook suggestion and variables for the template file which you're going to use.

Example:

/**
 * Override or insert variables into the node template.
 */
function YOURTHEME_preprocess_node(&$variables) {

  $node = $variables['node'];

  // it allows to use templates for the type and view_mode e.g: node--blog--teaser.tpl.php
  $variables['theme_hook_suggestions'][] = 'node__' . $node->type . '__' . $variables['view_mode'];

  // blog teaser
  if ($node->type == 'blog' && $variables['view_mode'] == 'teaser') {
    $images = field_get_items('node', $node, 'field_blog_images');
    if ($images) {
      $blog_image = field_view_value('node', $node, 'field_blog_images', $images[0], 
        array(
          'type' => 'image',
          'settings' => array(
          'image_style' => 'thumbnail',
          'image_link' => 'content',
        ),
      ));
      $variables['blog_image'] = $blog_image; 
    }
  }
}

Now you can use node--blog--teaser.tpl.php file for theming your blog teasers and print the image:

<div class="image"><?php print render($blog_image); ?></div>
0

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.