How to Create Content Type in Drupal 7 Programmatically
Please, take a look
There is a good example in this module, but
The code that will be created new content type we should add into .install file.
Lets add hook_install():
<?php
function your_module_name_install() {
// use get_t() to get the name of our localization function for translation
// during install, when t() is not available.
$t = get_t();
// Define the node type.
$node_example = array(
'type' => 'node_example',
'name' => $t('Example Node'),
'base' => 'node_content',
'description' => $t('This is an example node type with a few fields.'),
'body_label' => $t('Example Description')
);
// Complete the node type definition by setting any defaults not explicitly
// declared above.
// http://api.drupal.org/api/function/node_type_set_defaults/7
$content_type = node_type_set_defaults($node_example);
node_add_body_field($content_type);
// Save the content type
node_type_save($content_type);
?>
We should not add
node_types_rebuild();
menu_rebuild();
in the code of your_module_name_install() because rebuild will perfome automatically.
According to the rules of good Drupal code, we should to make drupal message and write this event to the log:
<?php
function your_module_name_install() {
$t = get_t();
$node_example = array(
'type' => 'node_example',
'name' => $t('Example Node'),
'base' => 'node_content',
'description' => $t('This is an example node type with a few fields.'),
'body_label' => $t('Example Description')
);
$content_type = node_type_set_defaults($node_example);
node_add_body_field($content_type);
// Check if we create content type or update.
$status = node_type_save($content_type);
// Replacement rule for the messages.
$t_args = array('%name' => $content_type->name);
if ($status == SAVED_UPDATED) { // update case
drupal_set_message($t('The content type %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) { // create case
drupal_set_message($t('The content type %name has been added.', $t_args));
watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l($t('view'), 'admin/structure/types'));
}
?>
Ok, and we also should provide hook_uninstall() to remove our content type:
<?php
function your_module_name_uninstall() {
// Gather all the example content that might have been created while this
// module was enabled. Simple selects still use db_query().
// http://api.drupal.org/api/function/db_query/7
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => 'node_example'));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
// Delete all the nodes at once
// http://api.drupal.org/api/function/node_delete_multiple/7
node_delete_multiple($nids);
// Delete our content type
// http://api.drupal.org/api/function/node_type_delete/7
node_type_delete('node_example');
?>
I should mention that if we will not delete all nodes of such content type the function node_type_delete() will not run.
See also How to transfer the content type settings by Features.