How to add a new content type in code

Submitted by Dan Grant on Sat, 01/07/2017 - 01:58

The following is the minimum code required to add a new content type to Drupal 7 in a custom module (replace "new_content_type" with your module name...).

new_content_type.info:

name = New Content Type
description = A simple custom module to add a new content type
package = xq42
core = 7.x

new_content_type.module:

<?php

/**
 * @file
 * Defines a new, custom content type in code.
 */

new_content_type.install:

<?php

/**
 * @file
 * Install file for the "New Content Type" module.
 */

/**
 * Implements hook_node_info().
 */
function new_content_type_node_info() {
  $items = array(
    'new_content_type' => array(
      'name' => t('New Content Type'),
      // The following lines are not required, but can be added to control more defaults.
      'description' => t('Text that will appear in the Description field.'),
      'base' => 'node_content',
      // It is possible to create a content type without a Title field.
      // Set to 0 to make this happen.
      'has_title' => '1',
      // Set the default title field label (i.e. change it from "Title" to something else...)
      'title_label' => t('New Title Label'),
      'help' => t('Text to appear in the Explanation or submission guidelines field'),
      // End of optional elements.
    ),
  );

  return $items;
}

That's it!

Drupal will assume this is a new Content type bundle called "New Content Type", and will create it with all the default content type settings.

blog_tags