Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display ONE image under teaser mode, while multi images under full content?

I'm building a site with Drupal 7.2, and using several useful modules (Views, Display Suite, and 20+ more).
I've added an image field in a content type (article), and set the 'Field Settings' -> 'number of values' to 5, which means users can upload 5 images for this field.
Under 'Full Content' view mode, I'd like to display all the images, but How DO I display only ONE image under 'Teaser' mode? Is there any modules can do this?

like image 996
MessyCS Avatar asked Jun 15 '11 08:06

MessyCS


3 Answers

I solved this problem by adding a new template for this field type like field--field_image.tpl.php in my case with the following code:

// Reduce image array to single image in teaser view mode
if ($element['#view_mode'] == 'teaser') {
  $items = array(reset($items));
}

print render($items);

Hope this helps.

Edit: Here's the (probably) correct way to do it:

function MYTHEME_process_field(&$vars) {
  $element = $vars['element'];

  // Field type image
  if ($element['#field_type'] == 'image') {

    // Reduce number of images in teaser view mode to single image
    if ($element['#view_mode'] == 'teaser') {
      $item = reset($vars['items']);
      $vars['items'] = array($item);
    }

  }

}
like image 57
bjo3rnf Avatar answered Oct 21 '22 04:10

bjo3rnf


It did not work for me, i had to adjust the code sligthly:

function MYTHEME_process_field(&$vars) {
  $element = $vars['element'];

  // Reduce number of images in teaser view mode to single image
  if ($element['#view_mode'] == 'node_teaser' && $element['#field_type'] == 'image') {
    $vars['items'] = array($vars['items'][0]);
  }
}
like image 2
dotpower Avatar answered Oct 21 '22 03:10

dotpower


One option without having to change code is to use the module Field Multiple Limit. With this module you can choose how many items to show in which view for fields that allow for multiple entries.

Then on the teaser view of the field you can choose how many items to show or to skip.

Just saw this is already answered in Drupal Stack Exchange

like image 2
Daniel Avatar answered Oct 21 '22 04:10

Daniel