nodeapi_example.module

  1. examples
    1. 6 nodeapi_example/nodeapi_example.module
    2. 7 nodeapi_example/nodeapi_example.module
    3. 8 nodeapi_example/nodeapi_example.module
  2. drupal
    1. 4.6 developer/examples/nodeapi_example.module
    2. 4.7 developer/examples/nodeapi_example.module
    3. 5 developer/examples/nodeapi_example.module

This is an example outlining how a module can be used to extend existing content types.

We will add the ability for each node to have a "rating," which will be a number from one to five.

Functions & methods

NameDescription
nodeapi_example_form_alterImplementation of hook_form_alter().
nodeapi_example_nodeapiImplementation of hook_nodeapi().
theme_nodeapi_example_ratingA custom theme function.

File

developer/examples/nodeapi_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is an example outlining how a module can be used to extend existing
  5. * content types.
  6. *
  7. * We will add the ability for each node to have a "rating," which will be a
  8. * number from one to five.
  9. */
  10. /**
  11. * Implementation of hook_form_alter().
  12. *
  13. * By implementing this hook, we're able to modify any form. We'll only make
  14. * changes to two types: a node's content type configuration and edit forms.
  15. */
  16. function nodeapi_example_form_alter($form_id, &$form) {
  17. // We're only modifying node forms, if the type field isn't set we don't need
  18. // to bother; otherwise, store it for later retrieval.
  19. if (isset($form['type'])) {
  20. $type = $form['type']['#value'];
  21. }
  22. elseif (isset($form['orig_type'])) {
  23. $type = $form['orig_type']['#value'];
  24. }
  25. else {
  26. return;
  27. }
  28. // The rating is enabled on a per node type basis. The variable used to store
  29. // this information is named named using both the name of this module, to
  30. // avoid namespace conflicts, and the node type, because we support multiple node
  31. // types.
  32. $enabled = variable_get('nodeapi_example_'. $type, 0);
  33. switch ($form_id) {
  34. // We need to have a way for administrators to indicate which content
  35. // types should have our rating field added. This is done by inserting a
  36. // checkbox in the node's content type configuration page. The variable will
  37. // be automatically saved by the settings form.
  38. case 'node_type_form':
  39. $form['workflow']['nodeapi_example_'. $type] = array(
  40. '#type' => 'radios',
  41. '#title' => t('NodeAPI Example Rating'),
  42. '#default_value' => $enabled,
  43. '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
  44. '#description' => t('Should this node have a rating attached to it?'),
  45. );
  46. break;
  47. case $type .'_node_form':
  48. // If the rating is enabled for this node type, we insert our control
  49. // into the form.
  50. if ($enabled) {
  51. $options = array(0 => t('Unrated'), 1, 2, 3, 4, 5);
  52. $form['nodeapi_example_rating'] = array(
  53. '#type' => 'select',
  54. '#title' => t('Rating'),
  55. '#default_value' => $form['#node']->nodeapi_example_rating,
  56. '#options' => $options,
  57. '#required' => TRUE,
  58. '#weight' => 0,
  59. );
  60. }
  61. break;
  62. }
  63. }
  64. /**
  65. * Implementation of hook_nodeapi().
  66. *
  67. * We will implement several node API operations here. This hook allows us to
  68. * act on all major node operations, so we can manage our additional data
  69. * appropriately.
  70. */
  71. function nodeapi_example_nodeapi(&$node, $op, $teaser, $page) {
  72. switch ($op) {
  73. // When the content editing form is submitted, we need to validate the input
  74. // to make sure the user made a selection, since we are requiring the rating
  75. // field. We have to check that the value has been set to avoid showing an
  76. // error message when a new blank form is presented. Calling form_set_error()
  77. // when the field is set but zero ensures not only that an error message is
  78. // presented, but also that the user must correct the error before being able
  79. // to submit the node.
  80. case 'validate':
  81. if (variable_get('nodeapi_example_'. $node->type, TRUE)) {
  82. if (isset($node->nodeapi_example_rating) && !$node->nodeapi_example_rating) {
  83. form_set_error('nodeapi_example_rating', t('You must rate this content.'));
  84. }
  85. }
  86. break;
  87. // Now we need to take care of loading one of the extended nodes from the
  88. // database. An array containing our extra field needs to be returned.
  89. case 'load':
  90. $object = db_fetch_object(db_query('SELECT rating FROM {nodeapi_example} WHERE nid = %d', $node->nid));
  91. return array('nodeapi_example_rating' => $object->rating);
  92. break;
  93. // Insert is called after the node has been validated and saved to the
  94. // database. It gives us a chance to create our own record in the database.
  95. case 'insert':
  96. db_query('INSERT INTO {nodeapi_example} (nid, rating) VALUES (%d, %d)', $node->nid, $node->nodeapi_example_rating);
  97. break;
  98. // Update is called when an existing node has been changed. Here, we use a
  99. // DELETE then an INSERT rather than an UPDATE. The reason is that a node
  100. // created before this module was installed won't already have a rating
  101. // saved so there would be nothing to update.
  102. case 'update':
  103. db_query('DELETE FROM {nodeapi_example} WHERE nid = %d', $node->nid);
  104. db_query('INSERT INTO {nodeapi_example} (nid, rating) VALUES (%d, %d)', $node->nid, $node->nodeapi_example_rating);
  105. break;
  106. // Delete is called whn the node is being deleted, it gives us a chance
  107. // to delete the rating too.
  108. case 'delete':
  109. db_query('DELETE FROM {nodeapi_example} WHERE nid = %d', $node->nid);
  110. break;
  111. // Finally, we need to take care of displaying our rating when the node is
  112. // viewed. This operation is called after the node has already been prepared
  113. // into HTML and filtered as necessary, so we know we are dealing with an
  114. // HTML teaser and body. We will inject our additional information at the front
  115. // of the node copy.
  116. //
  117. // Using nodeapi('view') is more appropriate than using a filter here, because
  118. // filters transform user-supplied content, whereas we are extending it with
  119. // additional information.
  120. case 'view':
  121. $node->content['nodeapi_example'] = array(
  122. '#value' => theme('nodeapi_example_rating', $node->nodeapi_example_rating),
  123. '#weight' => -1,
  124. );
  125. break;
  126. }
  127. }
  128. /**
  129. * A custom theme function.
  130. *
  131. * By using this function to format our rating, themes can override this presentation
  132. * if they wish; for example, they could provide a star graphic for the rating. We
  133. * also wrap the default presentation in a CSS class that is prefixed by the module
  134. * name. This way, style sheets can modify the output without requiring theme code.
  135. */
  136. function theme_nodeapi_example_rating($rating) {
  137. $output = '<div class="nodeapi_example_rating">';
  138. $options = array(
  139. 0 => t('Unrated'),
  140. 1 => t('Poor'),
  141. 2 => t('Needs improvement'),
  142. 3 => t('Acceptable'),
  143. 4 => t('Good'),
  144. 5 => t('Excellent'));
  145. $output .= t('Rating: %rating', array('%rating' => $options[(int)$rating]));
  146. $output .= '</div>';
  147. return $output;
  148. }
Login or register to post comments