4

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?

0
5

You can do this on your own easily by extending core's ImageFormatter class from a custom module. Simply copy core's ImageFormatter.php into a custom module. Rename everything properly, remove all methods except viewElements() and then add only one simple break; at the last line of the foreach in viewElements(). After enabling the module (or after flushing cache if you added the formatter to an existing custom module) you can choose the new formatter under display settings for your teaser view mode of your content type. Works in views, too.

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

0

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.