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