Skip to content

Instantly share code, notes, and snippets.

@pifantastic
Created October 27, 2010 21:14
Show Gist options
  • Save pifantastic/650012 to your computer and use it in GitHub Desktop.
Save pifantastic/650012 to your computer and use it in GitHub Desktop.
<?php
/**
* Clones a node by directly saving it.
*/
function clone_node_save($nid) {
if (is_numeric($nid)) {
global $user;
$node = node_load($nid);
if (isset($node->nid) && clone_is_permitted($node->type)) {
$original_node = drupal_clone($node);
$node->nid = NULL;
$node->vid = NULL;
$node->tnid = NULL;
$node->name = $user->name;
$node->uid = $user->uid;
$node->created = NULL;
$node->menu = clone_node_clone_menu_link($original_node);
$node->book['mlid'] = NULL;
$node->path = NULL;
$node->files = array();
$node->title = t('Clone of !title', array('!title' => $node->title));
// Add an extra property as a flag.
$node->clone_from_original_nid = $original_node->nid;
if (variable_get('clone_reset_'. $node->type, FALSE)) {
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
// Fill in the default values.
foreach (array('status', 'moderate', 'promote', 'sticky', 'revision') as $key) {
$node->$key = in_array($key, $node_options);
}
}
// Let other modules do special fixing up.
// The function signature is: hook_clone_node_alter(&$node, $original_node, $method)
// Where $method is either 'prepopulate' or 'save-edit'.
drupal_alter("clone_node", $node, $original_node, "save-edit");
node_save($node);
return $node->nid;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment