node_example.module

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

Module file for Node Example module.

Part of the Examples for Developers project.

Functions & methods

NameDescription
node_example_entity_info_alterImplements hook_entity_info_alter().
node_example_field_formatter_infoImplements hook_field_formatter_info().
node_example_field_formatter_viewImplements hook_field_formatter_view().
node_example_helpImplements hook_help().
node_example_menuImplements hook_menu().
node_example_pageCallback that builds our content and returns it to the browser.
node_example_themeImplements hook_theme().
theme_example_node_colorA custom theme function.

File

node_example/node_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Module file for Node Example module.
  5. *
  6. * Part of the Examples for Developers project.
  7. */
  8. /**
  9. * @defgroup node_example Example: Node
  10. * @ingroup examples
  11. * @{
  12. * Example defining a node type in code.
  13. *
  14. * This is an example outlining how a module can be used to define a new
  15. * node type. Our example node type will allow users to specify multiple
  16. * "colors", a "quantity" and an "image" for their nodes; some kind of
  17. * rudimentary inventory-tracking system, perhaps?
  18. *
  19. * The basic pattern for defining a node type is to tell Drupal about the
  20. * node's field types, and view modes. Drupal will then take over and manage
  21. * the storage for this node type. This differs from Drupal 6, where we
  22. * would have to handle all the database storage ourselves in the module.
  23. *
  24. * Remember that most node types do not require any custom code, as one
  25. * simply creates them using the Drupal user interface. Creating a node like
  26. * this in code is a special case.
  27. *
  28. * Since we only have to define our node type once, most of the code
  29. * required to do this is moved to the node type's .install file.
  30. * Drupal 7 has us defining most of our node structure in arrays,
  31. * and passing those to node_type_save(). We use hook_install() as
  32. * a convenient place to define these types, and hook_uninstall()
  33. * as a convenient place to not only uninstall the data contained
  34. * in these nodes, but also remove the node types from Drupal's
  35. * knowledge.
  36. *
  37. * In previous versions of Drupal, "teaser" and "page" were node view modes.
  38. * In Drupal 7 we can define custom view modes to let the node know how it
  39. * should return it's data. This module declares a custom view mode called
  40. * "example_node_list".
  41. *
  42. * Consult the @link http://drupal.org/node/707832 Field API Tutorial @endlink
  43. * and @link http://drupal.org/node/443536 Field API Handbook Page @endlink
  44. * and @link field Field API documentation @endlink.
  45. *
  46. * @see node_example.install
  47. * @see field_example.module
  48. */
  49. /**
  50. * Implements hook_menu().
  51. *
  52. * We are providing a default page to illustrate the use of our custom node view
  53. * mode that will live at http://example.com/?q=examples/node_example
  54. */
  55. function node_example_menu() {
  56. $items['examples/node_example'] = array(
  57. 'page callback' => 'node_example_page',
  58. 'access arguments' => array('access content'),
  59. 'title' => 'Node Example',
  60. );
  61. return $items;
  62. }
  63. /**
  64. * Callback that builds our content and returns it to the browser.
  65. *
  66. * This callback comes from hook_menu().
  67. *
  68. * @return
  69. * a build array
  70. * a renderable array showing a list of our nodes.
  71. *
  72. * @see node_load()
  73. * @see node_view()
  74. * @see node_example_field_formatter_view()
  75. */
  76. function node_example_page() {
  77. // We'll start building a renderable array that will be our page.
  78. // For now we just declare the array.
  79. $renderable_array = array();
  80. // We query the database and find all of the nodes for the type we defined.
  81. $sql = 'SELECT nid FROM {node} n WHERE n.type = :type AND n.status = :status';
  82. $result = db_query($sql,
  83. array(
  84. ':type' => 'node_example',
  85. ':status' => 1,
  86. )
  87. );
  88. $renderable_array['explanation'] = array(
  89. '#markup' => t("Node Example nodes you've created will be displayed here. Note that the color fields will be displayed differently in this list, than if you view the node normally. Click on the node title to see the difference. This is a result of using our 'example_node_list' node view type."),
  90. );
  91. // Loop through each of our node_example nodes and instruct node_view
  92. // to use our "example_node_list" view.
  93. // http://api.drupal.org/api/function/node_load/7
  94. // http://api.drupal.org/api/function/node_view/7
  95. foreach ($result as $row) {
  96. $node = node_load($row->nid);
  97. $renderable_array['node_list'][]= node_view($node, 'example_node_list');
  98. }
  99. return $renderable_array;
  100. }
  101. /**
  102. * Implements hook_entity_info_alter().
  103. *
  104. * We need to modify the default node entity info by adding a new view mode to
  105. * be used in functions like node_view() or node_build_content().
  106. */
  107. function node_example_entity_info_alter(&$entity_info) {
  108. // Add our new view mode to the list of view modes...
  109. $entity_info['node']['view modes']['example_node_list'] = array(
  110. 'label' => t('Example Node List'),
  111. 'custom settings' => TRUE,
  112. );
  113. }
  114. /**
  115. * Implements hook_field_formatter_info().
  116. */
  117. function node_example_field_formatter_info() {
  118. return array(
  119. 'node_example_colors' => array(
  120. 'label' => t('Node Example Color Handle'),
  121. 'field types' => array('text'),
  122. ),
  123. );
  124. }
  125. /**
  126. * Implements hook_field_formatter_view().
  127. *
  128. * @todo: We need to provide a formatter for the colors that a user is allowed
  129. * to enter during node creation.
  130. */
  131. function node_example_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
  132. $element = array();
  133. switch ($display['type']) {
  134. case 'node_example_colors':
  135. foreach ($items as $delta => $item) {
  136. $element[$delta]['#type'] = 'markup';
  137. $color = $item['safe_value'];
  138. $element[$delta]['#markup'] = theme('example_node_color', array('color' => $color));
  139. }
  140. break;
  141. }
  142. return $element;
  143. }
  144. /**
  145. * Implements hook_theme().
  146. *
  147. * This lets us tell Drupal about our theme functions and their arguments.
  148. */
  149. function node_example_theme($existing, $type, $theme, $path) {
  150. return array(
  151. 'example_node_color' => array(
  152. 'variables' => array('color' => NULL),
  153. ),
  154. );
  155. }
  156. /**
  157. * Implements hook_help().
  158. */
  159. function node_example_help($path, $arg) {
  160. switch ($path) {
  161. case 'examples/node_example':
  162. return "<p>" . t("The Node Example module provides a custom node type.
  163. You can create new Example Node nodes using the <a href='!nodeadd'>node add form</a>.",
  164. array('!nodeadd' => url('node/add/node-example'))) . "</p>";
  165. }
  166. }
  167. /**
  168. * A custom theme function.
  169. *
  170. * By using this function to format our node-specific information, themes
  171. * can override this presentation if they wish. This is a simplifed theme
  172. * function purely for illustrative purposes.
  173. */
  174. function theme_example_node_color($variables) {
  175. $output = '<span style="background-color: #ccc; padding: 1em; margin-bottom: 1em; float: left; color: ' . $variables['color'] . '">' . $variables['color'] . '</span>';
  176. return $output;
  177. }
  178. /**
  179. * @} End of "defgroup node_example".
  180. */
Login or register to post comments