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 Backdrop about the node's fields and view modes. Backdrop will then take over and manage the storage for this node type.

Remember that most node types do not require any custom code, as one simply creates them using the Backdrop 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 Backdrop 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.

Backdrop 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.

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

See also

field_example.module

Parent topics

File

modules/examples/node_example/node_example.module, line 10
Module file for Node Example module.

Functions

Namesort ascending Location Description
_node_example_installed_instances modules/examples/node_example/node_example.module Define the field instances for our content type.
_node_example_installed_fields modules/examples/node_example/node_example.module Define the fields for our content type.
theme_example_node_color modules/examples/node_example/node_example.module A custom theme function.
node_example_theme modules/examples/node_example/node_example.module Implements hook_theme().
node_example_page modules/examples/node_example/node_example.module Callback that builds our content and returns it to the browser.
node_example_node_type_insert modules/examples/node_example/node_example.module Implements hook_node_type_insert().
node_example_menu modules/examples/node_example/node_example.module Implements hook_menu().
node_example_form modules/examples/node_example/node_example.module Implements hook_form().
node_example_field_formatter_view modules/examples/node_example/node_example.module Implements hook_field_formatter_view().
node_example_field_formatter_info modules/examples/node_example/node_example.module Implements hook_field_formatter_info().
node_example_entity_info_alter modules/examples/node_example/node_example.module Implements hook_entity_info_alter().

Classes

Namesort ascending Location Description
NodeExampleTestCase modules/examples/node_example/node_example.test Functionality tests for node example module.