You are here

function clone_node in Node clone 5.2

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

Clones a node - prepopulate a node editing form

1 string reference to 'clone_node'
clone_menu in ./clone.module
Implementation of hook_menu().

File

./clone.module, line 142

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 = NULL;
      $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,
      ));
      drupal_set_title(check_plain($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);
        }
      }
      if (empty($_POST['op'])) {
        drupal_set_message(t('This clone will not be saved to the database until you submit.'));
      }

      // 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, "prepopulate");
      }
      return drupal_get_form($node->type . '_node_form', $node);
    }
  }
}