node_example.module

You are here

  1. 7 node_example/node_example.module
  2. 6 node_example/node_example.module

This is an example outlining how a module can be used to define a new node type.

(Note that custom node types are most often created with CCK in recent versions of Drupal.)

Our example node type will allow users to specify a "color" and a "quantity" for their nodes; some kind of rudimentary inventory-tracking system, perhaps? To store this extra information, we need an auxiliary database table as defined in node_example_schema().

Functions

Namesort descending Description
node_example_access Implementation of hook_access().
node_example_delete Implementation of hook_delete().
node_example_form Implementation of hook_form().
node_example_info Explain how the module demonstrates a new node type.node_example.test
node_example_insert Implementation of hook_insert().
node_example_load Implementation of hook_load().
node_example_menu Implementation of hook_menu().
node_example_nodeapi Implementation of hook_nodeapi().
node_example_node_info Implementation of hook_node_info().
node_example_perm Implementation of hook_perm().
node_example_theme Implementation of hook_theme().
node_example_update Implementation of hook_update().
node_example_validate Implementation of hook_validate().
node_example_view Implementation of hook_view().
theme_node_example_order_info A custom theme function.

File

node_example/node_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is an example outlining how a module can be used to define a new
  5. * node type.
  6. *
  7. * (Note that custom node types are most often created with CCK in recent
  8. * versions of Drupal.)
  9. *
  10. * Our example node type will allow users to specify a "color" and a "quantity"
  11. * for their nodes; some kind of rudimentary inventory-tracking system, perhaps?
  12. * To store this extra information, we need an auxiliary database table as
  13. * defined in node_example_schema().
  14. */
  15. /**
  16. * @defgroup node_example Example: Node
  17. * @ingroup examples
  18. * @{
  19. * Creating a new content type in a module. (drupal 6)
  20. *
  21. * This is an example outlining how a module can be used to define a new
  22. * node type.
  23. *
  24. * (Note that custom node types are most often created with CCK in recent
  25. * versions of Drupal.)
  26. *
  27. * Our example node type will allow users to specify a "color" and a "quantity"
  28. * for their nodes; some kind of rudimentary inventory-tracking system, perhaps?
  29. * To store this extra information, we need an auxiliary database table as
  30. * defined in node_example_schema().
  31. *
  32. * This example is part of the Examples for Developers Project which you can download
  33. * and experiment with here: http://drupal.org/project/examples
  34. */
  35. /**
  36. * Implementation of hook_menu().
  37. */
  38. function node_example_menu() {
  39. $items['examples/node_example'] = array(
  40. 'title' => 'Node Example',
  41. 'page callback' => 'node_example_info',
  42. 'access callback' => TRUE,
  43. );
  44. return $items;
  45. }
  46. /**
  47. * Explain how the module demonstrates a new node type.node_example.test
  48. */
  49. function node_example_info() {
  50. return t('The node example defines a new node type, "Example node type 1", which can be created at !link.', array('!link' => l(t('node/add/example-node-type-1'), 'node/add/example-node-type-1')));
  51. }
  52. /**
  53. * Implementation of hook_node_info().
  54. *
  55. * This is a required node hook. This function describes the nodes provided by
  56. * this module.
  57. *
  58. * The required attributes are:
  59. * - "name" provides a human readable name for the node,
  60. * - "module" tells Drupal how the module's functions map to hooks (i.e. if the
  61. * module is node_example_foo then node_example_foo_insert will be called
  62. * when inserting the node).
  63. * - "description" provides a brief description of the node type, which is
  64. * shown when a user accesses the "Create content" page for that node type.
  65. *
  66. * The other optional, attributes:
  67. * - "has_title" boolean that indicates whether or not this node type has a
  68. * title field.
  69. * - "title_label": the label for the title field of this content type.
  70. * - "has_body": boolean that indicates whether or not this node type has a
  71. * body field.
  72. * - "body_label": the label for the body field of this content type.
  73. * - "min_word_count": the minimum number of words for the body field to be
  74. * considered valid for this content type.
  75. *
  76. * The key in this example, "example_node_type_1", is the "machine name" of the
  77. * node type and is stored in {node}.type. The node's type value cannot be
  78. * changed through the admin interface.
  79. *
  80. */
  81. function node_example_node_info() {
  82. return array(
  83. 'example_node_type_1' => array(
  84. 'name' => t('Example node type 1'),
  85. 'module' => 'node_example',
  86. 'description' => t("An example node type with a few fields."),
  87. 'has_title' => TRUE,
  88. 'title_label' => t('Example Node Type 1 Title'),
  89. 'has_body' => TRUE,
  90. 'body_label' => t('Example Node Type 1 Body'),
  91. )
  92. );
  93. }
  94. /**
  95. * Implementation of hook_access().
  96. *
  97. * Node modules may implement node_access() to determine the operations
  98. * users may perform on nodes. This example uses a very common access pattern.
  99. */
  100. function node_example_access($op, $node, $account) {
  101. if ($op == 'create') {
  102. return user_access('create example content', $account);
  103. }
  104. if ($op == 'update') {
  105. if (user_access('edit any example content', $account) || (user_access('edit own example content', $account) && ($account->uid == $node->uid))) {
  106. return TRUE;
  107. }
  108. }
  109. if ($op == 'delete') {
  110. if (user_access('delete any example content', $account) || (user_access('delete own example content', $account) && ($account->uid == $node->uid))) {
  111. return TRUE;
  112. }
  113. }
  114. }
  115. /**
  116. * Implementation of hook_perm().
  117. *
  118. * Since we are limiting the ability to create new nodes to certain users,
  119. * we need to define what those permissions are here. We also define a permission
  120. * to allow users to edit the nodes they created.
  121. */
  122. function node_example_perm() {
  123. return array(
  124. 'create example content',
  125. 'delete own example content',
  126. 'delete any example content',
  127. 'edit own example content',
  128. 'edit any example content',
  129. );
  130. }
  131. /**
  132. * Implementation of hook_form().
  133. *
  134. * Now it's time to describe the form for collecting the information
  135. * specific to this node type. This hook requires us to return an array with
  136. * a sub array containing information for each element in the form.
  137. */
  138. function node_example_form(&$node, $form_state) {
  139. // The site admin can decide if this node type has a title and body, and how
  140. // the fields should be labeled. We need to load these settings so we can
  141. // build the node form correctly.
  142. $type = node_get_types('type', $node);
  143. if ($type->has_title) {
  144. $form['title'] = array(
  145. '#type' => 'textfield',
  146. '#title' => check_plain($type->title_label),
  147. '#required' => TRUE,
  148. '#default_value' => $node->title,
  149. '#weight' => -5
  150. );
  151. }
  152. if ($type->has_body) {
  153. // In Drupal 6, we use node_body_field() to get the body and filter
  154. // elements. This replaces the old textarea + filter_form() method of
  155. // setting this up. It will also ensure the teaser splitter gets set up
  156. // properly.
  157. $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  158. }
  159. // Now we define the form elements specific to our node type.
  160. $form['color'] = array(
  161. '#type' => 'textfield',
  162. '#title' => t('Color'),
  163. '#default_value' => isset($node->color) ? $node->color : '',
  164. );
  165. $form['quantity'] = array(
  166. '#type' => 'textfield',
  167. '#title' => t('Quantity'),
  168. '#default_value' => isset($node->quantity) ? $node->quantity : 0,
  169. '#size' => 10,
  170. '#maxlength' => 10
  171. );
  172. return $form;
  173. }
  174. /**
  175. * Implementation of hook_validate().
  176. *
  177. * Our "quantity" field requires a number to be entered. This hook lets
  178. * us ensure that the user entered an appropriate value before we try
  179. * inserting anything into the database.
  180. *
  181. * Errors should be signaled with form_set_error().
  182. */
  183. function node_example_validate($node, &$form) {
  184. if ($node->quantity) {
  185. if (!is_numeric($node->quantity)) {
  186. form_set_error('quantity', t('The quantity must be a number.'));
  187. }
  188. }
  189. }
  190. /**
  191. * Implementation of hook_insert().
  192. *
  193. * As a new node is being inserted into the database, we need to do our own
  194. * database inserts.
  195. */
  196. function node_example_insert($node) {
  197. db_query("INSERT INTO {node_example} (vid, nid, color, quantity) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $node->color, $node->quantity);
  198. }
  199. /**
  200. * Implementation of hook_update().
  201. *
  202. * As an existing node is being updated in the database, we need to do our own
  203. * database updates.
  204. */
  205. function node_example_update($node) {
  206. // if this is a new node or we're adding a new revision,
  207. if ($node->revision) {
  208. node_example_insert($node);
  209. }
  210. else {
  211. db_query("UPDATE {node_example} SET color = '%s', quantity = %d WHERE vid = %d", $node->color, $node->quantity, $node->vid);
  212. }
  213. }
  214. /**
  215. * Implementation of hook_nodeapi().
  216. *
  217. * When a node revision is deleted, we need to remove the corresponding record
  218. * from our table. The only way to handle revision deletion is by implementing
  219. * hook_nodeapi().
  220. */
  221. function node_example_nodeapi(&$node, $op, $teaser, $page) {
  222. switch ($op) {
  223. case 'delete revision':
  224. // Notice that we're matching a single revision based on the node's vid.
  225. db_query('DELETE FROM {node_example} WHERE vid = %d', $node->vid);
  226. break;
  227. }
  228. }
  229. /**
  230. * Implementation of hook_delete().
  231. *
  232. * When a node is deleted, we need to remove all related records from our table.
  233. */
  234. function node_example_delete($node) {
  235. // Notice that we're matching all revision, by using the node's nid.
  236. db_query('DELETE FROM {node_example} WHERE nid = %d', $node->nid);
  237. }
  238. /**
  239. * Implementation of hook_load().
  240. *
  241. * Now that we've defined how to manage the node data in the database, we
  242. * need to tell Drupal how to get the node back out. This hook is called
  243. * every time a node is loaded, and allows us to do some loading of our own.
  244. */
  245. function node_example_load($node) {
  246. $additions = db_fetch_object(db_query('SELECT color, quantity FROM {node_example} WHERE vid = %d', $node->vid));
  247. return $additions;
  248. }
  249. /**
  250. * Implementation of hook_view().
  251. *
  252. * This is a typical implementation that simply runs the node text through
  253. * the output filters.
  254. */
  255. function node_example_view($node, $teaser = FALSE, $page = FALSE) {
  256. $node = node_prepare($node, $teaser);
  257. $node->content['myfield'] = array(
  258. '#value' => theme('node_example_order_info', $node),
  259. '#weight' => 1,
  260. );
  261. return $node;
  262. }
  263. /**
  264. * Implementation of hook_theme().
  265. *
  266. * This lets us tell Drupal about our theme functions and their arguments.
  267. */
  268. function node_example_theme() {
  269. return array(
  270. 'node_example_order_info' => array(
  271. 'arguments' => array('node'),
  272. ),
  273. );
  274. }
  275. /**
  276. * A custom theme function.
  277. *
  278. * By using this function to format our node-specific information, themes
  279. * can override this presentation if they wish. We also wrap the default
  280. * presentation in a CSS class that is prefixed by the module name. This
  281. * way, style sheets can modify the output without requiring theme code.
  282. */
  283. function theme_node_example_order_info($node) {
  284. $output = '<div class="node_example_order_info">';
  285. $output .= t('The order is for %quantity %color items.', array('%quantity' => check_plain($node->quantity), '%color' => check_plain($node->color)));
  286. $output .= '</div>';
  287. return $output;
  288. }
  289. /**
  290. * @} End of "defgroup node_example".
  291. */