Node Copy

Events happening in the community are now at Drupal community events on www.drupal.org.
EPO's picture

When I started with Drupal most problems were solved by finding the proper module. But after all I stumbled over the fact that there is no button on the node page for copying the complete contents of a node to a new one. This is sometimes usefull, if you have very similar nodes only differing in marginal fields. You don't want to click through tens of attributes only for modifying at last one item. Especially if the node is a little bit complex due to customized fields it would be smart to have a 'copy' function. It was a ten minute job to write a module doing this. But now I have a problem.

Creating a project on this 25 lines of code seems a little bit overstated. On the other hand I'd personally miss this copy button in the node menu badly. And may be a lot of other users too. What to do? Did I miss the proper module when I searched for a node copy function? Is there a project where small but useful modules can be added?

AttachmentSize
node_copy.info_.txt115 bytes
node_copy.module.txt612 bytes

Comments

Node clone

Thats it

EPO's picture

I looked for copy and not clone. So I missed the clue.

Hello EPO, do you know

alexmc's picture

Hello EPO, do you know whether your method works on copying D7 Entities into Nodes? (I will be looking at node_clone too :-)

Drupal 7 Objects

EPO's picture

This suggestion was a qick and dirty approach. You can use this for copying a few nodes from scratch. If you are going to migrate nodes from Drupal 6 to 7 there are a few obstacles to overcome. I provide some code snippets I used for a similar task. So in short it was a Module in Drupal 7 grabbing in a Drupal 6 DB. You can wrap the functions in a batch process if necessary. Observe the new DB syntax in Drupal 7! You'd recommended to use a debugger. Set a breakpoint just before insert and update statements to check what's going on. Have fun.

$entity_id = db_insert('field_data_body')
  ->fields(array(
    'entity_type' => 'node',
    'bundle' => 'book',
    'deleted' => 0,
    'entity_id' => $nid,
    'revision_id' => $vid,
    'language' => 'und',
    'delta' =>  0,
    'body_value' => $body_e,
    'body_summary' => "$mlid_alt-$mlid-$nid_alt", // Übersetzung alt - neu
    'body_format' => 'full_html',
   )
)    
  ->execute()
;


$revision_id = db_insert('field_revision_body')
  ->fields(array(
    'entity_type' => 'node',
    'bundle' => 'book',
    'deleted' => 0,
    'entity_id' => $nid,
    'revision_id' => $vid,
    'language' => 'und',
    'delta' =>  0,
    'body_value' => $body_e,
    'body_summary' => '',
    'body_format' => 'full_html',
   )
)    
  ->execute()
;

$num_updated = db_update('node')
  ->fields(array(
    'vid' => $vid,
  ))
  ->condition('nid', $nid, '=')
  ->execute();

Grabbing in a Drupal 6 DB:

db_set_active('Home'); // Legacy DB wird in der settings.php definiert

  // Old Drupal 6 Style. Dont use. Drupal 7 doesnt like.
  //  $result = db_query("SELECT nid, mlid, bid FROM {book} LIMIT 2");  

// New Drupal 7 Style. Take that.
  $query = db_select('book', 'b');
    $query->fields('b', array('nid', 'mlid', 'bid'));
    //      ->range(0, 2);
    // ->condition('n.type', 'article', '=')
    $result = $query->execute();

db_set_active('default');

foreach ($result as $record) { // the following is an example, you can do what you like with each record
        $op[] = array ('batch_add_book_record',
                       array(array('nid' => $record->nid,
                                   'mlid'=> $record->mlid,
                                   'bid'=> $record->bid,
                                   )
                             )
                       );
}

You have to tune your sites/default/settings.php to make the code above working:

$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'usr_3',
      'username' => 'yyyyyyyyyy',
      'password' => 'xxxxxxxxxxx',
      'host' => 'www.zzz.de',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => 'Drupal7_',
    ),
  ),

  'Home' =>
  array (
    'default' =>
    array (
      'database' => 'drupal_6',
      'username' => 'root',
      'password' => 'xxxxxxxxxxxxxx',
      'host' => '62.143.164.18',     //the temporary IP of my notebook
                                     //Port 3306 (MySql) was forwarded
                                     //in my Router.
     'prefix' => '',
      'driver' => 'mysql',
    ),
  ),

);

Contributed Module Ideas

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: