2

I've added an image field in a content type, and set the 'Field Settings' -> 'number of values' to unlimited, which means users can upload unlimited images for this field.

then I build a view for this content type and expose the image filed to the view. what I 'd like is to expose only image in teaser view, and all images in the full view.

I use a template preprocess function

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') {
      // something might be wrong here
      $item = reset($vars['items']);      // not working
      $vars['items'] = array($item);      // not working
    }
  }
}

In debugging this function, this 2 lines

  $item = reset($vars['items']);      
  $vars['items'] = array($item); 

doesn't seem to work. any suggestion?

1

The Display Suite Module offers you to choose the number of items to show on each viewmode.

  • Thanks, I will try it. but I wonder how to possibly do it in preprocess() way – helxsz May 23 '12 at 10:58
  • in drupal 7 you can do this from admin/structure/types/manage/page/display/teaser for Page content type for example, not need other module – svetlio Aug 23 '12 at 15:50
0

I created a custom module with the following code. It works for me but you probably need to adjust the field names to your own.

function image_teaser_fix_entity_view($entity, $type, $view_mode, $langcode) 
{
  if ($view_mode == 'teaser' && isset($entity->content['product:field_image'])) {
    $entity->content['product:field_image']['#items'] = array_slice($entity->content['product:field_image']['#items'], 0, 1);
  }

if ($view_mode == 'teaser' && isset($entity->content['field_field_image'])) {
$entity->content['field_field_image']['#items'] = array_slice($entity->content['field_field_image']['#items'], 0, 1);
  }

}

Could it be that in your code you need to replace 'items' with '#items' ? Just a guess.

0

The http://drupal.org/project/field_multiple_limit module allows you to set how many images display, per view mode. So you can set up your teaser to only show the first one but your full node to show all of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.