Example: Node in Examples for Developers 7
Same name and namespace in other branches
- 6 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
Parent topics
File
- node_example/
node_example.module, line 10 - Module file for Node Example module.
Functions
Classes
Name | Location | Description |
---|---|---|
NodeExampleTestCase |
node_example/ |
Functionality tests for node example module. |