function node_convert_field_convert in Node Convert 6
Same name and namespace in other branches
- 5 node_convert.module \node_convert_field_convert()
- 7 node_convert.util.inc \node_convert_field_convert()
Transfers information from source_field to dest_field.
Parameters
$nid: The nid of the node to be converted.
$source_field: A string containing the name of the source field which contains to be transfered information.
$dest_field: A string containing the name of the destination field where the information should be strored.
Return value
Returns additional data needed for further operations. Currently contains the changed node body and teaser.
1 call to node_convert_field_convert()
- node_convert_node_convert in ./
node_convert.module - Converts a node to another content type.
File
- ./
node_convert.module, line 671 - The node_convert module converts nodes from one type to another.
Code
function node_convert_field_convert($nid, $source_field, $dest_field) {
$node = node_load($nid, NULL, TRUE);
$vid = $node->vid;
$field_info_source = content_fields($source_field);
// Get source field information
$db_info_source = content_database_info($field_info_source);
// Get DB specific source field information
// If the source field has a separate table, we will have to delete the node data in it
if (strpos($db_info_source['table'], "content_field_") !== FALSE) {
$db_source_content_field = TRUE;
}
else {
$db_source_content_field = FALSE;
}
if ($dest_field == 'discard') {
// Delete node info in the separate field table
if ($db_source_content_field == TRUE) {
db_query("DELETE FROM {%s} WHERE nid = %d", $db_info_source['table'], $nid);
}
return;
}
$field_info_dest = content_fields($dest_field);
// Get destination field information
$db_info_dest = content_database_info($field_info_dest);
// Get DB specific destination field information
if (strpos($db_info_dest['table'], "content_field_") !== FALSE) {
$db_dest_content_field = TRUE;
}
else {
$db_dest_content_field = FALSE;
}
$data = array();
$dest_column_names = array();
$source_column_names = array();
$column_placeholders = array();
$column_assignments = array();
$source_values = array();
// Collect the DB columns names of the source field
foreach ($db_info_source['columns'] as $column => $attributes) {
$source_column_names[] = $attributes['column'];
}
// If the field has multiple values, we remember each value from the DB
if ($field_info_source['multiple'] != 0) {
$query = db_query("SELECT " . implode(", ", $source_column_names) . ", delta FROM {%s} WHERE nid = %d AND vid = %d", $db_info_source['table'], $nid, $vid);
while ($db_row = db_fetch_array($query)) {
$source_values[] = array_merge(array_values($db_row), array(
$nid,
$vid,
));
}
}
else {
$source_values = db_fetch_array(db_query("SELECT " . implode(", ", $source_column_names) . " FROM {%s} WHERE nid = %d AND vid = %d", $db_info_source['table'], $nid, $vid));
// array_values throws a error if $source_values is NULL, so we catch this.
if (!empty($source_values)) {
$source_values = array_values($source_values);
}
else {
$source_values[] = '';
}
$source_values[] = $nid;
$source_values[] = $vid;
}
// After getting the source field values, we delete them in the DB
if ($db_source_content_field == TRUE) {
db_query("DELETE FROM {%s} WHERE nid = %d", $db_info_source['table'], $nid);
}
// The source field value should be appended to the body or replaced.
if ($dest_field == APPEND_TO_BODY || $dest_field == REPLACE_BODY) {
static $node_body = '';
static $node_teaser = '';
if (empty($node_body)) {
$node_body = $node->body;
$node_teaser = $node->teaser;
}
foreach ($source_values as $values) {
// If there are multiple values
if (is_array($values) && !empty($values[0])) {
if ($dest_field == APPEND_TO_BODY) {
$node_body = $node_body . "\n" . $values[0];
$node_teaser = $node_teaser . "\n" . $values[0];
}
elseif ($dest_field == REPLACE_BODY) {
$node_body = $values[0];
$node_teaser = $values[0];
}
// A single value
}
elseif (!is_array($values) && !empty($values)) {
if ($dest_field == APPEND_TO_BODY) {
$node_body = $node_body . "\n" . $values;
$node_teaser = $node_teaser . "\n" . $values;
}
elseif ($dest_field == REPLACE_BODY) {
$node_body = $values;
$node_teaser = $values;
}
return;
// We return because there's only a single value, and the rest are vid and nid -> unuseful.
}
}
return array(
'body' => $node_body,
'teaser' => $node_teaser,
);
}
// Prepare the INSERT and UDPATE queries, for transfering the values in the destination fields
foreach ($db_info_dest['columns'] as $column => $attributes) {
$dest_column_names[] = $attributes['column'];
switch ($attributes['type']) {
case 'int':
case 'mediumint':
case 'tinyint':
case 'bigint':
$column_placeholders[] = '%d';
$column_assignments[] = $attributes['column'] . ' = %d';
break;
case 'float':
$column_placeholders[] = '%f';
$column_assignments[] = $attributes['column'] . ' = %f';
break;
default:
$column_placeholders[] = "'%s'";
$column_assignments[] = $attributes['column'] . " = '%s'";
}
}
// If the field has multiple values, we put each value into the DB
if ($field_info_source['multiple'] != 0) {
foreach ($source_values as $values) {
$db_message = db_query("INSERT INTO {" . $db_info_dest['table'] . "} (" . implode(", ", $dest_column_names) . ", delta, nid, vid) VALUES (" . implode(', ', $column_placeholders) . ", %d, %d, %d)", $values);
}
}
else {
if ($db_dest_content_field == TRUE) {
$db_message = db_query("INSERT INTO {" . $db_info_dest['table'] . "} (" . implode(", ", $dest_column_names) . ", nid, vid) VALUES (" . implode(', ', $column_placeholders) . ", %d, %d)", $source_values);
}
else {
$db_message = db_query("UPDATE {" . $db_info_dest['table'] . "} SET " . implode(", ", $column_assignments) . " WHERE nid = %d AND vid = %d", $source_values);
}
}
}