node_example.module

  1. drupal
    1. 6
    2. 8
    3. 7

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.
View source
<?php

/**
 * @file
 * Module file for Node Example module.
 *
 * Part of the Examples for Developers project.
 */

/**
 * @defgroup node_example Example: Node
 * @ingroup examples
 * @{
 * 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 field types, 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.
 *
 * Since we only have to define our node type once, most of the code
 * required to do this is moved to the node type's .install file.
 * Drupal 7 has us defining most of our node structure in arrays,
 * and passing those to node_type_save(). We use hook_install() as
 * a convenient place to define these types, and hook_uninstall()
 * as a convenient place to not only uninstall the data contained
 * in these nodes, but also remove the node types from Drupal's
 * knowledge.
 *
 * 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 @link http://drupal.org/node/707832 Field API Tutorial @endlink
 * and @link http://drupal.org/node/443536 Field API Handbook Page @endlink
 * and @link field Field API documentation @endlink.
 *
 * @see node_example.install
 * @see field_example.module
 */

/**
 * Implements hook_menu().
 *
 * We are providing a default page to illustrate the use of our custom node view
 * mode that will live at http://example.com/?q=examples/node_example
 */
function node_example_menu() {
  $items['examples/node_example'] = array(
    'page callback' => 'node_example_page',
    'access arguments' => array('access content'),
    'title' => 'Node Example',
  );
  return $items;
}

/**
 * Callback that builds our content and returns it to the browser.
 *
 * This callback comes from hook_menu().
 *
 * @return
 *   a build array
 *   a renderable array showing a list of our nodes.
 *
 * @see node_load()
 * @see node_view()
 * @see node_example_field_formatter_view()
 */
function node_example_page() {
  // We'll start building a renderable array that will be our page.
  // For now we just declare the array.
  $renderable_array = array();
  // We query the database and find all of the nodes for the type we defined.
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type AND n.status = :status';
  $result = db_query($sql,
    array(
      ':type' => 'node_example',
      ':status' => 1,
    )
  );
  $renderable_array['explanation'] = array(
    '#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."),
    );
  // Loop through each of our node_example nodes and instruct node_view
  // to use our "example_node_list" view.
  // http://api.drupal.org/api/function/node_load/7
  // http://api.drupal.org/api/function/node_view/7
  foreach ($result as $row) {
    $node = node_load($row->nid);
    $renderable_array['node_list'][]= node_view($node, 'example_node_list');
  }
  return $renderable_array;
}

/**
 * Implements hook_entity_info_alter().
 *
 * We need to modify the default node entity info by adding a new view mode to
 * be used in functions like node_view() or node_build_content().
 */
function node_example_entity_info_alter(&$entity_info) {
  // Add our new view mode to the list of view modes...
  $entity_info['node']['view modes']['example_node_list'] = array(
    'label' => t('Example Node List'),
    'custom settings' => TRUE,
  );
}


/**
 * Implements hook_field_formatter_info().
 */
function node_example_field_formatter_info() {
  return array(
    'node_example_colors' => array(
      'label' => t('Node Example Color Handle'),
      'field types' => array('text'),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 *
 * @todo: We need to provide a formatter for the colors that a user is allowed
 * to enter during node creation.
 */
function node_example_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'node_example_colors':
      foreach ($items as $delta => $item) {
        $element[$delta]['#type'] = 'markup';
        $color = $item['safe_value'];
        $element[$delta]['#markup'] = theme('example_node_color', array('color' => $color));
      }
      break;
  }

  return $element;
}

/**
 * Implements hook_theme().
 *
 * This lets us tell Drupal about our theme functions and their arguments.
 */
function node_example_theme($existing, $type, $theme, $path) {
  return array(
    'example_node_color' => array(
      'variables' => array('color' => NULL),
    ),
  );
}

/**
 * Implements hook_help().
 */
function node_example_help($path, $arg) {
  switch ($path) {
    case 'examples/node_example':
      return "<p>" . t("The Node Example module provides a custom node type.
        You can create new Example Node nodes using the <a href='!nodeadd'>node add form</a>.",
        array('!nodeadd' => url('node/add/node-example'))) . "</p>";
  }
}

/**
 * A custom theme function.
 *
 * By using this function to format our node-specific information, themes
 * can override this presentation if they wish.  This is a simplifed theme
 * function purely for illustrative purposes.
 */
function theme_example_node_color($variables) {
  $output = '<span style="background-color: #ccc; padding: 1em; margin-bottom: 1em; float: left; color: ' . $variables['color'] . '">' . $variables['color'] . '</span>';
  return $output;
}
/**
 * @} End of "defgroup node_example".
 */