[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/token/ -> token_node.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Implementations of token module hooks for the core node and book modules.
   6   *
   7   * The token module requires specific hooks to be added to modules
   8   * so that those modules can return data about their objects to the
   9   * token API.  Until and unless token becomes a part of core, the
  10   * implementations of the token hooks for core modules are provided
  11   * in the token module itself.
  12   *
  13   * @ingroup token
  14   */
  15  
  16  /**
  17   * Implementation of hook_token_values().
  18   */
  19  function node_token_values($type, $object = NULL, $options = array()) {
  20    $values = array();
  21    switch ($type) {
  22      case 'node':
  23        $node = $object;
  24        $account = db_fetch_object(db_query("SELECT name, mail FROM {users} WHERE uid = %d", $node->uid));
  25  
  26        // Adjust for the anonymous user name.
  27        if (!$node->uid && !$account->name) {
  28          $account->name = variable_get('anonymous', t('Anonymous'));
  29        }
  30  
  31        $values['nid']                = $node->nid;
  32        $values['type']               = $node->type;
  33        $values['type-name']          = node_get_types('name', $node->type);
  34        $values['language']           = check_plain($node->language);
  35        $values['title']              = check_plain($node->title);
  36        $values['title-raw']          = $node->title;
  37        $values['node-path-raw']      = drupal_get_path_alias('node/'. $node->nid);
  38        $values['node-path']          = check_plain($values['node-path-raw']);
  39        $values['node-url']           = url('node/' . $node->nid, array('absolute' => TRUE));
  40        $values['author-uid']         = $node->uid;
  41        $values['author-name']        = check_plain($account->name);
  42        $values['author-name-raw']    = $account->name;
  43        $values['author-mail']        = check_plain($account->mail);
  44        $values['author-mail-raw']    = $account->mail;
  45  
  46        $values['log-raw']            = isset($node->log) ? $node->log : '';
  47        $values['log']                = filter_xss($values['log-raw']);
  48  
  49        if (module_exists('comment')) {
  50          $values['node_comment_count'] = isset($node->comment_count) ? $node->comment_count : 0;
  51          $values['unread_comment_count'] = comment_num_new($node->nid);
  52        }
  53        else {
  54          $values['node_comment_count'] = 0;
  55          $values['unread_comment_count'] = 0;
  56        }
  57  
  58        if (isset($node->created)) {
  59          $values += token_get_date_token_values($node->created, '');
  60        }
  61  
  62        if (isset($node->changed)) {
  63          $values += token_get_date_token_values($node->changed, 'mod-');
  64        }
  65  
  66        // And now taxonomy, which is a bit more work. This code is adapted from
  67        // pathauto's handling code; it's intended for compatibility with it.
  68        if (module_exists('taxonomy') && !empty($node->taxonomy) && is_array($node->taxonomy)) {
  69          foreach ($node->taxonomy as $term) {
  70            $original_term = $term;
  71            if ((object)$term) {
  72              // With free-tagging it's somewhat hard to get the tid, vid, name values
  73              // Rather than duplicating taxonomy.module code here you should make sure your calling module
  74              // has a weight of at least 1 which will run after taxonomy has saved the data which allows us to
  75              // pull it out of the db here.
  76              if (!isset($term->name) || !isset($term->tid)) {
  77                $vid = db_result(db_query_range("SELECT t.vid FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name", $object->nid, 0, 1));
  78                if (!$vid) {
  79                  continue;
  80                }
  81                $term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY t.weight", $vid, $object->vid, 0, 1));
  82                $term->vid = $vid;
  83              }
  84  
  85              // Ok, if we still don't have a term name maybe this is a pre-taxonomy submit node
  86              // So if it's a number we can get data from it
  87              if (!isset($term->name) && is_array($original_term)) {
  88                $tid = array_shift($original_term);
  89                if (is_numeric($tid)) {
  90                  $term = taxonomy_get_term($tid);
  91                }
  92              }
  93              $values['term'] = check_plain($term->name);
  94              $values['term-raw'] = $term->name;
  95              $values['term-id'] = $term->tid;
  96              $vid = $term->vid;
  97  
  98              if (!empty($vid)) {
  99                $vocabulary = taxonomy_vocabulary_load($vid);
 100                $values['vocab'] = check_plain($vocabulary->name);
 101                $values['vocab-raw'] = $vocabulary->name;
 102                $values['vocab-id'] = $vocabulary->vid;
 103              }
 104  
 105              // The 'catpath' (and 'cat') tokens have been removed, as they caused quite a bit of confusion,
 106              // and the catpath was a relatively expensive query when the taxonomy tree was deep.
 107              //
 108              // It existed only to provide forward-compatability with pathauto module, and
 109              // for most uses of token.module, it was a relatively useless token -- it exposed
 110              // a list of term names formatted as a URL/path string. Once pathauto supports
 111              // tokens, *it* should handle this catpath alias as it's the primary consumer.
 112              break;
 113            }
 114          }
 115        }
 116        // It's possible to leave that block and still not have good data.
 117        // So, we test for these and if not set, set them.
 118        if (!isset($values['term'])) {
 119          $values['term'] = '';
 120          $values['term-raw'] = '';
 121          $values['term-id'] = '';
 122          $values['vocab'] = '';
 123          $values['vocab-raw'] = '';
 124          $values['vocab-id'] = '';
 125        }
 126  
 127        break;
 128    }
 129  
 130    return $values;
 131  }
 132  
 133  /**
 134   * Implementation of hook_token_list().
 135   */
 136  function node_token_list($type = 'all') {
 137    if ($type == 'node' || $type == 'all') {
 138      $tokens['node']['nid']             = t('The unique ID of the content item, or "node".');
 139      $tokens['node']['type']            = t('The type of the node.');
 140      $tokens['node']['type-name']       = t('The human-readable name of the node type.');
 141      $tokens['node']['language']        = t('The language the node is written in.');
 142      $tokens['node']['title']           = t('The title of the node.');
 143      $tokens['node']['title-raw']       = t('The title of the node.');
 144      $tokens['node']['node-path']       = t('The URL alias of the node.');
 145      $tokens['node']['node-path-raw']   = t('The URL alias of the node.');
 146      $tokens['node']['node-url']        = t('The URL of the node.');
 147  
 148      $tokens['node']['author-uid']      = t("The unique ID of the author of the node.");
 149      $tokens['node']['author-name']     = t("The login name of the author of the node.");
 150      $tokens['node']['author-name-raw'] = t("The login name of the author of the node.");
 151      $tokens['node']['author-mail']     = t("The email address of the author of the node.");
 152      $tokens['node']['author-mail-raw'] = t("The email address of the author of the node.");
 153  
 154      $tokens['node']['log']     = t('The explanation of the most recent changes made to the node.');
 155      $tokens['node']['log-raw'] = t('The explanation of the most recent changes made to the node.');
 156  
 157      $tokens['node'] += token_get_date_token_info(t('Node creation'));
 158      $tokens['node'] += token_get_date_token_info(t('Node modification'), 'mod-');
 159  
 160      if (module_exists('comment')) {
 161        $tokens['node']['node_comment_count']   = t("The number of comments posted on a node.");
 162        $tokens['node']['unread_comment_count'] = t("The number of comments posted on a node since the reader last viewed it.");
 163      }
 164  
 165      if (module_exists('taxonomy')) {
 166        $tokens['node']['term']            = t("Name of top taxonomy term");
 167        $tokens['node']['term-raw']        = t("Unfiltered name of top taxonomy term.");
 168        $tokens['node']['term-id']         = t("ID of top taxonomy term");
 169        $tokens['node']['vocab']           = t("Name of top term's vocabulary");
 170        $tokens['node']['vocab-raw']       = t("Unfiltered name of top term's vocabulary.");
 171        $tokens['node']['vocab-id']        = t("ID of top term's vocabulary");
 172        // Temporarily disabled -- see notes in node_token_values.
 173        // $tokens['node']['catpath']        = t("Full taxonomy tree for the topmost term");
 174      }
 175  
 176      return $tokens;
 177    }
 178  }
 179  
 180  /**
 181   * Implements hook_token_list() on behalf of menu.module.
 182   */
 183  function menu_token_list($type = 'all') {
 184    $tokens = array();
 185  
 186    if ($type == 'node' || $type == 'all') {
 187      $tokens['node']['menu']                = t("The name of the menu the node belongs to.");
 188      $tokens['node']['menu-raw']            = t("The name of the menu the node belongs to.");
 189      $tokens['node']['menupath']            = t("The menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.");
 190      $tokens['node']['menupath-raw']        = t("The unfiltered menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.");
 191      $tokens['node']['menu-link-title']     = t("The text used in the menu as link text for this item.");
 192      $tokens['node']['menu-link-title-raw'] = t("The unfiltered text used in the menu as link text for this item.");
 193      $tokens['node']['menu-link-mlid']      = t("The unique ID of the node's menu link.");
 194      $tokens['node']['menu-link-plid']      = t("The unique ID of the node's menu link parent.");
 195      $tokens['node']['menu-link-parent-path']     = t('The URL alias of the parent menu link of the node.');
 196      $tokens['node']['menu-link-parent-path-raw'] = t('The URL alias of the parent menu link of the node.');
 197    }
 198  
 199    return $tokens;
 200  }
 201  
 202  /**
 203   * Implements hook_token_values() on behalf of menu.module.
 204   */
 205  function menu_token_values($type, $object = NULL, $options = array()) {
 206    static $menus;
 207  
 208    $values = array();
 209  
 210    // Statically cache menu_get_menus() since this causes a database query
 211    // every time it is called, and is not likely to change in the same page
 212    // request.
 213    if (!isset($menus)) {
 214      $menus = menu_get_menus();
 215    }
 216  
 217    if ($type == 'node' && !empty($object)) {
 218      $node = $object;
 219  
 220      // Initialize tokens to empty strings.
 221      $values['menu']                = '';
 222      $values['menu-raw']            = '';
 223      $values['menupath']            = '';
 224      $values['menupath-raw']        = '';
 225      $values['menu-link-title']     = '';
 226      $values['menu-link-title-raw'] = '';
 227      $values['menu-link-mlid']      = '';
 228      $values['menu-link-plid']      = '';
 229      $values['menu-link-parent-path'] = '';
 230      $values['menu-link-parent-path-raw'] = '';
 231  
 232      if (!isset($node->menu)) {
 233        // Nodes do not have their menu links loaded via menu_nodeapi($node, 'load').
 234        menu_nodeapi($node, 'prepare');
 235      }
 236  
 237      // Now get the menu related information.
 238      if (!empty($node->menu['mlid'])) {
 239        $link = token_menu_link_load($node->menu['mlid']);
 240  
 241        if (isset($menus[$link['menu_name']])) {
 242          $values['menu-raw'] = $menus[$link['menu_name']];
 243          $values['menu']     = check_plain($values['menu-raw']);
 244        }
 245  
 246        $parents = token_menu_link_get_parents_all($link['mlid']);
 247        $trail_raw = array();
 248        foreach ($parents as $parent) {
 249          $trail_raw[] = $parent['title'];
 250        }
 251        $trail = array_map('check_plain', $trail_raw);
 252  
 253        $values['menupath']            = !empty($options['pathauto']) ? $trail : implode('/', $trail);
 254        $values['menupath-raw']        = !empty($options['pathauto']) ? $trail_raw : implode('/', $trail_raw);
 255        $values['menu-link-title']     = check_plain($link['title']);
 256        $values['menu-link-title-raw'] = $link['title'];
 257        $values['menu-link-mlid']      = $link['mlid'];
 258  
 259        if (!empty($link['plid']) && $parent = token_menu_link_load($link['plid'])) {
 260          $values['menu-link-plid'] = $parent['mlid'];
 261          $values['menu-link-parent-path-raw'] = drupal_get_path_alias($parent['href']);
 262          $values['menu-link-parent-path']     = check_plain($values['menu-link-parent-path-raw']);
 263        }
 264      }
 265    }
 266  
 267    return $values;
 268  }
 269  
 270  /**
 271   * Implements hook_token_list() on behalf of book.module.
 272   */
 273  function book_token_list($type) {
 274    $tokens = array();
 275  
 276    if ($type == 'node' || $type == 'all') {
 277      $tokens['book']['book']         = t("The title of the node's book parent.");
 278      $tokens['book']['book-raw']     = t("The title of the node's book parent.");
 279      $tokens['book']['book_id']      = t("The id of the node's book parent.");
 280      $tokens['book']['bookpath']     = t("The titles of all parents in the node's book hierarchy.");
 281      $tokens['book']['bookpath-raw'] = t("The titles of all parents in the node's book hierarchy.");
 282    }
 283  
 284    return $tokens;
 285  }
 286  
 287  /**
 288   * Implements hook_token_values() on behalf of book.module.
 289   */
 290  function book_token_values($type, $object = NULL, $options = array()) {
 291    $values = array();
 292  
 293    if ($type == 'node' && !empty($object)) {
 294      $node = $object;
 295  
 296      // Initialize tokens to empty strings.
 297      $values['book'] = '';
 298      $values['book-raw'] = '';
 299      $values['book_id'] = '';
 300      $values['bookpath'] = '';
 301      $values['bookpath-raw'] = '';
 302  
 303      if (!empty($node->book['mlid'])) {
 304        // Exclude the current node's title from the book path trail (start with
 305        // the book link's plid rather than mlid).
 306        $parents = token_menu_link_get_parents_all($node->book['plid']);
 307        $trail_raw = array();
 308        foreach ($parents as $parent) {
 309          $trail_raw[] = $parent['title'];
 310        }
 311        $trail = array_map('check_plain', $trail_raw);
 312  
 313        // Load the root book page.
 314        $root = token_menu_link_load($node->book['p1']);
 315  
 316        $values['book'] = check_plain($root['title']);
 317        $values['book-raw'] = $root['title'];
 318        $values['book_id'] = $node->book['bid'];
 319        $values['bookpath'] = !empty($options['pathauto']) ? $trail : implode('/', $trail);
 320        $values['bookpath-raw'] = !empty($options['pathauto']) ? $trail_raw : implode('/', $trail_raw);
 321      }
 322    }
 323  
 324    return $values;
 325  }


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