Fix for white screen of death on node_example

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

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.