How to Create Content Type in Drupal 7 Programmatically

Please, take a look on node_example module (http://drupal.org/project/examples)
There is a good example in this module, but I'm try to stop only on simple content type adding without any fields as extensions.

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?

Comments

“But I must appreciate the woman! ” <a href="http://www.polooutletstores.org/">polo outlet online</a>, shopping crestfallen, explained to this waitress. He / she produced some sort of black color gun in addition to a sheet of report at a zippered fast snakeskin bag, which often functioned such as a briefcase, in addition to easily sketched some sort of portrait on the person. The item bundled some sort of globule ring in addition to the woman Chanel case, this quilting caused to become seeing that crosshatching as well as a brand connected with interlocking “C’s” dangled on the area. <a href="http://www.polooutletstores.org/">polo outlet store</a> He / she likewise came some sort of caricature connected with him or her self, that has a massive sale paper anime scalp framed by means of a pair of glasses in addition to a oversize bow wrap. That he / she eventually left intended for Ester in reference to his gratitude. This waitress, definitely not discerning Mr. Elbaz, checked the item almost like the item were being some sort of child’s illustrating, in addition to said within a compartment. The true secret to help realizing this achievements in addition to endurance connected with Mr. Elbaz, an excellent gentleman exactly who wouldn't finish off past, <a href="http://www.polooutletstores.org/">polo factory store</a> comes down to this particular: “I never ever imagine persons ought to do factors in my opinion, ” he / she claimed while in a meet with for the bistro a couple weeks ago. “I imagine I should complete factors intended for some others. Which enables everyone practical. ”<a href="http://www.polooutletstores.org/">http://www.polooutletstores.org/</a>
http://www.polooutletstores.org/.

Sorry but,
http://vimax4pills.com/ ,penis enlargement, http://www.phen375-4diet.com ,buy phen375,

asqkzdw http://paydayloanslkm.com/ payday loans 6241 http://autoinsuranceqx.com/ cheap car insurance >:-[ http://paydayloanslkm.ca/ pay day loans >:]] http://paydayloanslkm.co.uk/ payday Advance uk fNgWYT

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Image CAPTCHA
Enter the characters shown in the image.