3

I have a content type with an image field that can contain an unlimited number of images. The field is used to display an image gallery in the full node view, but I also want to use the first image of the field in the teaser.

I vaguely remember doing something like this years ago in Drupal 6, but now I can't find any option to do this in Drupal 8. I'm not sure if I'm misremembering, or just looking in the wrong place.

How can I display only the first image of an image field in the teaser?

4

Create a custom image formatter by copying the default ImageFormatter.php from the image module into a new custom module, rename everything properly and then add only one simple break; at the last line of the foreach in viewElements(). Choose that formatter under display settings for your teaser view mode of your content type. Works in views, too.

Done that for you in a quick custom module: image_first_formatter.

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.