I'm currently working in a custom video player. For that I've created a content type which uses a field_collection (for the video file and other stuff) and a field_caption for a XML file.

The video file field uses a custom field formatter, which I'm using to theme up the player. I need the field_caption file to be added there so I'm trying to get the node id of the field_section Field Collection in the hook_field_formatter_view() to get that info....with no luck!

If I print $entity->vid from the function parameters I will have NULL, because the field is within a field_section I believe.

Is there a way round this?

Thanks in advance!


As the Field Collection docs says:

A field collection is internally represented as an entity, which is embedded in the host entity

So what you need to do in your field, is:

  1. Test if it's container is a node. This case works already.
  2. Test if it's a Field Collection entity.
  3. Fallback to situation when you can't obtain parent container or it's an unexpected entity.

Now we'll take care about 2. point. If you can safely assume your field is always in a node, you can use code from answer by Chris Muench:

$model = entity_load_single('field_collection_item', $model_id);
$node = $model->hostEntity();
echo $node->nid;

Note that you are writing formatter, and it means you have no guarantee that host entity will always be a node. If it's not, go to point 3 on my list.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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