[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/default/files/hooks/ -> system.node.php (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * These hooks are defined by node modules, modules that define a new kind
   6   * of node.
   7   *
   8   * If you don't need to make a new node type but rather extend the existing
   9   * ones, you should instead investigate using hook_nodeapi().
  10   *
  11   * Node hooks are typically called by node.module using node_invoke().
  12   */
  13  
  14  /**
  15   * @addtogroup hooks
  16   * @{
  17   */
  18  
  19  
  20  /**
  21   * Define module-provided node types.
  22   *
  23   * This is a hook used by node modules. This hook is required for modules to
  24   * define one or more node types. It is called to determine the names and the
  25   * attributes of a module's node types.
  26   *
  27   * Only module-provided node types should be defined through this hook. User-
  28   * provided (or 'custom') node types should be defined only in the 'node_type'
  29   * database table, and should be maintained by using the node_type_save() and
  30   * node_type_delete() functions.
  31   *
  32   * @return
  33   *   An array of information on the module's node types. The array contains a
  34   *   sub-array for each node type, with the machine-readable type name as the
  35   *   key. Each sub-array has up to 10 attributes. Possible attributes:
  36   *   - "name": the human-readable name of the node type. Required.
  37   *   - "module": a string telling Drupal how a module's functions map to hooks
  38   *      (i.e. if module is defined as example_foo, then example_foo_insert will
  39   *      be called when inserting a node of that type). This string is usually
  40   *      the name of the module in question, but not always. Required.
  41   *   - "description": a brief description of the node type. Required.
  42   *   - "help": text that will be displayed at the top of the submission form for
  43   *      this content type. Optional (defaults to '').
  44   *   - "has_title": boolean indicating whether or not this node type has a title
  45   *      field. Optional (defaults to TRUE).
  46   *   - "title_label": the label for the title field of this content type.
  47   *      Optional (defaults to 'Title').
  48   *   - "has_body": boolean indicating whether or not this node type has a  body
  49   *      field. Optional (defaults to TRUE).
  50   *   - "body_label": the label for the body field of this content type. Optional
  51   *      (defaults to 'Body').
  52   *   - "min_word_count": the minimum number of words for the body field to be
  53   *      considered valid for this content type. Optional (defaults to 0).
  54   *   - "locked": boolean indicating whether the machine-readable name of this
  55   *      content type can (FALSE) or cannot (TRUE) be edited by a site
  56   *      administrator. Optional (defaults to TRUE).
  57   *
  58   * The machine-readable name of a node type should contain only letters,
  59   * numbers, and underscores. Underscores will be converted into hyphens for the
  60   * purpose of constructing URLs.
  61   *
  62   * All attributes of a node type that are defined through this hook (except for
  63   * 'locked') can be edited by a site administrator. This includes the
  64   * machine-readable name of a node type, if 'locked' is set to FALSE.
  65   *
  66   * For a detailed usage example, see node_example.module.
  67   */
  68  function hook_node_info() {
  69    return array(
  70      'book' => array(
  71        'name' => t('book page'),
  72        'module' => 'book',
  73        'description' => t("A book is a collaborative writing effort: users can collaborate writing the pages of the book, positioning the pages in the right order, and reviewing or modifying pages previously written. So when you have some information to share or when you read a page of the book and you didn't like it, or if you think a certain page could have been written better, you can do something about it."),
  74      )
  75    );
  76  }
  77  
  78  /**
  79   * Act on node type changes.
  80   *
  81   * This hook allows modules to take action when a node type is modified.
  82   *
  83   * @param $op
  84   *   What is being done to $info. Possible values:
  85   *   - "delete"
  86   *   - "insert"
  87   *   - "update"
  88   *   Most modules will not need the update or insert operations, because form
  89   *   fields you add to the node type editing form will be automatically saved as
  90   *   variables. For instance, if you add a field called 'my_module_foo' to
  91   *   content type 'machine_name', its value will be saved as variable
  92   *   'my_module_foo_machine_name' (and if the machine name is updated, the
  93   *   variables will be updated appropriately).
  94   * @param $info
  95   *   The node type object on which $op is being performed.
  96   */
  97  function hook_node_type($op, $info) {
  98    switch ($op){
  99      case 'delete':
 100        // Example from comment.module.
 101        variable_del('comment_'. $info->type);
 102        break;
 103      case 'update':
 104        // Here is an example where you do need an update operation (from the
 105        // book module), because the simple default case doesn't cover what needs
 106        // to be done.
 107        if (!empty($type->old_type) && $type->old_type != $type->type) {
 108          // Update the list of node types that are allowed to be added to books.
 109          $allowed_types = variable_get('book_allowed_types', array('book'));
 110          $key = array_search($type->old_type, $allowed_types);
 111          if ($key !== FALSE) {
 112            $allowed_types[$type->type] = $allowed_types[$key] ? $type->type : 0;
 113            unset($allowed_types[$key]);
 114            variable_set('book_allowed_types', $allowed_types);
 115          }
 116          // Update the setting for the "Add child page" link.
 117          if (variable_get('book_child_type', 'book') == $type->old_type) {
 118            variable_set('book_child_type', $type->type);
 119          }
 120        }
 121        break;
 122    }
 123  }
 124  
 125  /**
 126   * Define access restrictions.
 127   *
 128   * This hook allows node modules to limit access to the node types they
 129   * define.
 130   *
 131   * @param $op
 132   *   The operation to be performed. Possible values:
 133   *   - "create"
 134   *   - "delete"
 135   *   - "update"
 136   *   - "view"
 137   * @param $node
 138   *   The node on which the operation is to be performed, or, if it does
 139   *   not yet exist, the type of node to be created.
 140   * @param $account
 141   *   A user object representing the user for whom the operation is to be
 142   *   performed.
 143   * @return
 144   *   TRUE if the operation is  to be allowed;
 145   *   FALSE if the operation is to be denied;
 146   *   NULL to not override the settings in the node_access table, or access
 147   *     control modules.
 148   *
 149   * The administrative account (user ID #1) always passes any access check,
 150   * so this hook is not called in that case. If this hook is not defined for
 151   * a node type, all access checks will fail, so only the administrator will
 152   * be able to see content of that type. However, users with the "administer
 153   * nodes" permission may always view and edit content through the
 154   * administrative interface.
 155   * @see http://api.drupal.org/api/group/node_access/6
 156   *
 157   * For a detailed usage example, see node_example.module.
 158   *
 159   * @ingroup node_access
 160   */
 161  function hook_access($op, $node, $account) {
 162    if ($op == 'create') {
 163      return user_access('create stories', $account);
 164    }
 165  
 166    if ($op == 'update' || $op == 'delete') {
 167      if (user_access('edit own stories', $account) && ($account->uid == $node->uid)) {
 168        return TRUE;
 169      }
 170    }
 171  }
 172  
 173  /**
 174   * Respond to node deletion.
 175   *
 176   * This is a hook used by node modules. It is called to allow the module
 177   * to take action when a node is being deleted from the database by, for
 178   * example, deleting information from related tables.
 179   *
 180   * @param &$node
 181   *   The node being deleted.
 182   * @return
 183   *   None.
 184   *
 185   * To take action when nodes of any type are deleted (not just nodes of
 186   * the type defined by this module), use hook_nodeapi() instead.
 187   *
 188   * For a detailed usage example, see node_example.module.
 189   */
 190  function hook_delete(&$node) {
 191    db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
 192  }
 193  
 194  /**
 195   * This is a hook used by node modules. It is called after load but before the
 196   * node is shown on the add/edit form.
 197   *
 198   * @param &$node
 199   *   The node being saved.
 200   * @return
 201   *   None.
 202   *
 203   * For a usage example, see image.module.
 204   */
 205  function hook_prepare(&$node) {
 206    if ($file = file_check_upload($field_name)) {
 207      $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
 208      if ($file) {
 209        if (!image_get_info($file->filepath)) {
 210          form_set_error($field_name, t('Uploaded file is not a valid image'));
 211          return;
 212        }
 213      }
 214      else {
 215        return;
 216      }
 217      $node->images['_original'] = $file->filepath;
 218      _image_build_derivatives($node, true);
 219      $node->new_file = TRUE;
 220    }
 221  }
 222  
 223  
 224  /**
 225   * Display a node editing form.
 226   *
 227   * This hook, implemented by node modules, is called to retrieve the form
 228   * that is displayed when one attempts to "create/edit" an item. This form is
 229   * displayed at the URI http://www.example.com/?q=node/<add|edit>/nodetype.
 230   *
 231   * @param &$node
 232   *   The node being added or edited.
 233   * @param $form_state
 234   *   The form state array. Changes made to this variable will have no effect.
 235   * @return
 236   *   An array containing the form elements to be displayed in the node
 237   *   edit form.
 238   *
 239   * The submit and preview buttons, taxonomy controls, and administrative
 240   * accoutrements are displayed automatically by node.module. This hook
 241   * needs to return the node title, the body text area, and fields
 242   * specific to the node type.
 243   *
 244   * For a detailed usage example, see node_example.module.
 245   */
 246  function hook_form(&$node, $form_state) {
 247    $type = node_get_types('type', $node);
 248  
 249    $form['title'] = array(
 250      '#type'=> 'textfield',
 251      '#title' => check_plain($type->title_label),
 252      '#required' => TRUE,
 253    );
 254    $form['body'] = array(
 255      '#type' => 'textarea',
 256      '#title' => check_plain($type->body_label),
 257      '#rows' => 20,
 258      '#required' => TRUE,
 259    );
 260    $form['field1'] = array(
 261      '#type' => 'textfield',
 262      '#title' => t('Custom field'),
 263      '#default_value' => $node->field1,
 264      '#maxlength' => 127,
 265    );
 266    $form['selectbox'] = array(
 267      '#type' => 'select',
 268      '#title' => t('Select box'),
 269      '#default_value' => $node->selectbox,
 270      '#options' => array(
 271        1 => 'Option A',
 272        2 => 'Option B',
 273        3 => 'Option C',
 274      ),
 275      '#description' => t('Please choose an option.'),
 276    );
 277  
 278    return $form;
 279  }
 280  
 281  /**
 282   * Respond to node insertion.
 283   *
 284   * This is a hook used by node modules. It is called to allow the module
 285   * to take action when a new node is being inserted in the database by,
 286   * for example, inserting information into related tables.
 287   *
 288   * @param $node
 289   *   The node being inserted.
 290   * @return
 291   *   None.
 292   *
 293   * To take action when nodes of any type are inserted (not just nodes of
 294   * the type(s) defined by this module), use hook_nodeapi() instead.
 295   *
 296   * For a detailed usage example, see node_example.module.
 297   */
 298  function hook_insert($node) {
 299    db_query("INSERT INTO {mytable} (nid, extra)
 300      VALUES (%d, '%s')", $node->nid, $node->extra);
 301  }
 302  
 303  /**
 304   * Load node-type-specific information.
 305   *
 306   * This is a hook used by node modules. It is called to allow the module
 307   * a chance to load extra information that it stores about a node, or
 308   * possibly replace already loaded information - which can be dangerous.
 309   *
 310   * @param $node
 311   *   The node being loaded. At call time, node.module has already loaded
 312   *   the basic information about the node, such as its node ID (nid),
 313   *   title, and body.
 314   * @return
 315   *   An object containing properties of the node being loaded. This will
 316   *   be merged with the passed-in $node to result in an object containing
 317   *   a set of properties resulting from adding the extra properties to
 318   *   the passed-in ones, and overwriting the passed-in ones with the
 319   *   extra properties if they have the same name as passed-in properties.
 320   *
 321   * For a detailed usage example, see node_example.module.
 322   */
 323  function hook_load($node) {
 324    $additions = db_fetch_object(db_query('SELECT * FROM {mytable} WHERE vid = %d', $node->vid));
 325    return $additions;
 326  }
 327  
 328  /**
 329   * Respond to node updating.
 330   *
 331   * This is a hook used by node modules. It is called to allow the module
 332   * to take action when an edited node is being updated in the database by,
 333   * for example, updating information in related tables.
 334   *
 335   * @param $node
 336   *   The node being updated.
 337   * @return
 338   *   None.
 339   *
 340   * To take action when nodes of any type are updated (not just nodes of
 341   * the type(s) defined by this module), use hook_nodeapi() instead.
 342   *
 343   * For a detailed usage example, see node_example.module.
 344   */
 345  function hook_update($node) {
 346    db_query("UPDATE {mytable} SET extra = '%s' WHERE nid = %d",
 347      $node->extra, $node->nid);
 348  }
 349  
 350  /**
 351   * Verify a node editing form.
 352   *
 353   * This is a hook used by node modules. It is called to allow the module
 354   * to verify that the node is in a format valid to post to the site.
 355   * Errors should be set with form_set_error().
 356   *
 357   * @param $node
 358   *   The node to be validated.
 359   * @param $form
 360   *   The node edit form array.
 361   * @return
 362   *   None.
 363   *
 364   * To validate nodes of all types (not just nodes of the type(s) defined by
 365   * this module), use hook_nodeapi() instead.
 366   *
 367   * Changes made to the $node object within a hook_validate() function will
 368   * have no effect.  The preferred method to change a node's content is to use
 369   * hook_nodeapi($op='presave') instead. If it is really necessary to change
 370   * the node at the validate stage, you can use function form_set_value().
 371   *
 372   * For a detailed usage example, see node_example.module.
 373   */
 374  function hook_validate($node, &$form) {
 375    if (isset($node->end) && isset($node->start)) {
 376      if ($node->start > $node->end) {
 377        form_set_error('time', t('An event may not end before it starts.'));
 378      }
 379    }
 380  }
 381  
 382  /**
 383   * Display a node.
 384   *
 385   * This is a hook used by node modules. It allows a module to define a
 386   * custom method of displaying its nodes, usually by displaying extra
 387   * information particular to that node type.
 388   *
 389   * @param $node
 390   *   The node to be displayed.
 391   * @param $teaser
 392   *   Whether we are to generate a "teaser" or summary of the node, rather than
 393   *   display the whole thing.
 394   * @param $page
 395   *   Whether the node is being displayed as a standalone page. If this is
 396   *   TRUE, the node title should not be displayed, as it will be printed
 397   *   automatically by the theme system. Also, the module may choose to alter
 398   *   the default breadcrumb trail in this case.
 399   * @return
 400   *   $node. The passed $node parameter should be modified as necessary and
 401   *   returned so it can be properly presented. Nodes are prepared for display
 402   *   by assembling a structured array in $node->content, rather than directly
 403   *   manipulating $node->body and $node->teaser. The format of this array is
 404   *   the same used by the Forms API. As with FormAPI arrays, the #weight
 405   *   property can be used to control the relative positions of added elements.
 406   *   If for some reason you need to change the body or teaser returned by
 407   *   node_prepare(), you can modify $node->content['body']['#value']. Note
 408   *   that this will be the un-rendered content. To modify the rendered output,
 409   *   see hook_nodeapi($op = 'alter').
 410   *
 411   * For a detailed usage example, see node_example.module.
 412   */
 413  function hook_view($node, $teaser = FALSE, $page = FALSE) {
 414    if ($page) {
 415      $breadcrumb = array();
 416      $breadcrumb[] = l(t('Home'), NULL);
 417      $breadcrumb[] = l(t('Example'), 'example');
 418      $breadcrumb[] = l($node->field1, 'example/' . $node->field1);
 419      drupal_set_breadcrumb($breadcrumb);
 420    }
 421  
 422    $node = node_prepare($node, $teaser);
 423    $node->content['myfield'] = array(
 424      '#value' => theme('mymodule_myfield', $node->myfield),
 425      '#weight' => 1,
 426    );
 427  
 428    return $node;
 429  }
 430  
 431  /**
 432   * @} End of "addtogroup hooks".
 433   */


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7