Created
December 4, 2014 13:47
-
-
Save esolitos/92560db6e7820da04b5a to your computer and use it in GitHub Desktop.
Migrate Add Field Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Attaches a (group) of field collections to an node entity | |
* | |
* @param stdClass $node_entity The loaded node object. | |
* @param array $field_collections the array of structures describing the entity collections | |
* keyed by the entity collection field name. | |
* @return void | |
*/ | |
public function attachFieldCollections(&$node_entity, $field_collections) | |
{ | |
$last_changed = $node_entity->changed; | |
foreach ($field_collections as $field_coll_name => $field_collection_group) { | |
for ($i=0; $i < count($field_collection_group); $i++) { | |
$field_coll_fields = $field_collection_group[$i]; | |
$field_c = entity_create('field_collection_item', array('field_name' => $field_coll_name)); | |
$field_c->setHostEntity('node', $node_entity); | |
$field_collection_w = entity_metadata_wrapper('field_collection_item', $field_c); | |
foreach ($field_coll_fields as $subfield_name => $subfield_value) { | |
$field_collection_w->{$subfield_name}->set( $subfield_value ); | |
} | |
$field_collection_w->save(); | |
} | |
} | |
// Save the changes on the node and restore the last changed time to the original | |
node_save($node_entity); | |
self::setNodeChanged($node_entity->nid, $last_changed); | |
} | |
public static function setNodeChanged($nid, $changed) | |
{ | |
$query = db_update('node') | |
->fields(array( 'changed' => $changed )) | |
->condition('nid', $entity->nid, '='); | |
@$query->execute(); | |
} | |
?> | |
To use it you need to byuld an array with this structure: | |
<?php | |
$someArray = array( | |
'field_collection_name' => array( // field_collection_name is the actual (machine) name of the field collection | |
array( | |
'field_name' => 'some to give value' // field_name is tha name of a field inside a field collection | |
'field_some_file' => array('fid'=>$file_id) // you can passa also "complex types" | |
), | |
), | |
// ecc... | |
); | |
?> | |
Example #1: | |
<?php | |
$field_collections['field_main_image'] = array( | |
array( | |
'field_image' => array('fid' => $migrated_file_id), | |
'field_image_caption' => $caption, | |
), | |
); | |
$this->attachFieldCollections($entity, $field_collections); | |
?> | |
To attach more items of one field collection type: | |
<?php | |
$field_collections['field_blockquote'] = array( | |
array( | |
'field_quote_title' => $first_quote_title, | |
'field_quote_content' => $first_quote_text, | |
), | |
array( | |
'field_quote_title' => $second_quote_title, | |
'field_quote_content' => $second_quote_text, | |
), | |
); | |
// OR also | |
$field_collections['field_blockquote'] = array(); | |
// later on in a loop (for/foreach/whatever) | |
$field_collections['field_blockquote'][] = array( | |
'field_quote_title' => $quote_title, | |
'field_quote_content' => $quote_content, | |
); | |
$this->attachFieldCollections($entity, $field_collections); | |
?> | |
Another option is to add multiple field collections to different fields, also easy: | |
<?php | |
$field_collections['field_main_image'] = array( | |
array( | |
'field_image' => array('fid' => $migrated_file_id), | |
'field_image_caption' => $caption, | |
), | |
); | |
$field_collections['field_blockquote'] = array( | |
array( | |
'field_quote_title' => $first_quote_title, | |
'field_quote_content' => $first_quote_text, | |
), | |
array( | |
'field_quote_title' => $second_quote_title, | |
'field_quote_content' => $second_quote_text, | |
), | |
); | |
$this->attachFieldCollections($entity, $field_collections); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment