You are here

function clone_node_prepopulate in Node clone 6

Same name and namespace in other branches
  1. 7 clone.pages.inc \clone_node_prepopulate()

Clones a node - prepopulate a node editing form

1 call to clone_node_prepopulate()
clone_node_check in ./clone.pages.inc
Menu callback: prompt the user to confirm the operation

File

./clone.pages.inc, line 139
Additional functions for Node_Clone module.

Code

function clone_node_prepopulate($original_node) {
  if (isset($original_node->nid)) {
    global $user;
    if (clone_is_permitted($original_node->type)) {
      $node = drupal_clone($original_node);
      $node->nid = NULL;
      $node->vid = NULL;
      $node->tnid = NULL;

      // Anyonmymous users don't have a name.
      // @see: drupal_anonymous_user().
      $node->name = isset($user->name) ? $user->name : NULL;
      $node->uid = $user->uid;
      $node->created = NULL;
      $node->menu = clone_node_clone_menu_link($original_node);
      if (isset($node->book['mlid'])) {
        $node->book['mlid'] = NULL;
        $node->book['has_children'] = 0;
      }
      $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;
      drupal_set_title(check_plain($node->title));

      // @see https://www.drupal.org/node/795394 the publishing options set here
      // are overwritten in node_object_prepare() and must be set again to the
      // desired values in clone_nodeapi(). However, we are setting the
      // publishing options here to the expected final values since that may be
      // helpful for alter hooks that might be inspecting those flags.
      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.
      drupal_alter("clone_node", $node, $original_node, "prepopulate");

      // Make sure the file defining the node form is loaded.
      module_load_include('inc', 'node', 'node.pages');
      return drupal_get_form($node->type . '_node_form', $node);
    }
  }
}