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