Example: Node

Same name in other branches
  1. 6.x-1.x node_example/node_example.module \node_example

Example defining a node type in code.

This is an example outlining how a module can be used to define a new node type. Our example node type will allow users to specify multiple "colors", a "quantity" and an "image" for their nodes; some kind of rudimentary inventory-tracking system, perhaps?

The basic pattern for defining a node type is to tell Drupal about the node's fields and view modes. Drupal will then take over and manage the storage for this node type. This differs from Drupal 6, where we would have to handle all the database storage ourselves in the module.

Remember that most node types do not require any custom code, as one simply creates them using the Drupal user interface. Creating a node like this in code is a special case.

At absolute minimum, in order to provide a content type for node, you have to implement hook_node_info() and hook_form(). Node can take care of the rest, if you want it to.

First and foremost, defining a node type happens in hook_node_info(). Our implementation of this hook gives Drupal an array of information about the content type we want to create.

Next, since we want to add fields to our content type, we implement hook_node_type_insert(), which gives us a chance to modify recently-created content types.

Drupal is able to handle deletion of our content, including dependencies based on re-use of our field instances, so we don't have to manage any of it.

In previous versions of Drupal, "teaser" and "page" were node view modes. In Drupal 7 we can define custom view modes to let the node know how it should return it's data. This module declares a custom view mode called "example_node_list".

Consult the Field API Tutorial and Field API Handbook Page and Field API documentation.

See also

field_example.module

Parent topics

File

node_example/node_example.module, line 10

Functions

Title Sort descending File name Summary
node_example_entity_info_alter node_example/node_example.module Implements hook_entity_info_alter().
node_example_field_formatter_info node_example/node_example.module Implements hook_field_formatter_info().
node_example_field_formatter_view node_example/node_example.module Implements hook_field_formatter_view().
node_example_form node_example/node_example.module Implements hook_form().
node_example_help node_example/node_example.module Implements hook_help().
node_example_menu node_example/node_example.module Implements hook_menu().
node_example_node_info node_example/node_example.module Implements hook_node_info().
node_example_node_type_insert node_example/node_example.module Implements hook_node_type_insert().
node_example_page node_example/node_example.module Callback that builds our content and returns it to the browser.
node_example_theme node_example/node_example.module Implements hook_theme().
theme_example_node_color node_example/node_example.module A custom theme function.
_node_example_installed_fields node_example/node_example.module Define the fields for our content type.
_node_example_installed_instances node_example/node_example.module Define the field instances for our content type.

Classes

Title Sort descending File name Summary
NodeExampleTestCase node_example/node_example.test Functionality tests for node example module.