Changing the type of a Drupal node

Printer-friendly versionPrinter-friendly versionSend to friendSend to friend

Not for the faint hearted this, you need a very good understanding of Drupal, Node objects and CCK in order to do this successfully. I thought I would jot down the steps I carried out in order to change my blog nodes from one type to another.

Why would I want to do this? Well... Drupal's blogging system is not too hot (IMHO) and on most other sites I have built, I have created a new content type of say, "Blog Post" and powered the rest with views. There is no real need to I guess but Drupal's blogging system is set up to cater for multiple users having blogs, and for a personal blog / portfolio site such as this one there is not really much call for that kind of functionality. For example Drupal's blogging mechanism automatically adds links to 's blog to the top of each post, doesn't use views by default to display posts etc. The only reason my blog was set up this way is because I built it in a hurry, now I have a little more time I thought it was time to do something about it.

Disclaimer!

This method will only work for content types built with CCK. Any custom content type installed by other modules can not be changed with this method. Also back up your database before attempting this. I cannot emphasize that enought, but you are a responsible dev right? Don't come crying to me if you wipe out your content and have no plan B.

Also, this was done on a Drupal 6 site... not sure if the Content Copy is available for 5 and if it is, it may differ slightly. Any feedback on a Drupal 5 method is most welcome.

Now for the science bit...

Basically this is as simple as opening up the database in your favorite SQL application (mine is the awesome Sequel Pro on the Mac) and running:

UPDATE node SET type = "new type" WHERE type = "old type"

Now, if the content types you are merging / switching just have the default fields then that is really all you need to do, no node IDs change therefore everything should be fine.

If you have slightly more complex content types then you will need to do a few extra bits.

First up, create your new content type by going to admin/content/types and clicking "Add new content type". Fill in the form as usual, do not add any more fields as yet and save the content type.

Next, enable Content Copy module that ships with CCK. This gives you an import / export mechanism for content type definitions, and we will need this to export our old content type. Please note that this method only works with CCK content types - any custom content types that are installed as part of a module (such as the Question nodes in the FAQ module) will not work with this method.

So, with the Content Copy module enabled go back to admin/content/types and click the "Export" tab from the tabs at the top of the page. From the form, select the content type you want to export then hit "Export".

Copy the resulting code into a text document for safe keeping and go back to admin/content/types (again) but this time click "Import" from the tabs. You will see a new form with 2 fields, the first should be a select list (drop down) that defaults to , if we left this as it is then Drupal would try to create a new content type from our exported code - this would fail as our old content type still exists, so select the content type you created just now from this list then copy the code out of the text editor and paste it into the large textarea below the select list. Once you have done that, click import.

If all went to plan you should see that the fields you exported from your old content type have been added to the new one. We are now ready to hit the database. Basically all you need to do is run the SQL query at the top of this post and all should be well.

Clearing up

There are a couple of things that are worth checking once you have done this:

  1. If your site uses Views (lets face it, most Drupal builds do!) you will need to check over any views that relied on the old content type as filters, fields etc. You should go and change all of these instances to the new content type and clear the views cache.
  2. Check your taxonomys! Vocabularies are associated with content types so you will need to go and check any vocabs that were associated with the old content type are now associated with the new one. You can do this via the admin interface, no db work required here!
  3. Check your path alias settings. You will no doubt have set up automated path alias settings for the old content type. Make sure your new content type has the same rules. Your exisiting nodes will be fine, this just makes sure that any future nodes will behave in the same way.
  4. Clear all your caches using drush, or visit admin/settings/performance and clear them from there.
  5. Hope that helps someone out there, this is not a definitive solution by any means, just one that worked for me.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • You may embed videos from the following providers . Just add the video URL to your textarea in the place where you would like the video to appear, i.e. http://www.youtube.com/watch?v=pw0jmvdh.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].
  • You may use <swf file="song.mp3"> to display Flash files inline
  • Twitter-style @usersnames are linked to their Twitter account pages.
  • Twitter-style #hashtags are linked to search.twitter.com.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.