0

What does drupal_clone($node) do in Drupal? Will it clone a node? I know the node will be saved only if node_save($clone) is rendered. So what will happen when drupal_clone($node) is rendered ?

/**
 * Generate a node preview.
 */
function node_preview($node) {
    if (node_access('create', $node) || node_access('update', $node)) {
        // Load the user's name when needed.
        if (isset($node->name)) {
            // The use of isset() is mandatory in the context of user IDs, because
            // user ID 0 denotes the anonymous user.
            if ($user = user_load(array('name' => $node->name))) {
                $node->uid = $user->uid;
                $node->picture = $user->picture;
            } else {
                $node->uid = 0; // anonymous user
            }
        } else if ($node->uid) {
            $user = user_load(array('uid' => $node->uid));
            $node->name = $user->name;
            $node->picture = $user->picture;
        }

        $node->changed = time();

        // Extract a teaser, if it hasn't been set (e.g. by a module-provided
        // 'teaser' form item).
        if (!isset($node->teaser)) {
            $node->teaser = empty($node->body) ? '' : node_teaser($node->body, $node->format);
            // Chop off the teaser from the body if needed.
            if (!$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
                $node->body = substr($node->body, strlen($node->teaser));
            }
        }

        // Display a preview of the node.
        // Previewing alters $node so it needs to be cloned.
        if (!form_get_errors()) {
            $cloned_node = drupal_clone($node);
            $cloned_node->build_mode = NODE_BUILD_PREVIEW;
            $output = theme('node_preview', $cloned_node);
        }
        drupal_set_title(t('Preview'));

        return $output;
    }
}

Real Issue

I am getting the node cloned when clicking the preview button. Is it related to any code changes?

4
  • What happens when you try it?
    – Clive
    Commented Nov 30, 2015 at 11:16
  • Its a default function to preview the node i guess , i printed out the $cloned_node and its printing the form variables submitted .But its not saved though nor making any clones
    – KTM
    Commented Nov 30, 2015 at 11:22
  • It's lower level than that - it's simply a function to clone an object, the fact you're passing it a node is irrelevant (it doesn't know what a node is). It's only there to provide object cloning for old versions of php. So the outcome of a render will be identical to the same render of the original object. To save and deal with a copy, you'd need to adjust nid and vid on the cloned object (and maybe other stuff)
    – Clive
    Commented Nov 30, 2015 at 11:29
  • @clive I am getting the node cloned when clicking the preview button , do you know how its came like that ?
    – KTM
    Commented Nov 30, 2015 at 12:08

2 Answers 2

1

allows users to make a copy of an existing item of site content (a node) and then edit that copy. The authorship is set to the current user, the menu and url aliases are reset, and the words "Clone of" are inserted into the title to remind you that you are not editing the original content.

https://www.drupal.org/project/node_clone

0

drupal_clone exists because Drupal 6 originally supported PHP 4.x, which didn't have a clone keyword (the min now is 5.x; I don't totally recall when this happened, but there was a big purge a few years ago to rid the planet of PHP 4.x).

To clone an object in PHP 4, you just used the = operator. In PHP 5, the = operator with an object will create a new reference.

This just clones the new object in memory. It should behave the same as if you loaded it from the database, but nothing DB related will happen to it unless you explicitly cause something to happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.