2

On my site I've an image field, multi value. I would like, on a custom view mode, to display only the first image. On the field formatter I don't have this option.

How should I do this? Directly from twig or I've to use a preprocess function?

[EDIT] I would to do this without using a custom node template :)

2 Answers 2

4

Then it's

{{ content.field_name[0] }}

Note: If you need to print the label use {{ content.field_name['#title'] }} as {{ content.field_name[0] }} does not print the label.

4
  • No, I'm not using views, I'm working on node template.
    – arrubiu
    Jul 9, 2016 at 10:00
  • 1
    @arrubiu updated answer
    – No Sssweat
    Jul 9, 2016 at 10:02
  • 1
    Ok, works :) And it was simple to imagine it ;)
    – arrubiu
    Jul 9, 2016 at 10:08
  • 1
    @arrubiu if you need the label of the image field use {{ content.field_name['#title'] }} as {{ content.field_name[0] }} does not print the label.
    – No Sssweat
    Jul 9, 2016 at 10:12
1

There are now a couple of modules which look like they could help.

https://www.drupal.org/project/single_image_formatter

This module provides formatters for Image, Media and Responsive image field, that displays only first value of multi-valued fields. It is useful, when you configure teasers display and don't need to show all images.

https://www.drupal.org/project/field_formatter_range

The Field Formatter Range module provides the option to display only selected range of values for multivalued entity fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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