Drupal7: Multifield Module

David G - DrupalOn a recent project I want to have related fields within a content type and I want some flexibility on what that data is. An example of this I want an entity reference field and I want to be able to add a 2nd field that says what type of person this is, a husband, a wife. Or I may want a collection of media items and the ability to say what type of media this is. There are many solutions to this in Drupal, we are talking about compound fields or relational fields. Existing modules include Field Collection, the Relation module, Entity Reference (for 1 field) — and for display purposes you could think that Paragraphs meets this need. A relatively new module in this space is the Multifield module. Conceptually it is similar to the Field Collection module but instead of using Drupal Entities to store the compound data, it uses Fields within the existing entity (subfields essentially) and kind of uses some hacks to accomplish this. It attempts to improve performance in complex drupal sites by using Fields will use less MySQL Joins.

You install the module as you would any other module. Being a new module, and if you’re familiar with Field Collection you may be tempted to head over to Admin -> Structure -> Multifields to add a new multifield. But, that page does not have Add New button anywhere.

Instead you goto any content type you want to add a multifield too and use the typical Add Field option to choose multifield. You then give it a machine name as is typical of any Drupal field. Once this step is completed, you may then goto Structure -> Multifields and Edit the newly created field and add whatever sub-fields you want within the multifield.

After adding a multifield to a content type you can manage its subfields.

After adding a multifield to a content type you can manage its subfields.

Note that multifields accept multi-value collections. But every field within a multivalue must contain 1 entry per multifield. To clarify a multifield cannot contain in 1 entry other multiple values.

Subfield configuration is just like configuring a new Content Type or Entity's fields -- but this is a multifield. Its backend storage is different, and leaner.

Subfield configuration is just like configuring a new Content Type or Entity’s fields — but this is a multifield. Its backend storage is different, and leaner.

So this allows us to easily created nested relations, or compound fields. There exist some basic Views supports for this module at this time. Although, I found out that Entity API support was lacking without applying a patch (and so was erroring out, until I patched multifield).

After patching multifield I was able to use the Entity API metadata wrapper to easily create a multifield via code as seen in this issue:

$field_category_score = array(
   'field_category' => array(LANGUAGE_NONE => array(array('tid' => 10))),
   'field_score' => array(LANGUAGE_NONE => array(array('value' => 8.5))),
 );
  
// This would work;
 $wrapper->field_category_score->set(array($field_category_score));
  
// This would NOT work;
 $wrapper->field_category_score[] = $field_category_score;

I hope you try out this module and give your own feedback on how it works for you!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *