Home ›
How to programmatically rename a content type (change node type machine name)How to programmatically rename a content type (change node type machine name)
Submitted by Benjamin Melançon on August 30, 2014 - 6:06pm
Set old_type if you want to programmatically update a node type / machine name.
(This is not about changing the type of a node, but changing what type is called on the machine-info level.)
$type->old_type is what node_type_save() checks for when updating a node type, if it's not set it will create a new node type even if orig_type is set to an existing bundle. So really all you need to do right before saving a modified content type is set the old_type to the orig_type, such as:
<?php
$type = node_type_load('example');
$type->type = 'new_machine_name';
$type->old_type = $type->orig_type; // This will be 'example'.
node_type_save($type); // This will now result in SAVED_UPDATED (2)
?>
As noted when documenting this at the node_type_save API page, why orig_type
is documented but old_type
is checked for determining if we are renaming a node type, i have no idea and would greatly appreciate enlightenment.
Searched words:
drupal programmatic rename content type
drupal programmatic change machine name of content type drupal
drupal 7 update node type machine-readable name
Comments
Post new comment