Skip to main content

Drupal 8: Limit an image field display in teaser mode to one image

  1. /**
  2.  * Implements template_process_field().
  3.  */
  4. function THEME_preprocess_field(&$variables) {
  5.   $element = $variables['element'];
  6.   // Field type image
  7.   if ($variables['field_name'] == 'FIELD_IMAGE' && $element['#view_mode'] == 'teaser') {
  8.     // Reduce number of images in teaser view mode to single image
  9.     $item = reset($variables['items']);
  10.     $variables['items'] = array($item);
  11.   }
  12. }


Comments