condition('entity_type', 'node') ->condition('bundle', $content_type); $query->addExpression('DISTINCT entity_id', 'nid'); $query->addExpression('revision_id', 'vid'); $nodes_result = $query->execute(); foreach ($nodes_result as $node) { // Construct the legacy multigroup for the node from the individual fields. $multigroup_data = array(); foreach ($multigroup_fields as $field) { $field_result = db_select('field_data_' . $field, 'field') ->fields('field') ->condition('entity_type', 'node') ->condition('entity_id', $node->nid) ->execute(); foreach ($field_result as $field_item) { $multigroup_data[$field_item->delta][$field] = $field_item; } } // Step through the reconstructed multigroups, which are collections from now. foreach ($multigroup_data as $delta => $data) { // Create entry in field_collection_item table. $id = db_insert('field_collection_item') ->fields(array('field_name' => $collection_field)) ->execute(); // Attach collection field data to the node. db_insert('field_data_' . $collection_field) ->fields(array( 'entity_type' => 'node', 'bundle' => $content_type, 'entity_id' => $node->nid, 'revision_id' => $node->vid, 'language' => 'und', 'delta' => $delta, $collection_field . '_value' => $id, )) ->execute(); // Go through all the fields in the multigroup. foreach ($data as $multigroup_field => $field_data) { // Reassign the fields in the multigroup from the node to the collection field instance. db_update('field_data_' . $multigroup_field) ->fields(array( 'entity_type' => 'field_collection_item', 'bundle' => $collection_field, 'entity_id' => $id, 'revision_id' => $id, 'delta' => 0, )) ->condition('entity_type', 'node') ->condition('entity_id', $node->nid) ->condition('delta', $delta) ->execute(); } } } } ?>