# Can I access node ID of the parent node of a field collection field in field-collection-item--FIELD_NAME.tpl.php

I am just hoping to get the node ID of the parent node in a field-collection-item--name.tpl.php template file.

So if my parent node name is mypage in which I have child fields of field-collection-item--name.tpl.php, I just want to access the Node ID of the parent page (mypage) in field-collection-item--name.tpl.php.

Is it possible?

Apparently somewhat yes. If I do this, I get details on the parent node. The only problem is that that data is protected. The output of this line is pasted below. All I want is [vid] value (i.e. 120)

  $var =$content["field_image_title"]["#object"];
print_r ($var);  ## Output of above line FieldCollectionItemEntity Object ( [fieldInfo:protected] => [hostEntity:protected] => stdClass Object ( [vid] => 120 [uid] => 1 [title] => My Parent Page Name [log] => [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [vuuid] => f3c2f63d-5d86-4d94-98f3-8a1ba76ce1f9 [nid] => 120 [type] => subpage [language] => und [created] => 1408623334 [changed] => 1408640101 [tnid] => 0 [translate] => 0 [uuid] => 486dead0-4f72-45a3-988c-64512e6b91de [revision_timestamp] => 1408640101 [revision_uid] => 1  Any help on how I can get this vid value in this protected array? Thanks. • Those items in your debug output are Objects and not Arrays. It's not recommended to place business logic into template files whose purpose is for view output. – tenken Aug 22 '14 at 3:06 • Thanks tenken, so what shoudl I do ? – user219492 Aug 22 '14 at 3:19 ## 4 Answers Load the field collection using entity_load by passing the entity id. It gives you object where you can get the Host entity of the loaded entity. example profile2 will have user entity as host entity, or commerce line item will have commerce order as host entity. $entity = entity_load('field_collection_item', $your_file_collection_id);$host_entity = $entity->hostEntity(); echo$host_entity->nid;

• Hello and welcome. this site aims to be a knowledge repository, not a free code writing service. If this code answers the question, please explain why and how. Explanations are more useful for future visitors than specific code, and even for OP - it's always better if people know what they are doing. – Mołot Dec 29 '14 at 8:11

I know this question is kind of old but I wanted to just give a quick answer because I had the same problem and stumbled upon here and the existing answers didn't satisfy me (though I'm not saying they don't work, I just found a simpler way to solve my problem). I just hope it may help anyone else finding this thread.

Anyway my solution is to just use the hostEntity() method of the field-collection entity. Simple example:

$var =$content["field_image_title"]["#object"];
$parent_node =$var->hostEntity();
$nid =$parent_node->nid; // for the node id, similarily for other values of the node.


You can access that information using a Preprocess function on the field from your themes template.php

Something along the lines of http://dropbucket.org/node/764

function MYTHEME_preprocess_field(&$vars) { // you need to know the name of your field collection here. // dpm($vars) by using the Devel module may help you figure that out.
if($vars['element']['#field_name'] == "field_your_fc_field") {$fcs = array();
foreach (element_children($vars['element']) as$key) {
$fcs[] = array_pop($vars['element'][$key]['entity']['field_collection_item']); } // now you have all your fcs pulled out of that nasty array foreach ($fcs as $fc) { # code ... # Can access the VID or NID you want #$nid = $fc->nid; #$vid = $fc->vid; } // I would recommend overrideing items markup for whatever black magic you desire$vars['items'] = array(
'#markup' => "Whatever you need to render",
);
}
}

• Hi tenken, thank you very much for answering my question. It seems though that there is somehow a problem with this line foreach (element_children($vars['element']) as$key) { $fcs[] = array_pop($vars['element'][$key]['entity']['field_collection_item']); . The internal array layout is not like that and hence this returns empty. – user219492 Aug 22 '14 at 5:32 All I needed was the nid of the parent, so I wound up going with: $parent_node = menu_get_object();


This gave me access to the full node info, and I could use what was needed. It's not quite as elegant as the solution above, but I wanted to keep it simple.