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.
Views
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.
Conclusion
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.
Hi, Ivan. Thanks for the tutorial–as usual, very informative. I have been trying to find a way to affect the input of the fields. For instance, I have a field that requires at least 3 values, but can accept unlimited values. If I set the number of records to three, I get three fields but no “Add More” button; if I set it to unlimited, I get a single field with an “Add More” button. Is it feasible to start with three fields AND have an add more button?
Thanks a lot.
In this scenario, I would set the “Number of values” to unlimited and then implement some type of validation that requires the user to enter in at least 3 values.
The validation can be implemented with a bit of custom code or look at the Field validation module (https://drupal.org/project/field_validation) and see if it does the job.
It is possible to choose how many input widgets you start with for a field with unilimited values.
See this answer from Drupal StackExchange: http://drupal.stackexchange.com/a/11221/487
(read the comments).
I think it can be made into a module with a nice UI, I’d code it for a bounty.
That’s useful, thanks for the link.
I originally went with the suggestion that Ivan had made (validation for at least 3 fields), but I think that the stackexchange link that you provided is a better match for what my client wants. In addition to the adjustment to $form[$field_name][$lang][#max_delta] = 3 noted in the answer to which you linked, I had to add the field instances, which I did by adding copies of the first field instance back into the $form[$field_name][$lang] array. So my code basically looks like this:
…
$form[$field_name][$lang][#max_delta] = 3;
$form[$field_name][$lang][] = $form[$field_name][$lang][0];
$form[$field_name][$lang][] = $form[$field_name][$lang][0];
…
I still have some testing to do, but at first blush it seems like this is exactly what I needed. Thanks to Capi and Ivan for the great suggestions!
Ivan!
Thank you! Very useful guide.
I some times have found answers to my questions on your site.
Thanks!
Hi, might be not exactly related to current topic. But in search of my solution I came across this page and thought may get some help. I have created content using Field collection module, each item has 8 fields and that set of 8 field is shown as one ITEM in the field collection. I have created 25 such ITEMS. The problem is all 25 ITEMS are show on single page only, there is no way in the field collection to add Pager. I tried views also, views considering that entire field collection as one entity so automatically all 25 coming on one page only. Same was I tried Multifield Module also, but same issue. I need only 5 Items on one page. I tried Smart paging also, but smart paging is for breaking the content which is in one field, but I have to break one field collection item with other. Very Thanks in Advance.
I’d recommend you try it with Views, but by the sounds of it, you’ve already tried it.
I would ask in the field collection issue queue or on Drupal Answers (http://drupal.stackexchange.com/).
Thanks for the tutorial. I have a question, how do I add the values of the multiple field? My view works perfectly, gives out integer results now I would like to sum up these values and display the total.
Hi Crawley,
To sum up values in Views look at using the “Views aggregations” setting. Found this on Drupal Answers, http://drupal.stackexchange.com/a/31542
Hope it helps.
i need limit multi-filed images to some nos, for example 8, but all need to display in new row. not in same. how to do it
Hi Ivan this is very useful i have a slightly different use case
I want to display fields from the multifield children in the parent view. To do this I can add a relationship to the view for “Content referenced from field_multifield” but the problem with this is that it breaks the views limit you are using above and displays all children in the multifield. Is there a way to limit the multifield count and access child nodes?
Sorry, I’ve never used the multifield module and its field type.