| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: pathauto.module,v 1.118.2.30 2010/09/27 15:52:58 greggles Exp $ 3 4 /** 5 * @defgroup pathauto Pathauto: Automatically generates aliases for content 6 * 7 * The Pathauto module automatically generates path aliases for various kinds of 8 * content (nodes, categories, users) without requiring the user to manually 9 * specify the path alias. This allows you to get aliases like 10 * /category/my-node-title.html instead of /node/123. The aliases are based upon 11 * a "pattern" system which the administrator can control. 12 */ 13 14 /** 15 * @file 16 * Main file for the Pathauto module, which automatically generates aliases for content. 17 * 18 * @ingroup pathauto 19 */ 20 21 /** 22 * Implements hook_help(). 23 */ 24 function pathauto_help($path, $arg) { 25 switch ($path) { 26 case 'admin/help#pathauto': 27 $output = t('<p>Provides a mechanism for modules to automatically generate aliases for the content they manage.</p> 28 <h2>Settings</h2> 29 <p>The <strong>Maximum Alias Length</strong> and <strong>Maximum component length</strong> values 30 default to 100 and have a limit of 128 from pathauto. This length is limited by the length of the dst 31 column of the url_alias database table. The default database schema for this column is 128. If you 32 set a length that is equal to that of the one set in the dst column it will cause problems in situations 33 where the system needs to append additional words to the aliased URL. For example... URLs generated 34 for feeds will have "/feed" added to the end. You should enter a value that is the length of the dst 35 column minus the length of any strings that might get added to the end of the URL. The length of 36 strings that might get added to the end of your URLs depends on which modules you have enabled and 37 on your Pathauto settings. The recommended and default value is 100.</p> 38 <p><strong>Raw Tokens</strong> In Pathauto it is appropriate to use the -raw form of tokens. Paths are 39 sent through a filtering system which ensures that raw user content is filtered. Failure to use -raw 40 tokens can cause problems with the Pathauto punctuation filtering system.</p>'); 41 return $output; 42 } 43 } 44 45 /** 46 * Implements hook_perm(). 47 */ 48 function pathauto_perm() { 49 return array( 50 'administer pathauto', 51 'notify of path changes', 52 ); 53 } 54 55 /** 56 * Implements hook_menu(). 57 */ 58 function pathauto_menu() { 59 $items['admin/build/path/pathauto'] = array( 60 'title' => 'Automated alias settings', 61 'page callback' => 'drupal_get_form', 62 'page arguments' => array('pathauto_admin_settings'), 63 'access arguments' => array('administer pathauto'), 64 'type' => MENU_LOCAL_TASK, 65 'weight' => 10, 66 'file' => 'pathauto.admin.inc', 67 ); 68 69 $items['admin/build/path/delete_bulk'] = array( 70 'title' => 'Delete aliases', 71 'page callback' => 'drupal_get_form', 72 'page arguments' => array('pathauto_admin_delete'), 73 'access arguments' => array('administer url aliases'), 74 'type' => MENU_LOCAL_TASK, 75 'weight' => 30, 76 'file' => 'pathauto.admin.inc', 77 ); 78 79 return $items; 80 } 81 82 /** 83 * Include all Pathauto include files. 84 */ 85 function _pathauto_include() { 86 module_load_include('inc', 'pathauto'); 87 module_load_include('inc', 'pathauto', 'pathauto_node'); 88 module_load_include('inc', 'pathauto', 'pathauto_taxonomy'); 89 module_load_include('inc', 'pathauto', 'pathauto_user'); 90 } 91 92 /** 93 * Implements hook_token_list(). 94 */ 95 function pathauto_token_list($type = 'all') { 96 $tokens = array(); 97 if (module_exists('taxonomy')) { 98 if ($type == 'taxonomy' || $type == 'all') { 99 $tokens['taxonomy']['catpath'] = t('As [cat], but including its supercategories separated by /.'); 100 $tokens['taxonomy']['catpath-raw'] = t('As [cat-raw], but including its supercategories separated by /.'); 101 $tokens['taxonomy']['catalias'] = t('The URL alias of the taxonomy term.'); 102 $tokens['taxonomy']['catalias-raw'] = t('The URL alias of the taxonomy term.'); 103 } 104 if ($type == 'node' || $type == 'all') { 105 $tokens['node']['termpath'] = t('As [term], but including its supercategories separated by /.'); 106 $tokens['node']['termpath-raw'] = t('As [term-raw], but including its supercategories separated by /.'); 107 $tokens['node']['termalias'] = t('The URL alias of the taxonomy term.'); 108 $tokens['node']['termalias-raw'] = t('The URL alias of the taxonomy term.'); 109 } 110 } 111 if (module_exists('book')) { 112 if ($type == 'node' || $type == 'all') { 113 $tokens['node']['bookpathalias'] = t('The URL alias of the parent book of the node.'); 114 $tokens['node']['bookpathalias-raw'] = t('The URL alias of the parent book of the node.'); 115 } 116 } 117 return $tokens; 118 } 119 120 /** 121 * Implements hook_token_values(). 122 */ 123 function pathauto_token_values($type, $object = NULL, $options = array(), $label = NULL) { 124 $values = array(); 125 126 switch ($type) { 127 case 'node': 128 // Token [bookpathalias]. 129 if (module_exists('book')) { 130 $values['bookpathalias'] = ''; 131 $values['bookpathalias-raw'] = ''; 132 if (!empty($object->book['plid']) && $parent = book_link_load($object->book['plid'])) { 133 $values['bookpathalias-raw'] = drupal_get_path_alias($parent['href']); 134 $values['bookpathalias'] = check_plain($values['bookpathalias-raw']); 135 } 136 } 137 138 // Tokens [termpath], [termpath-raw], and [termalias]. 139 if (module_exists('taxonomy')) { 140 // Get the lowest-weighted term from the lowest-weighted vocabulary. 141 // This query is copied from @taxonomy_node_get_terms() 142 $term = db_fetch_object(db_query_range('SELECT t.* 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.vid = %d ORDER BY v.weight, t.weight, t.name', $object->vid, 0, 1)); 143 if ($term) { 144 $values = array_merge($values, pathauto_token_values('taxonomy', $term, $options, 'term')); 145 } 146 else { 147 $values['termpath'] = $values['termpath-raw'] = $values['termalias'] = ''; 148 } 149 } 150 break; 151 152 case 'taxonomy': 153 // In the realm of nodes these are 'terms', in the realm of taxonomy, 'cats'. 154 if (!isset($label)) { 155 $label = 'cat'; 156 } 157 158 $values[$label . 'path'] = ''; 159 $values[$label . 'path-raw'] = ''; 160 $values[$label . 'alias'] = ''; 161 $values[$label . 'alias-raw'] = ''; 162 163 // Tokens [catpath] and [catpath-raw]. 164 if (isset($object->tid)) { 165 module_load_include('inc', 'pathauto'); 166 $parents = taxonomy_get_parents_all($object->tid); 167 $catpath = $catpath_raw = array(); 168 foreach ($parents as $parent) { 169 array_unshift($catpath, check_plain($parent->name)); 170 array_unshift($catpath_raw, $parent->name); 171 } 172 $values[$label . 'path'] = !empty($options['pathauto']) ? $catpath : implode('/', $catpath); 173 $values[$label . 'path-raw'] = !empty($options['pathauto']) ? $catpath_raw : implode('/', $catpath_raw); 174 175 // Token [catalias-raw] and [catalias]. 176 $values[$label . 'alias-raw'] = drupal_get_path_alias(taxonomy_term_path($object)); 177 $values[$label . 'alias'] = check_plain($values[$label . 'alias-raw']); 178 } 179 break; 180 } 181 182 return $values; 183 } 184 185 /** 186 * Implementation of hook_path_alias_types(). 187 * 188 * Used primarily by the bulk delete form. 189 */ 190 function pathauto_path_alias_types() { 191 $objects['user/'] = t('Users'); 192 $objects['node/'] = t('Content'); 193 if (module_exists('blog')) { 194 $objects['blog/'] = t('User blogs'); 195 } 196 if (module_exists('taxonomy')) { 197 $objects['taxonomy/term/'] = t('Taxonomy terms'); 198 } 199 if (module_exists('forum')) { 200 $objects['forum/'] = t('Forums'); 201 } 202 if (module_exists('contact')) { 203 $objects['user/%/contact'] = t('User contact forms'); 204 } 205 if (module_exists('tracker')) { 206 $objects['user/%/track'] = t('User trackers'); 207 } 208 return $objects; 209 } 210 211 //============================================================================== 212 // Some node related functions. 213 214 /** 215 * Implements hook_nodeapi(). 216 */ 217 function pathauto_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { 218 switch ($op) { 219 case 'presave': 220 // About to be saved (before insert/update) 221 if (!empty($node->pathauto_perform_alias) && isset($node->old_alias) 222 && $node->path == '' && $node->old_alias != '') { 223 /** 224 * There was an old alias, but when pathauto_perform_alias was checked 225 * the javascript disabled the textbox which led to an empty value being 226 * submitted. Restoring the old path-value here prevents the Path module 227 * from deleting any old alias before Pathauto gets control. 228 */ 229 $node->path = $node->old_alias; 230 } 231 break; 232 case 'insert': 233 case 'update': 234 _pathauto_include(); 235 // Get the specific pattern or the default 236 if (variable_get('language_content_type_'. $node->type, 0)) { 237 $pattern = trim(variable_get('pathauto_node_'. $node->type .'_'. $node->language .'_pattern', FALSE)); 238 } 239 if (empty($pattern)) { 240 $pattern = trim(variable_get('pathauto_node_'. $node->type .'_pattern', FALSE)); 241 if (empty($pattern)) { 242 $pattern = trim(variable_get('pathauto_node_pattern', FALSE)); 243 } 244 } 245 // Only do work if there's a pattern 246 if ($pattern) { 247 // Only create an alias if the checkbox was not provided or if the checkbox was provided and is checked 248 if (!isset($node->pathauto_perform_alias) || $node->pathauto_perform_alias) { 249 $placeholders = pathauto_get_placeholders('node', $node); 250 $src = "node/$node->nid"; 251 $node->path = pathauto_create_alias('node', $op, $placeholders, $src, $node->nid, $node->type, $node->language); 252 } 253 } 254 break; 255 case 'delete': 256 path_set_alias('node/'. $node->nid); 257 path_set_alias('node/'. $node->nid .'/feed'); 258 break; 259 } 260 } 261 262 /** 263 * Implements hook_form_alter(). 264 * 265 * This allows alias creators to override Pathauto and specify their 266 * own aliases (Pathauto will be invisible to other users). Inserted 267 * into the path module's fieldset in the node form. 268 */ 269 function pathauto_form_alter(&$form, $form_state, $form_id) { 270 // Process only node forms. 271 if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) { 272 $node = $form['#node']; 273 $pattern = FALSE; 274 275 // Find if there is an automatic alias pattern for this node type. 276 if (isset($form['language'])) { 277 $language = isset($form['language']['#value']) ? $form['language']['#value'] : $form['language']['#default_value']; 278 $pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_'. $language .'_pattern', '')); 279 } 280 if (!$pattern) { 281 $pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_pattern', '')); 282 if (!$pattern) { 283 $pattern = trim(variable_get('pathauto_node_pattern', '')); 284 } 285 } 286 287 // If there is a pattern, show the automatic alias checkbox. 288 if ($pattern) { 289 if (!isset($node->pathauto_perform_alias)) { 290 if (!empty($node->nid)) { 291 // If this is not a new node, compare it's current alias to the 292 // alias that would be genereted by pathauto. If they are the same, 293 // then keep the automatic alias enabled. 294 _pathauto_include(); 295 $placeholders = pathauto_get_placeholders('node', $node); 296 $pathauto_alias = pathauto_create_alias('node', 'return', $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language); 297 $node->pathauto_perform_alias = isset($node->path) && $node->path == $pathauto_alias; 298 } 299 else { 300 // If this is a new node, enable the automatic alias. 301 $node->pathauto_perform_alias = TRUE; 302 } 303 } 304 305 // Add JavaScript that will disable the path textfield when the automatic 306 // alias checkbox is checked. 307 drupal_add_js(drupal_get_path('module', 'pathauto') .'/pathauto.js'); 308 309 // Override path.module's vertical tabs summary. 310 $form['path']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'pathauto') . '/pathauto.js'; 311 312 $form['path']['pathauto_perform_alias'] = array( 313 '#type' => 'checkbox', 314 '#title' => t('Automatic alias'), 315 '#default_value' => $node->pathauto_perform_alias, 316 '#description' => t('An alias will be generated for you. If you wish to create your own alias below, uncheck this option.'), 317 '#weight' => -1, 318 ); 319 320 if (user_access('administer pathauto')) { 321 $form['path']['pathauto_perform_alias']['#description'] .= ' '. t('To control the format of the generated aliases, see the <a href="@pathauto">automated alias settings</a>.', array('@pathauto' => url('admin/build/path/pathauto'))); 322 } 323 324 if ($node->pathauto_perform_alias && !empty($node->old_alias) && empty($node->path)) { 325 $form['path']['path']['#default_value'] = $node->old_alias; 326 $node->path = $node->old_alias; 327 } 328 329 // For Pathauto to remember the old alias and prevent the Path-module from deleteing it when Pathauto wants to preserve it 330 if (isset($node->path)) { 331 $form['path']['old_alias'] = array( 332 '#type' => 'value', 333 '#value' => $node->path, 334 ); 335 } 336 } 337 } 338 } 339 340 /** 341 * Implements hook_node_operations(). 342 */ 343 function pathauto_node_operations() { 344 $operations['pathauto_update_alias'] = array( 345 'label' => t('Update URL alias'), 346 'callback' => 'pathauto_node_update_alias_multiple', 347 'callback arguments' => array('bulkupdate', TRUE), 348 ); 349 return $operations; 350 } 351 352 /** 353 * Update the URL aliases for an individual node. 354 * 355 * @param $node 356 * A node object. 357 * @param $op 358 * Operation being performed on the node ('insert', 'update' or 'bulkupdate'). 359 */ 360 function pathauto_node_update_alias($node, $op) { 361 module_load_include('inc', 'pathauto'); 362 $placeholders = pathauto_get_placeholders('node', $node); 363 pathauto_create_alias('node', $op, $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language); 364 } 365 366 /** 367 * Update the URL aliases for multiple nodes. 368 * 369 * @param $nids 370 * An array of node IDs. 371 * @param $op 372 * Operation being performed on the nodes ('insert', 'update' or 373 * 'bulkupdate'). 374 * @param $message 375 * A boolean if TRUE will display a message about how many nodes were 376 * updated. 377 */ 378 function pathauto_node_update_alias_multiple($nids, $op, $message = FALSE) { 379 foreach ($nids as $nid) { 380 if ($node = node_load($nid, NULL, TRUE)) { 381 pathauto_node_update_alias($node, $op); 382 } 383 } 384 if ($message) { 385 drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.')); 386 } 387 } 388 389 /** 390 * Wrapper function backwards compatibility. Should be avoided. 391 * 392 * @param $nodes 393 * An array of node IDs. 394 * 395 * @see pathauto_node_update_alias_multiple(). 396 */ 397 function pathauto_node_operations_update($nodes) { 398 return pathauto_node_update_alias_multiple($nodes, 'bulkupdate'); 399 } 400 401 //============================================================================== 402 // Taxonomy related functions. 403 404 /** 405 * Implements hook_taxonomy(). 406 */ 407 function pathauto_taxonomy($op, $type, $object = NULL) { 408 switch ($type) { 409 case 'term': 410 switch ($op) { 411 case 'insert': 412 case 'update': 413 _pathauto_include(); 414 // Use the category info to automatically create an alias 415 $term = (object) $object; 416 417 // Clear the taxonomy term's static cache. 418 if ($op == 'update') { 419 taxonomy_get_term($term->tid, TRUE); 420 } 421 422 if ($term->name) { 423 $count = _taxonomy_pathauto_alias($term, $op); 424 } 425 426 // For all children generate new alias (important if [catpath] used) 427 foreach (taxonomy_get_tree($term->vid, $term->tid) as $subcategory) { 428 $count = _taxonomy_pathauto_alias($subcategory, $op); 429 } 430 431 break; 432 case 'delete': 433 // If the category is deleted, remove the path aliases 434 $term = (object) $object; 435 path_set_alias('taxonomy/term/'. $term->tid); 436 path_set_alias(taxonomy_term_path($term)); 437 path_set_alias('forum/'. $term->tid); 438 path_set_alias('taxonomy/term/'. $term->tid .'/0/feed'); 439 break; 440 } 441 break; 442 } 443 } 444 445 //============================================================================== 446 // User related functions. 447 448 /** 449 * Implements hook_user(). 450 */ 451 function pathauto_user($op, &$edit, &$user, $category = NULL) { 452 switch ($op) { 453 case 'insert': 454 case 'update': 455 _pathauto_include(); 456 // Use the username to automatically create an alias 457 $pathauto_user = (object) array_merge((array) $user, $edit); 458 if ($user->name) { 459 $placeholders = pathauto_get_placeholders('user', $pathauto_user); 460 $src = 'user/'. $user->uid; 461 $alias = pathauto_create_alias('user', $op, $placeholders, $src, $user->uid); 462 463 if (module_exists('blog')) { 464 $new_user = drupal_clone($user); 465 if ($category == 'account') { 466 $new_user->roles = isset($edit['roles']) ? $edit['roles'] : array(); 467 $new_user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; // Add this back 468 } 469 if (node_access('create', 'blog', $new_user)) { 470 $src = 'blog/'. $user->uid; 471 $alias = pathauto_create_alias('blog', $op, $placeholders, $src, $user->uid); 472 } 473 else { 474 path_set_alias('blog/'. $user->uid); 475 path_set_alias('blog/'. $user->uid .'/feed'); 476 } 477 } 478 if (module_exists('tracker')) { 479 $src = 'user/'. $user->uid .'/track'; 480 $alias = pathauto_create_alias('tracker', $op, $placeholders, $src, $user->uid); 481 } 482 if (module_exists('contact')) { 483 $src = 'user/'. $user->uid .'/contact'; 484 $alias = pathauto_create_alias('contact', $op, $placeholders, $src, $user->uid); 485 } 486 } 487 break; 488 case 'delete': 489 // If the user is deleted, remove the path aliases 490 $user = (object) $user; 491 path_set_alias('user/'. $user->uid); 492 493 // They may have enabled these modules and/or feeds when the user was created, so let's try to delete all of them 494 path_set_alias('blog/'. $user->uid); 495 path_set_alias('blog/'. $user->uid .'/feed'); 496 path_set_alias('user/'. $user->uid .'/track'); 497 path_set_alias('user/'. $user->uid .'/track/feed'); 498 path_set_alias('user/'. $user->uid .'/contact'); 499 break; 500 default: 501 break; 502 } 503 } 504 505 /** 506 * Implements hook_user_operations(). 507 */ 508 function pathauto_user_operations() { 509 $operations['pathauto_update_alias'] = array( 510 'label' => t('Update URL alias'), 511 'callback' => 'pathauto_user_update_alias_multiple', 512 'callback arguments' => array('bulkupdate', TRUE), 513 ); 514 return $operations; 515 } 516 517 /** 518 * Update the URL aliases for an individual user account. 519 * 520 * @param $account 521 * A user account object. 522 * @param $op 523 * Operation being performed on the account ('insert', 'update' or 524 * 'bulkupdate'). 525 * 526 * @todo Remove support for any sub-path aliases. 527 */ 528 function pathauto_user_update_alias($account, $op) { 529 module_load_include('inc', 'pathauto'); 530 $placeholders = pathauto_get_placeholders('user', $account); 531 pathauto_create_alias('user', $op, $placeholders, "user/{$account->uid}", $account->uid); 532 533 if (module_exists('blog')) { 534 if (node_access('create', 'blog', $account)) { 535 pathauto_create_alias('blog', $op, $placeholders, "blog/{$account->uid}", $account->uid); 536 } 537 else { 538 path_set_alias('blog/'. $user->uid); 539 path_set_alias('blog/'. $user->uid .'/feed'); 540 } 541 } 542 if (module_exists('tracker')) { 543 $alias = pathauto_create_alias('tracker', $op, $placeholders, "user/{$account->uid}/track", $user->uid); 544 } 545 if (module_exists('contact')) { 546 $alias = pathauto_create_alias('contact', $op, $placeholders, "user/{$account->uid}/contact", $user->uid); 547 } 548 } 549 550 /** 551 * Update the URL aliases for multiple user accounts. 552 * 553 * @param $uids 554 * An array of user account IDs. 555 * @param $op 556 * Operation being performed on the accounts ('insert', 'update' or 557 * 'bulkupdate'). 558 * @param $message 559 * A boolean if TRUE will display a message about how many accounts were 560 * updated. 561 */ 562 function pathauto_user_update_alias_multiple($uids, $op, $message = FALSE) { 563 foreach ($uids as $uid) { 564 if ($account = user_load($uid)) { 565 pathauto_user_update_alias($account, $op); 566 } 567 } 568 if ($message) { 569 drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.')); 570 } 571 }
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 |