Example: Programmatically adding field collection entries into a existing field collection parent item
Some times we find ourselves with Field Collection that have other Field Collection as childs and we need to add data to those child fields programatically. Luckily this is something that can be done, but we just need to know the correct syntaxis to do it, and that is not always obvious.
So let's go directly to the issue and lets propose this structure:
- NodeX has one field called FieldCollectionParent.
- Now FieldCollectionParent is defined as having one of it's fields a field Called FieldCollectionChild.
- FieldCollectionChild is a field collection with 3 text fields TextField1,TextField2 and TextField3
We want to add content to TextField1,TextField2 and TextField3 from a source that returns for example 5 sets of content, so we need to add 5 FieldCollectionChild to our FieldCollectionParent.
Let's say that we have an array called $processedData that store those 5 sets.
// Our example loop array.
$processedData = array(
array('text1'=>'First Text Field - Item 1',
'text2'=>'Second Text Field - Item 1',
'text3'=>'Third Text Field - Item 1'),
array('text1'=>'First Text Field - Item 2',
'text2'=>'Second Text Field - Item 2',
'text3'=>'Third Text Field - Item 2'),
array('text1'=>'First Text Field - Item 3',
'text2'=>'Second Text Field - Item 3',
'text3'=>'Third Text Field - Item 3'),
array('text1'=>'First Text Field - Item 4',
'text2'=>'Second Text Field - Item 4',
'text3'=>'Third Text Field - Item 4'),
array('text1'=>'First Text Field - Item 5',
'text2'=>'Second Text Field - Item 5',
'text3'=>'Third Text Field - Item 5')
);
This is how we are going to achieve this:
try {
$values = array('field_name' => 'field_collection_child');
// We bring The node
// We need to have the node id (vid is optional actually) previous to this
$node = node_load($nid,$vid);
// We get the Entity Id of the parent field collection
$eid = $node->field_collection_parent[LANGUAGE_NONE][0]['value'];
// We load the parent field collection
$field = entity_load('field_collection_item', array($eid));
foreach ($processedData as $item)
{
// Here we create a new field collection that is
// going to be the child one
$fc_item = entity_create('field_collection_item', $values);
// Now since this child is not attached to the
// main node but to a field collection
// we use the setHostEntity but we pass the
// machine name of the parent field collection instead of 'node'
// and we pass here the parent field
// The setHostEntity function is provided by
// the Field Collection module.
$fc_item->setHostEntity('field_collection_parent', $field);
// Here we setup our desired values into the child collection
$fc_item->field_text1[LANGUAGE_NONE][0]['value'] = $item['text1'];
$fc_item->field_text2[LANGUAGE_NONE][0]['value'] = $item['text2'];
$fc_item->field_text3[LANGUAGE_NONE][0]['value'] = $item['text3'];
// We save the child collection
$fc_item->save();
}
}catch (Exception $e) {
echo '<pre>' . $e->getTraceAsString() . '</pre><br>';
echo $e->getMessage();
}
In this way you can have the data parsed and added to the child collection and correctly attached to the parent one.
Disclaimer: Some parts of the code are taken from different parts of the documentation and from the internet. I do not pretend to claim ownership of those.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion