Copying a node to a new type

Drupal provides powerful tools that makes it easy to do all sorts of changes to your web site, but one change is difficult: changing the content type of a node after you've created it.

In general, once you create an item of a certain type, you cannot change that type without breaking it. But for one project, we needed to do exactly that.

We wanted to split one content type into two, and move the additional video, photo, and text fields over to the new type while keeping certain fields on the old type. And I came up with a quick way of accomplishing this task, thanks to Views Bulk Operations. Here's how.

  1. Create a VBO view and make "Execute arbitrary PHP Scripts" one of the operations.
  2. Select the nodes you want to change in the view.
  3. Use the following snippet to make the change:
unset ($object->nid); // this makes a copy of each node, instead of updating the existing one
$object->type = 'segment'; // Use the machine name of the new type
$object->comment = 0; // While we're at it, turn comments off
node_save($object);

That's it!

The above snippet will save all fields that are in both the old and the new content type, and ignore the data of any field that's not in your new type.

Of course, using PHP is a risky thing to do if you don't know what you're doing -- it's easy to destroy data if you're not careful. This type of operation should only be done after you have a solid backup, and not by novices. If you'd like our assistance, we're happy to help!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <h2> <h3> <h4> <h5> <p> <br> <img>
  • Lines and paragraphs break automatically.
  • Twitter-style @usersnames are linked to their Twitter account pages.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Freelock Blog Posts

Customer Feedback

I had the privilege of working with John and Freelock in launching a new Little League website. The process was flawless and the end product was magnificent exceeding our expectations.

John knows his stuff! He had a wonderful ability to bring the perfect solution to our community based organization. Being volunteer run, we needed some special considerations in the way our website works, John understood this and delivered solutions that were perfect for us.

We now have a cool website that also has the ability to grow with us into the future.

I highly recommend John Locke and Freelock Computing.

Brian Boone
Pacific Little League

About Freelock

We are located in the Fremont neighborhood of Seattle, WA. 3800 Woodland Park Ave. N. Seattle, WA 98103  USA [P] 206.577.0540 Contact Us | Site Map Get Updates ©1995-2011 Freelock Computing