You are here

function node_convert_node_convert in Node Convert 6

Same name and namespace in other branches
  1. 5 node_convert.module \node_convert_node_convert()
  2. 7 node_convert.util.inc \node_convert_node_convert()

Converts a node to another content type.

Parameters

$nid: The nid of the node to be converted.

$dest_node_type: A string containing the destination node type of the node.

$source_fields: An array containing the source field names.

$dest_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

Returns the $nid.

5 calls to node_convert_node_convert()
NodeConvertTestCase::testInvalidNidConvert in ./node_convert.test
NodeConvertTestCase::testSimpleNodeConvert in ./node_convert.test
node_convert_conversion_form_submit in ./node_convert.module
node_convert_convert_action in ./node_convert.module
node_convert_convert_nodes_using_template in ./node_convert.module
Converts a list of nodes using a given template

File

./node_convert.module, line 607
The node_convert module converts nodes from one type to another.

Code

function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag, $hook_options = NULL) {
  $node = node_load($nid);
  if ($node == FALSE) {
    return FALSE;
  }
  $vid = $node->vid;
  $source_node_type = $node->type;
  $tables_info = content_types();
  db_query("UPDATE {node} SET type = '%s' WHERE nid = %d", $dest_node_type, $nid);

  // Change the node type in the DB
  if (count($tables_info[$dest_node_type]['tables']) != 0) {
    db_query("INSERT INTO {%s} (nid, vid) VALUES (%d, %d)", "content_type_" . $dest_node_type, $nid, $vid);
  }

  // Add the current node to the chosen content type
  if ($no_fields_flag == FALSE) {

    // If there are cck fields that can be converted
    $additional_data = array();
    foreach ($source_fields as $key => $field) {

      // Conversion process for each field
      $additional_data = node_convert_field_convert($nid, $field, $dest_fields[$key]);
    }
    if (!empty($additional_data)) {
      db_query("UPDATE {node_revisions} SET body = '%s', teaser = '%s' WHERE nid = %d AND vid = %d", $additional_data['body'], $additional_data['teaser'], $node->nid, $node->vid);
    }
  }

  // We collate date to send to the hook implementations
  $data = array(
    'node' => $node,
    'dest_node_type' => $dest_node_type,
  );
  if (!empty($hook_options)) {
    $data['hook_options'] = $hook_options;
  }

  // We make sure that all custom node modules do their changes at the appropriate steps
  module_invoke_all('node_convert_change', $data, 'insert');
  module_invoke_all('node_convert_change', $data, 'delete');
  if (count($tables_info[$source_node_type]['tables']) != 0) {
    db_query("DELETE FROM {%s} WHERE nid = %d", "content_type_" . $source_node_type, $nid);

    // We delete the source node_type info
  }
  db_query("DELETE FROM {cache_content} WHERE cid = '%s'", "content:" . $nid . ":" . $vid);

  // We clear the cache
  cache_clear_all('node:' . $nid, 'cache_menu', 'TRUE');
  cache_clear_all('node/' . $nid, 'cache_menu', 'TRUE');
  if (module_exists('token')) {
    token_get_values('global', NULL, TRUE);
  }
  $converted_node = node_load($nid, NULL, TRUE);
  $node_user = user_load($converted_node->uid);
  node_save($converted_node);
  if (module_exists('rules')) {
    rules_invoke_event('node_convert_converted_node', $converted_node, $node_user);
  }
  return $nid;
}