function node_convert_node_convert in Node Convert 7
Same name and namespace in other branches
- 5 node_convert.module \node_convert_node_convert()
- 6 node_convert.module \node_convert_node_convert()
Converts a node to another content type.
Parameters
$nid: The nid of the node to be converted.
$destination_node_type: A string containing the destination node type of the node.
$source_fields: An array containing the source field names.
$destination_fields: An array containing the destination field names.
$no_fields_flag: A boolean containing if there are source fields that have to be converted.
$hook_options: An array containing values used by the hook_node_convert_change functions.
Return value
bool Returns the $nid.
5 calls to node_convert_node_convert()
- NodeConvertTestCase::testInvalidNidConvert in ./
node_convert.test - Tests converting an invalid nid.
- NodeConvertTestCase::testSimpleNodeConvert in ./
node_convert.test - Tests simple node conversion.
- node_convert_conversion_form_submit in ./
node_convert.forms.inc - Submit callback for converting a form.
- node_convert_convert_action in ./
node_convert.module - Implements hook_action, exposing predefined conversion templates as actions.
- node_convert_convert_nodes_using_template in ./
node_convert.module - Converts a list of nodes using a given template
File
- ./
node_convert.util.inc, line 25 - API and Utility Functions.
Code
function node_convert_node_convert($nid, $destination_node_type, $source_fields, $destination_fields, $no_fields_flag, $hook_options = NULL) {
$node = node_load($nid);
if ($node == FALSE) {
return FALSE;
}
// Change the node type in the DB
db_update('node')
->fields(array(
'type' => $destination_node_type,
))
->condition('nid', $nid)
->execute();
// If there are fields that can be converted
if ($no_fields_flag == FALSE) {
// Conversion process for each field
$re_save_body_field = FALSE;
// Use node revisions to extract all field revision in node_convert_field_convert
$node_revisions = node_revision_list($node);
foreach ($source_fields as $key => $field) {
$replaced_body = node_convert_field_convert($node, $field, $destination_fields[$key], $destination_node_type, $node_revisions);
if ($replaced_body == REPLACE_BODY) {
$re_save_body_field = TRUE;
}
}
// If something was appended to the body, or replaced the body, we update body field.
if ($re_save_body_field == TRUE) {
$field_body = field_info_fields();
$field_body = $field_body['body'];
$field_ids = array(
$field_body['id'] => $field_body['id'],
);
module_invoke($field_body['storage']['module'], 'field_storage_write', 'node', $node, FIELD_STORAGE_UPDATE, $field_ids);
}
}
// We collate data to send to the hook implementations
$data = array(
'node' => $node,
'dest_node_type' => $destination_node_type,
'source_fields' => $source_fields,
'destination_fields' => $destination_fields,
'no_fields_flag' => $no_fields_flag,
);
if (!empty($hook_options)) {
$data['hook_options'] = $hook_options;
}
// We make sure that all custom node modules do their changes at the appropriate steps
node_convert_invoke_all('node_convert_change', $data, 'insert');
node_convert_invoke_all('node_convert_change', $data, 'delete');
// TODO Check what to do about token cache clearing.
/* if (module_exists('token')) {
token_get_values('global', NULL, TRUE);
}*/
// Clear the node cache, so we have the latest information when saving the
// node.
$controller = entity_get_controller('node');
/* @var $controller DrupalEntityControllerInterface */
$controller
->resetCache(array(
$node->nid,
));
cache_clear_all('field:node:' . $node->nid, 'cache_field');
$converted_node = node_load($nid, NULL, TRUE);
$converted_node->type = $destination_node_type;
try {
module_invoke_all('node_convert_presave', $converted_node, $hook_options);
node_save($converted_node);
if (module_exists('rules')) {
rules_invoke_event('node_convert_converted_node', $converted_node);
}
} catch (Exception $e) {
drupal_set_message(t('Caught exception: @exception', array(
'@exception' => $e
->getMessage(),
)), 'error');
$nid = FALSE;
}
return $nid;
}