How to change a node's type?
Submitted by Drupalace
15
Dec
2007
Question Status:
Question details:
Hide your eyes, conservatives: it's a node-change operation, the shocking switch from Blog Entry to Page, or Story to Review, whatever your desires demand. Call it a miracle or call it a travesty, modern Drupal science makes it possible... and seductively easy.

Comments
Re: How to change a node's type?
I was curious if this could also be done by poking the database directly to change, say, nodes of type "blog" into nodes of type "question" (a custom content type)?
Oh, the danger.
Charlie
Re: How to change a node's type?
Ah, the hubris of a Frankenstein! Actually, I would think it's quite possible to manhandle the database directly, as you suggest. In fact, what else could the nodetype module be doing, but performing some database surgery that one could also perform manually?
I don't know what the SQL commands would be; I expect that dissecting the nodetype module would yield clues. I've got too much on my to-do list, so for now will stick to letting nodetype do the heavy gravedigging for me. But who knows, maybe some night when the moon is full and the Van de Graaf spheres are crackling, it'd be illuminating to strap down a node and take scalpel in hand...
Re: How to change a node's type?
wow, thanks for posting this. i wanted to do it, but searching google didnt bring up very good results.
you rocked it tho
Re: How to change a node's type?
Glad it helped! Thanks, and drop by any time.
Re: How to change a node's type?
I just had a first look at this module. It seems that its only task is to change the type. And because the module is doing that not directly by modifying the database but using ordinary drupal functions the cache will be updated probably.
As far as I can see the module (implicitly) does:
update node set type='NEWTYPE' where nid=NODEID;
(where NEWTYPE is one of the values in table node_type column type: select type from node_type; )
And the node should also be moved from table content_type_ to content_type_
Now some thoughts about what can be done to extend the nodetype module
When you have an node (NID) of type 'image', there is always one image attached to it.
Name and path of this image is stored in table 'files'. When you have versioning activated (VID) a query to pick up the image will look like:
select f.fid, n.nid, filename, filepath from node n, file_revisions r, files f where n.vid=r.vid and r.fid=f.fid and n.nid=NID;
You can see that the image module has created several additional derivative images with names (column filename) as
thumbnail
preview
If you want to change the node type from 'image' to 'story' all the derivative images (all filenames but '_original') can be purged.
The '_original' image can be attached to the story if there is e.g. a CCK field of type image. To check this try
select type_name, widget_type from node_field_instance where type_name='story' and widget_type='image';
Column type_name will show (all) CCK field name of type 'image' attached to node of type 'story'. It looks like
'field_attach_image'
where attach_image is the name for the field used when the CCK field was set up.
Prepending this type_name with 'content_' yields to
'content_field_attach_image'
which is the name of the table to put the image in:
insert into content_field_attach_image values (VID, delta?, NID, FID, '', '');
I hope this helps somebody
Regards
Schildi
Re: How to change a node's type?
I can't help but think it'll help somebody! I pitch this page on the drupal.org forums whenever someone asks about changing a node type, so I'm certain future seekers of transformational knowledge will see your post and learn. Thank you, Schildi!
Stick with the Drupal API, avoid the DB as much as possible.
I have been doing Drupal module and other code level development for a few years now and I personally have a rule that I stick with using the Drupal API to accomplish as much as possible.
There are too many unknowns when working on parts of the db that are controlled by Drupal Core or any of the contrib modules. So unless you wrote the tables yourself I suggest you don't touch them directly.
Using the API allows other tables that need to be updated to be updated automatically. This can be cache, reference or join tables, contrib tables, etc.
The one concern I have with updating the type has to do with contrib modules that hook into the nodeapi hooks and make changes based on a node's content type. Though if you created the content types yourself then you probably have very little to worry about here.
Don't muck with DB
Good point. I should have been clear in saying that while it's certainly possible to directly dabble in the database, it's typically not a good idea, for reasons you state. Even if "safe" changes create no problems for the moment, they could cause trouble for future updates to Drupal or modules.
Thanks for the good advice!
Re: How to change a node's type?
the nodetype.module comes quite handy thanks to the developer. it would be perfect though, if its part of the content bulk actions + a feature for cck per field operation.
Nodelist enhancement?
Seems to me info can be lost where the two node types are different in more than name.
I think it would be useful if one could see a comparison of the fields of the old and new node types. I came here because I misconfigured freelinking, and had the default new document type set to "story" where I intended "wiki." I don't think the differences are great, but they might be significant. And, from this description, nodelink will do its thing and all might seem good when, in face, not all is good.
Node type change and risk
Indeed, letting an automated procedure change a node's type does sound like a risk of data loss, especially if the two types' fields don't match. If the source type has more fields than the target type, do those extra fields simply get stripped off and their data lost forever? I don't know!
Your suggestion is a good one: It'd be great if the module displayed a pre-change list of what changes it will make. As it is, the workings are a black box. So for now, all I can suggest to users of Nodetype is: Make sure the target node type has all the fields you want to keep from the original type (and that those fields share the same field names); test the procedure on a dummy node or two before hitting the real nodes; and, ABSOLUTELY back up that database before letting Nodetype touch a thing!
With those precautions taken, I've found it a nice tool.
Thanks for the comment!
Great article. Thanks a lot.
Great article. Thanks a lot. It is great that there are such opportunities to switch from Blog Entry to Page. And it is relatively easy. I like Drupal.
Changing node type
I agree, the ability to make big changes like this down the road are a great relief. One of the constant worries of newbies is making some decision that'll later prove to be both wrong and hard to undo...
Re: How to change a node's type?
Thanks it helped me in solving my problem..
hotfile
Change node type
Here's another module that changes node types, including extra fields.
http://drupal.org/project/node_convert
Node Convert, not Nodetype?
Hey, thanks for the info! I hadn't heard of the newer Node Convert. Looking at the two modules' project pages now, it appears that your recommendation is the better of the two, by the Nodetype page's own admission. Nodetype only changes the node type registered in the database, without any additional exertion; Node Convert does more work to convert individual fields. And as a bonus, Node Convert also has a D7 version.
Disclaimer: I haven't yet tried Node Convert, but next time I have a conversion task to perform, I'll give it a shot. Thanks again!
I know what your talking
I know what your talking about with those Adam's apples of the "girls" in Phuket. I'm currently here www.thailandcamp.com in Phuket and am very selective about the company I keep.
Add new comment