How to Control the Display of Multi-Value Fields in Drupal

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

Fig 1.0

If you only want to display the first 2 images, then add 2 into the text field.

Fig 1.1

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.

Fig 1.2

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.

Fig 1.3

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.

13 thoughts on “How to Control the Display of Multi-Value Fields in Drupal”

  1. 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?

    1. 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.

      1. 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!

  2. 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.

  3. 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.

  4. 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?

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Media Management in Drupal

Download a FREE 8 part video course on managing media in Drupal.