7

I have the following code. I am able to load the field collection item using entity_load_single. I want to be able to get the node id for a given field collection item. How can I achieve this?

function dc_car_guide_model($model_id)
{
    $model = entity_load_single('field_collection_item', $model_id);
    //How can I get nid?
}

2 Answers 2

9
$model = entity_load_single('field_collection_item', $model_id);
$node = $model->hostEntity();
echo $node->nid;
2
  • Is it possible to retrieve the data in a particular field used in Field Collection? A bit offside I know but am just curious. Commented Jul 1, 2013 at 15:21
  • Yes, the Field API works for fields on Field Collection entities as well. field_get_items('field_collection_item', $field_collection, 'field_FIELD_NAME'); will return an array of values for a named field on that field collection entity. You can also use the Entity metadata wrappers (considered the new, slick way to do this :-p). Just specify that you're loading a wrapper for a field_collection_item instead of a node.
    – thirdender
    Commented Sep 10, 2013 at 23:52
0

Using EntityFieldQuery:

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->fieldCondition('field_MYCOLLECTIONFIELD', 'value',  $model_id, '=');
  $result = $query->execute();

  if (!empty($result['node'])) {
    $nid = key($result['node']);
  }

replace field_MYCOLLECTIONFIELD with the machine name of the Field Collection field on your node.

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.