Is it possible to add fields to a node type declared using hook_node_info? Do I have to add the fields separately? If so, what hook would I use for that?

1 Answer 1


You need to attach the fields separately, they can't be added through hook_node_info(). You would usually do this in a hook_install() function in your module's .install file.

A nice simple example from Drupal core is in the blog module's install file:

function blog_install() {
  // Ensure the blog node type is available.
  $types = node_type_get_types();

The function simply rebuilds the node types (so the newly added type is available), then adds a body field to it using the node_add_body_field() function. This function in itself gives an excellent example of how to create a field, an instance of that field, and then attach it to a content type using the field_create_field() and field_create_instance() functions.

The code isn't that so long so I'll include it here as an example:

function node_add_body_field($type, $label = 'Body') {
  // Add or remove the body field, as needed.
  $field = field_info_field('body');
  $instance = field_info_instance('node', 'body', $type->type);
  if (empty($field)) {
    $field = array(
      'field_name' => 'body', 
      'type' => 'text_with_summary', 
      'entity_types' => array('node'),
    $field = field_create_field($field);
  if (empty($instance)) {
    $instance = array(
      'field_name' => 'body', 
      'entity_type' => 'node', 
      'bundle' => $type->type, 
      'label' => $label, 
      'widget' => array('type' => 'text_textarea_with_summary'), 
      'settings' => array('display_summary' => TRUE), 
      'display' => array(
        'default' => array(
          'label' => 'hidden', 
          'type' => 'text_default',
        'teaser' => array(
          'label' => 'hidden', 
          'type' => 'text_summary_or_trimmed',
    $instance = field_create_instance($instance);
  return $instance;
  • Thanks. Do I need to do anything in my hook_uninstall?
    – Gaelan
    May 28, 2012 at 20:25
  • It's up to you really...if you want to remove any content/content types/fields you've created then yes, if you don't, then no :)
    – Clive
    May 28, 2012 at 20:30
  • So my content type won't disappear when my module gets turned off?
    – Gaelan
    May 29, 2012 at 1:40
  • 2
    Your custom Content Type will not disappear when your module gets disabled (in Drupal 7), not even if you Uninstall the disabled module. If you write code to delete the Content Types you created during install during the hook_uninstall() of your module, then yes the Content Type will disappear when you Uninstall your module (but still not when you merely disable it). Feb 1, 2013 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.