You are here

Drupal 7 - A Simple Content Type

Content types in Drupal are like data classes in that they can be designed to hold all of the information related to one story, article, or other type of content. There are two ways to create custom content types with Drupal 7: the Field UI and by coding up a module. Depending on your personal style and/or need, the Field UI may have everything you need to create the content type of your dreams. I, myself, favor portability and being able to customize every aspect available so that I can leverage Drupal to it's fullest: validation, templating and theming, access restrictions and permissions, and dynamic options lists just to name a few.

Creating a custom content type in D7 really only requires three files: content_type.info, content_type.install, and content_type.module. Each file contains a different type of data required by Drupal to create the content type, store it in the database, and display it to world. Much of this data is integrated into Drupal using a system of hooks which allow modules to hook into function calls being made by the core or other contributed modules. For this example we will create a really simple content type with just a title and body, we will call it: Simple Content Type. The first step is to create the file which will provide Drupal with some basic information about the module: name, description, package name, Drupal core id, dependencies, needed files, and other information. Here is simple_content_type.info:


name = Simple Content Type
description = A simple demonstration content type
package = Andy Pangus Examples
core = 7.x
files[] = simple_content_type.install
files[] = simple_content_type.module

Once Drupal has this information (and the module has been enabled) the next step is to install the module and set up any configurations which will determine how the add/edit forms behave and any tables which may be needed in the database. The uninstall code also lives here. Both fields in our example have locations in the database to store data so our job here is simple: we just need to configure those two fields and then set up the uninstall hook. All nodes have titles by default so really all we need to do is configure the body field. In the uninstall function we will delete all nodes of this type and any associated data from the database. Here is our simple_content_type.install file:


function simple_content_type_install() {
  // create the simple content type
  node_types_rebuild();
  $types = node_type_get_types();
  
  // add the body field to the node type
  node_add_body_field($types['simple_content_type']);
  // load the instance definition for our content type's body
  $body_instance = field_info_instance('node', 'body', 'simple_content_type');
  // configure the body field
  $body_instance['type'] = 'text_summary_or_trimmed';
  $body_instance['label'] = 'Simple Description';
  $body_instance['display'] = array(
    'default' => array(
      'label' => 'above', 
      'type' => 'text_default',
      'weight' => 0,
    ),
    'teaser' => array(
      'label' => 'hidden', 
      'type' => 'text_summary_or_trimmed',
      'weight' => 0,
    ),
  );
  $body_instance['widget']['weight'] = 0;
  
  // save our changes to the body field instance
  field_update_instance($body_instance);
  
  // disable comments for this content type
  variable_set('comment_simple_content_type', COMMENT_NODE_CLOSED);
}

function simple_content_type_uninstall() {
  // gather all the content while the module was enabled
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'simple_content_type'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }
  
  // delete all the nodes at once
  node_delete_multiple($nids);
  
  // delete any remaining field instances attached to this content type
  $instances = field_info_instances('node', 'simple_content_type');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }
  
  // delete our content type
  node_type_delete('simple_content_type');
  
  // purge all field information
  field_purge_batch(1000);
}

With the content type installed the last step is tell Drupal what it needs to know to display, validate, and gate any content of this type. With our simple content type we don't have much to set up so we'll configure some field labels and use node_content_form to create the add/edit form. Here is our simple_content_type.module file:


function simple_content_type_node_info() {
  
  $items['simple_content_type'] = array(
    'name' => t('Simple Content Type'),    // name of your content type
    'type' => 'simple_content_type',
    'base' => 'node_content',
    'description' => t('A simple demonstration content type with a title and body.'),
    'has_title' => '1',
    'title_label' => t('Simple Title'),
    'body_label' => t('Simple Description'),
    'help' => t('Please fill in all fields.'),
  );
  
  return $items;
}

function simple_content_type_form($node, $form_state) {
  return node_content_form($node, $form_state);
}

Save all these files, package them up, and put them in your sites/all/modules folder. Enable your new content type using the Admin interface:

Drupal Admin Interface: Modules

Once the content type has been installed you should now be able to add new nodes of your new type:

Create a new piece of content

A simple test of the Simple Content Type:

Fill out the content type's fields

If all goes well you should see something similar to this:

The submitted piece of content

Of course this is a really basic example and does not incorporate any of the really useful stuff creating a custom content type allows you to do. I'll get to some of that next.

Download the source for this example.

Tags: