3

It seems that many Drupal gurus disfavor using Field Collection module to wrap fields inside a field. They say it's not the Drupal's way of doing it.

In my current scenario, I need a single field with before & after image subfields. I also need a recipe ingredients field, with ingredient name, unit size (using taxonomy reference) and serving amount subfields.

What is the preferred Drupal's way of doing such a thing? Should you have to code an entirely custom module for each field type?

Update

It seems that the ideal approach is to use a custom field type. Optionally you can use Drupal modules to group fields, such as Composed Field and Multifield, and not-so-favored, architecturally cumbersome, Field Collection. Another solution is to create a content-type for each group of fields and use Entity Reference to reference the field to it.

Ideally I'd like to see an example of a custom field type which implements a text and an image field, or something similar.

2
  • It sound like you really need a new entity type.
    – mpdonadio
    Commented Nov 3, 2013 at 13:17
  • @MPD how would you go about doing it. Should I create an entity with the attached fields and then reference it to it? Commented Nov 4, 2013 at 1:39

3 Answers 3

1

You can try https://drupal.org/project/composed_field

The Field collection uses a different approach for creating composed fields. Here are the main differences between this module and the "Field Collection": The host field created by "Field Collection" is in fact an entity that gets attached to the fieldable entities (nodes, taxonomy, comment, commerce products, etc). So, although you attach a Field Collection through the Field UI, what is being attached is not a field but an entity.

Once you get your new entity (host field) attached to your fieldable entity (node, comment, taxonomy, commerce product, etc) then you have to attach the fields ( what would be the subfields ) into the host field ( Field Collection entity).

Each subfield is actually a field on its own, that is, from the database point of view, each subfield is a single field.

"Composed Field" on the other hand attaches a field (host field), not an entity, to your fieldable entities (node, comment, taxonomy, commerce product, etc) through the Field UI. At the end of the day, even if you set a field with say 4 subfields, you will end up having only one field and a single value saved into the database.

Unless you have a very specific use case need "Composed Field" should do what you need with less effort and less resources.

One more module availabel to achive the similar task is https://drupal.org/project/multifield but it is currently in unstable status

This project seeks to provide a true compound field solution for Drupal 7. As much as I love Field collection, it still has to save actual entities, and can cause performance problems due to having to load all the referenced field collection entities on node, or parent entity load.

4
  • I am aware of the other modules. My question was rather - what is the correct way of doing it? What do the big guys do? Commented Nov 3, 2013 at 7:45
  • @timofey Who are these "big guys" and why are you convinced they don't use field collection? You must have quite a few sources for this, can you share them? Surely they haven't just said "that's not the Drupal way" without offering the alternative?
    – Clive
    Commented Nov 3, 2013 at 12:27
  • 1
    BTW I agree with what you're saying - I won't let field collection near a project, it's cumbersome and doesn't integrate well with a lot of things. I always create a custom field type when I need that functionality, I don't think there's an alternative
    – Clive
    Commented Nov 3, 2013 at 12:39
  • @Clive - I cannot remember the exact sources, but for example in drupal.stackexchange.com/questions/4034 Dave Reid said "isn't that how Drupal usually works", when asked for a field grouping solution. I also remember reading that Field Group module dropped the Field Collection module because it didn't see a need for it. Commented Nov 4, 2013 at 0:54
1

https://drupal.org/project/composed_field is working fine for wrapping field inside field. suppose, if you want to set phone no. with country code then
$cntrcode = isset($form_state['cntrcode']) ? $form_state['cntrcode'] : '+358'; $pno = isset($form_state['phno']) ? $form_state['phno'] : ''; $passenger->field_phone_number[und][0]['composed'] = array('1' => $cntrcode,'2' => $pno);
Above code wrapp both contry code and phone no. in single field

0
1

in such cases I change my way of analyzing the project. for example I separate the fields which I need to wrap and create another content type for theme and use entity reference to join theme together! as Drupal's views module is completely integrated with entity reference module, It has worked very nice for me.

2
  • This is somewhat similar to what Field Collection does, without actually involving the module. Commented May 12, 2015 at 22:51
  • using field group module is another way actually. try using entity reference! I'm sure you will enjoy it...
    – shekoufeh
    Commented May 14, 2015 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.