Skip to main content
Managing field groups in Drupal with the Multifield module
September 16, 2013 |

Well known Drupal contributor Dave Reid--who, by the way, has a son who was recently named the first Drupal 9 maintainer!--recently did an AMA ("Ask Me Anything") on Reddit and one of the gems to come out of that discussion was a link to his new Multifield module. The Multifield module aims to be a "true compound field solution" which basically means it lets you create reusable groups of fields which can be used in multiples and on different entities/bundles. In other words, it provides the same functionality as the widely used Field Collection module (which itself is very similar to using an Entity Reference field in combination with Inline Entity Forms), but Multifield is a good bit simpler behind the scenes since Field Collections are stored as entities which are referenced, whereas Multifields are basically just fields. One downside (if you can call it that) of this approach is that you lose the ability to edit content in each field group individually like you have in Field Collection meaning you have to edit it within the context of the node or entity that it's attached to, but I've found that to be a fairly unused feature of Field Collection so it's not a great loss. So how can we set this up? Let's take a walk.

Making a new Multifield

Have you downloaded and enabled the Multifield module? Of course you have you sly devil. Now then, let's create our first Multifield. Head on over to Admin → Structure → Multifield and click the "+ Add" link at the top. You'll see a nice little form that looks like this one:



I'm going to be making a "Photos" Multifield to hold things like the image itself, a title, a description, and a source. So for this example, I'll call it "Photos" because I'm a genius. You can name your Multifield basically whatever you want, but note that what you enter for the "Multifield type label" is pretty important because it's what will show up in the little "Field type" dropdown when you're adding a new field to an entity in the "Manage Fields" UI. For example, if you create a new "Videos" Multifield, you'll see "Videos" in the select dropdown along with all the basics such as "File", "Image", "List", "Text", etc. So choose wisely. In general, I personally think it makes sense to choose a plural name simply because most of the cases where I'd want to use a Multifield would be so that I could add multiple field groups to an entity (if I didn't want to add multiples then I'd probably just use regular old fields on the entity type itself), but that's up to you. Once you click save, you'll be taken to a standard "Manage FIelds" page for the Multifield you just created. This is where you get to add the fields that make up your Multifield. In my case with my "Photos" multifield, I want to add a field for an image, a title, a description, and a source, leaving me with something like this:



And there we have it. Our Multifield is created and ready to use.

Adding your Multifield to a node (or other entity) type

To use it, you'll obviously want to add it to an entity/node type so that you can give it some data. In my case, I added it to the Article content type just for kicks and giggles. So on the content type's "Manage Fields" tab, add a new field, and choose your Multifield in the FIeld dropdown.



Like I said, it should show up in the dropdown list as whatever you originally named it. That'll take you to a form to configure it, and the form should look familiar to you if you've ever added a field to something via the field UI before. The only neat thing to note here is that Multifield gives you the option to set defaults for each of the sub-fields in the Multifield. In my case with my "Photos", it looks like this:



Pretty cool, right? Once you submit that form, your new Multifield should be added to your entity type and you can start using it. In my case, this means I create a new Article node and I see the Multifield on the node creation form like so:



Notice that each of the Multifield's subfields are represented there, and you can rearrange them via the little grabber on the left and add more via the "Add another item" button at the bottom. We're in business folks.

Making your Multifield visible

Once you've added some data into your Multifield it's probably a good idea to make sure it's showing up in the front end. So I'll just view the node I just created, and whadya know, there be data!



(Yeah it's not the most beautiful thing you've ever seen, but this isn't a theming blog post, so just whatever!) It shows up fine when viewing the node itself, but how about in a View? Does Multifield have decent Views support? Sho nuff, and it looks just like any other multi-value field looks.



Since this is a multi-value field we're adding to the View, there's the "Multiple Field Settings" collapsible fieldset there that lets you choose how many values to display and how to separate them (into a list or by commas or whatever), and the rest is basically the same as adding any other field to a view. And if we preview it, it shows up! Magic!



Customizing the display

Obviously no client is going to let you get away with just using the default display for things like this, so we'll have to do some customization. At a basic level it's just a matter of opening up the Multifield's "Manage Display" tab and changing stuff. For example, I'll want to use a custom image style for my image and change the order of my fields a bit, so that it looks more like this:



That will update anywhere the default display is being used, like on nodes that you attach it to or in Views. Now, you might want to have different display settings for different contexts. If so, you'll want to use Display Suite or the much simpler Entity View Modes module (also by Dave Reid) to tell Drupal about some custom display formats. Once Drupal knows about them, you can choose display options per-format and then in the node's "Manage Display" tab or in the View's field settings, you can choose to instead use one of your custom display formats for the Multifield. In essence, this is what you'd do with an Entity Reference or something like that as well. And there we have it--a simple, lightweight alternative to Field Collection that works like a champ and is a a breeze to set up. Thanks Dave!



As a Senior Software Architect at Phase2, Mike does in-the-trenches development and also leads development teams. Mike's job is to make sure development runs smoothly, everyone is able to be productive, and blockers are resolved quickly. Mike is also a staff manager, working with developers to on their education and development.

Jump back to top