You are here

function clone_node in Node clone 5

Same name and namespace in other branches
  1. 5.2 clone.module \clone_node()

Clones a node

2 calls to clone_node()
clone_node_check in ./clone.module
Menu callback: prompt the user to confirm the operation
clone_node_confirm_submit in ./clone.module
Handle confirm form submission

File

./clone.module, line 183

Code

function clone_node($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->name = $user->name;
      $node->uid = $user->uid;
      $node->created = 0;
      $node->menu = NULL;
      $node->path = NULL;
      $node->files = array();

      // Remove CCK file and image attachements
      if (module_exists('imagefield') || module_exists('filefield')) {
        $content_type = module_invoke('content', 'types', $node->type);

        // Find all the fields that are files or images.
        foreach ($content_type['fields'] as $data) {
          if ($data['type'] == 'file' || $data['type'] == 'image') {
            $key = $data['field_name'];

            // Remove any attached files as with $node->files
            if (isset($node->{$key})) {
              $node->{$key} = array();
            }
          }
        }
      }

      // Add indicator text to the title
      $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)
      foreach (module_implements('clone_node_alter') as $module) {
        $function = $module . '_clone_node_alter';
        $function($node, $original_node, "save-edit");
      }
      node_save($node);
      drupal_goto('node/' . $node->nid . '/edit');
    }
  }
}