Fields in Drupal can be configured to store a single value or multiple values. Displaying a single value field is fairly simple, but it gets tricky when you want to control multi-value fields.
For example, let’s say you add an image field to a blog content type which now allows you to upload multiple images. But you only want to display the first two images in the field, what is the best way to do this?
In this article, you’ll learn how to control the display of a multi-value field within a formatter using Display Suite and a few contributed modules. Then we’ll look at how to control a multi-value field in Views.
Using Display Suite
If you’re already using Display Suite, then you’re in luck because the module allows you to limit the number of displayed values from a multi-value field.
To limit the number, just go to the “Manage display” page on a particular content type and add your desired number in the text field (with a hash) beside the Format drop-down list
If you only want to display the first 2 images, then add
2 into the text field.
Now it’s important to note that this will work on any multi-value field; it doesn’t matter if it’s an image or text field.
Using Field Multiple Limit
The Field multiple limit module allows you to control the number of displayed values, but it goes one step further. It allows you to define which value to start from, in other words, the values you want to skip.
For example, let’s say you have an image field with 5 images (or values). You can use this module to display the fourth and fifth image within the field and skip the first three images.
So if you have a particular use case where you have to skip a certain number of values, then look at this module.
The Single Image Formatter module is very similar to Field multiple limit but only works on image fields.
Last but not least, Views can be used to control the number of displayed values. Similar to Field multiple limit, you can start from any value; however, you can not directly change a field formatter using Views.
To modify a multi-value field in Views, just edit the field and click on the “Multiple field settings” field-set. Within this field-set, you can change the number of displayed values and the starting value.
If all you want to do is control the number of values displayed and then use CSS to style the output, then use Field multiple limit, it’s a simple module and easy to use. For anything more complex; then look at Display Suite or Views.