Changing the type of a Drupal node
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
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 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:
- 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.
- 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!
- 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.
- Clear all your caches using drush, or visit admin/settings/performance and clear them from there.
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