Provides a mechanism for modules to automatically generate aliases for the content they manage.
The Maximum Alias Length and Maximum component length values default to 100 and have a limit of 128 from pathauto. This length is limited by the length of the dst column of the url_alias database table. The default database schema for this column is 128. If you set a length that is equal to that of the one set in the dst column it will cause problems in situations where the system needs to append additional words to the aliased URL. For example... URLs generated for feeds will have "/feed" added to the end. You should enter a value that is the length of the dst column minus the length of any strings that might get added to the end of the URL. The length of strings that might get added to the end of your URLs depends on which modules you have enabled and on your Pathauto settings. The recommended and default value is 100.
Raw Tokens In Pathauto it is appropriate to use the -raw form of tokens. Paths are sent through a filtering system which ensures that raw user content is filtered. Failure to use -raw tokens can cause problems with the Pathauto punctuation filtering system.
'); return $output; } } /** * Implementation of hook_perm(). */ function pathauto_perm() { return array('administer pathauto', 'notify of path changes'); } /** * Implementation of hook_menu(). */ function pathauto_menu() { $items['admin/build/path/pathauto'] = array( 'title' => 'Automated alias settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('pathauto_admin_settings'), 'access callback' => 'user_access', 'access arguments' => array('administer pathauto'), 'type' => MENU_LOCAL_TASK, 'weight' => 10, 'file' => 'pathauto.admin.inc', ); $items['admin/build/path/delete_bulk'] = array( 'title' => 'Delete aliases', 'page callback' => 'drupal_get_form', 'page arguments' => array('pathauto_admin_delete'), 'access arguments' => array('administer url aliases'), 'type' => MENU_LOCAL_TASK, 'file' => 'pathauto.admin.inc', ); return $items; } /** * Include all Pathauto include files. */ function _pathauto_include() { module_load_include('inc', 'pathauto'); module_load_include('inc', 'pathauto', 'pathauto_node'); module_load_include('inc', 'pathauto', 'pathauto_taxonomy'); module_load_include('inc', 'pathauto', 'pathauto_user'); } /** * Implementation of hook_token_values() for Pathauto specific tokens. */ function pathauto_token_values($type, $object = NULL) { if (module_exists('taxonomy')) { if ($type == 'taxonomy' || $type == 'node' || $type == 'all') { _pathauto_include(); switch ($type) { case 'node': // Get the bookpathalias token $values['bookpathalias'] = ''; // We're on a node and it's a book and it has a parent? Get the book path alias. if (module_exists('book') && !empty($object->book['menu_name'])) { for ($i = 1; $i < 10; $i++) { $second_child = $i + 2; if ($object->book["p$second_child"] === 0) { $parent_node = db_result(db_query('SELECT link_path FROM {menu_links} WHERE mlid = %d', $object->book["p$i"])); break; } } $values['bookpathalias'] = drupal_get_path_alias($parent_node); } // Get taxonomy related data. $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.vid = %d ORDER BY v.weight, t.weight, t.name", $object->vid, 0, 1)); $category = 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.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1)); $category->vid = $vid; // In the realm of nodes these are terms, in the realm of Taxonomy, cats $label = 'term'; case 'taxonomy': default: if (!isset($category)) { $category = $object; } if (!isset($label)) { $label = 'cat'; } if (isset($category->tid)) { $separator = variable_get('pathauto_separator', '-'); $parents = taxonomy_get_parents_all($category->tid); array_shift($parents); $catpath = ''; $catpath_raw = ''; foreach ($parents as $parent) { // Replace any / characters in individual terms which might create confusing URLs $catpath = pathauto_cleanstring(check_plain(preg_replace('/\//', '', $parent->name))) .'/'. $catpath; $catpath_raw = pathauto_cleanstring(preg_replace('/\//', '', $parent->name)) .'/'. $catpath_raw; } $values[$label .'path'] = $catpath .'/'. check_plain($category->name); $values[$label .'path-raw'] = $catpath_raw .'/'. $category->name; $values[$label .'alias'] = drupal_get_path_alias('taxonomy/term/'. $category->tid); if (!strncasecmp($values[$label .'alias'], 'taxonomy', 8)) { $values[$label .'alias'] = check_plain($category->name); } } else { // Provide some defaults if they aren't set. $values[$label .'path'] = ''; $values[$label .'path-raw'] = ''; $values[$label .'alias'] = ''; } } return $values; } } } /** * Implementation of hook_token_list() for Pathauto specific tokens. */ function pathauto_token_list($type = 'all') { $tokens = array(); if (module_exists('taxonomy')) { if ($type == 'taxonomy' || $type == 'all') { $tokens['taxonomy']['catpath'] = t('As [cat], but including its supercategories separated by /.'); $tokens['taxonomy']['catpath-raw'] = t('As [cat-raw], but including its supercategories separated by /. WARNING - raw user input.'); $tokens['taxonomy']['catalias'] = t('URL alias for the term.'); } if ($type == 'node' || $type == 'all') { $tokens['node']['termpath'] = t('As [term], but including its supercategories separated by /.'); $tokens['node']['termpath-raw'] = t('As [term-raw], but including its supercategories separated by /. WARNING - raw user input.'); $tokens['node']['termalias'] = t('URL alias for the term.'); } } if (module_exists('book')) { if ($type == 'node' || $type == 'all') { $tokens['node']['bookpathalias'] = t('URL alias for the parent book.'); } } return $tokens; } /** * Implementation of hook_path_alias_types(). * * Used primarily by the bulk delete form. */ function pathauto_path_alias_types() { $objects = array('user/' => t('users'), 'node/' => t('content')); if (module_exists('blog')) { $objects['blog/'] = t('user blogs'); } if (module_exists('taxonomy')) { $objects['taxonomy/'] = t('vocabularies and terms'); } if (module_exists('taxonomy')) { $objects['user/%/track'] = t('user trackers'); } if (module_exists('forum')) { $objects['forum/%'] = t('forums'); } return $objects; } //============================================================================== // Some node related functions. /** * Implementation of hook_nodeapi(). */ function pathauto_nodeapi(&$node, $op, $teaser, $page) { if (module_exists('path')) { switch ($op) { case 'presave': // About to be saved (before insert/update) if (!empty($node->pathauto_perform_alias) && isset($node->old_alias) && $node->path == '' && $node->old_alias != '') { /** * There was an old alias, but when pathauto_perform_alias was checked * the javascript disabled the textbox which led to an empty value being * submitted. Restoring the old path-value here prevents the Path module * from deleting any old alias before Pathauto gets control. */ $node->path = $node->old_alias; } break; case 'insert': case 'update': _pathauto_include(); // Get the specific pattern or the default if (variable_get('language_content_type_'. $node->type, 0)) { $pattern = trim(variable_get('pathauto_node_'. $node->type .'_'. $node->language .'_pattern', FALSE)); } if (empty($pattern)) { $pattern = trim(variable_get('pathauto_node_'. $node->type .'_pattern', FALSE)); if (empty($pattern)) { $pattern = trim(variable_get('pathauto_node_pattern', FALSE)); } } // Only do work if there's a pattern if ($pattern) { // Only create an alias if the checkbox was not provided or if the checkbox was provided and is checked if (!isset($node->pathauto_perform_alias) || $node->pathauto_perform_alias) { $placeholders = pathauto_get_placeholders('node', $node); $src = "node/$node->nid"; $alias = pathauto_create_alias('node', $op, $placeholders, $src, $node->nid, $node->type, $node->language); } } break; case 'delete': path_set_alias('node/'. $node->nid); path_set_alias('node/'. $node->nid .'/feed'); break; default: break; } } } /** * Implementation of hook_form_alter(). * * This allows alias creators to override Pathauto and specify their * own aliases (Pathauto will be invisible to other users). Inserted * into the path module's fieldset in the node form. */ function pathauto_form_alter(&$form, $form_state, $form_id) { // Process only node forms. if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) { $node = $form['#node']; $pattern = FALSE; // Find if there is an automatic alias pattern for this node type. if (isset($form['language'])) { $language = isset($form['language']['#value']) ? $form['language']['#value'] : $form['language']['#default_value']; $pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_'. $language .'_pattern', '')); } if (!$pattern) { $pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_pattern', '')); if (!$pattern) { $pattern = trim(variable_get('pathauto_node_pattern', '')); } } // If there is a pattern, show the automatic alias checkbox. if ($pattern) { if (!isset($node->pathauto_perform_alias)) { if (isset($node->nid)) { // If this is not a new node, compare it's current alias to the // alias that would be genereted by pathauto. If they are the same, // then keep the automatic alias enabled. _pathauto_include(); $placeholders = pathauto_get_placeholders('node', $node); $pathauto_alias = pathauto_create_alias('node', 'return', $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language); $node->pathauto_perform_alias = isset($node->path) && $node->path == $pathauto_alias; } else { // If this is a new node, enable the automatic alias. $node->pathauto_perform_alias = TRUE; } } // Add JavaScript that will disable the path textfield when the automatic // alias checkbox is checked. drupal_add_js(drupal_get_path('module', 'pathauto') .'/pathauto.js'); $form['path']['pathauto_perform_alias'] = array( '#type' => 'checkbox', '#title' => t('Automatic alias'), '#default_value' => $node->pathauto_perform_alias, '#description' => t('An alias will be generated for you. If you wish to create your own alias below, uncheck this option.'), '#weight' => -1, ); if (user_access('administer pathauto')) { $form['path']['pathauto_perform_alias']['#description'] .= ' '. t('To control the format of the generated aliases, see the automated alias settings.', array('@pathauto' => url('admin/build/path/pathauto'))); } if ($node->pathauto_perform_alias && !empty($node->old_alias) && empty($node->path)) { $form['path']['path']['#default_value'] = $node->old_alias; $node->path = $node->old_alias; } // For Pathauto to remember the old alias and prevent the Path-module from deleteing it when Pathauto wants to preserve it if (isset($node->path)) { $form['path']['old_alias'] = array( '#type' => 'value', '#value' => $node->path, ); } } } } /** * Implementation of hook_node_operations(). */ function pathauto_node_operations() { $operations = array( 'update_alias' => array( 'label' => t('Update path alias'), 'callback' => 'pathauto_node_operations_update', ), ); return $operations; } /** * Callback function for updating node aliases. * * @param $nodes * Array of node nid's. */ function pathauto_node_operations_update($nodes) { _pathauto_include(); foreach ($nodes as $nid) { $node = node_load($nid); $placeholders = pathauto_get_placeholders('node', $node); pathauto_create_alias('node', 'bulkupdate', $placeholders, "node/$node->nid", $node->nid, $node->type); } } //============================================================================== // Taxonomy related functions. /** * Implementation of hook_taxonomy(). */ function pathauto_taxonomy($op, $type, $object = NULL) { switch ($type) { case 'term': switch ($op) { case 'insert': case 'update': _pathauto_include(); // Use the category info to automatically create an alias $category = (object) $object; if ($category->name) { $count = _taxonomy_pathauto_alias($category, $op); } // For all children generate new alias (important if [catpath] used) foreach (taxonomy_get_tree($category->vid, $category->tid) as $subcategory) { $count = _taxonomy_pathauto_alias($subcategory, $op); } break; case 'delete': // If the category is deleted, remove the path aliases $category = (object) $object; path_set_alias('taxonomy/term/'. $category->tid); path_set_alias(taxonomy_term_path($category)); path_set_alias('forum/'. $category->tid); path_set_alias('taxonomy/term/'. $category->tid .'/0/feed'); break; default: break; } break; default: break; } } //============================================================================== // User related functions. /** * Implementation of hook_user() for users, trackers, and blogs. */ function pathauto_user($op, &$edit, &$user, $category = FALSE) { switch ($op) { case 'insert': case 'update': _pathauto_include(); // Use the username to automatically create an alias $pathauto_user = (object) array_merge((array) $user, $edit); if ($user->name) { $placeholders = pathauto_get_placeholders('user', $pathauto_user); $src = 'user/'. $user->uid; $alias = pathauto_create_alias('user', $op, $placeholders, $src, $user->uid); if (module_exists('blog')) { $new_user = drupal_clone($user); if ($category == 'account') { $new_user->roles = isset($edit['roles']) ? $edit['roles'] : array(); $new_user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; // Add this back } if (user_access('create blog entries', $new_user)) { $src = 'blog/'. $user->uid; $alias = pathauto_create_alias('blog', $op, $placeholders, $src, $user->uid); } else { path_set_alias('blog/'. $user->uid); path_set_alias('blog/'. $user->uid .'/feed'); } } if (module_exists('tracker')) { $src = 'user/'. $user->uid .'/track'; $alias = pathauto_create_alias('tracker', $op, $placeholders, $src, $user->uid); } } break; case 'delete': // If the user is deleted, remove the path aliases $user = (object) $user; path_set_alias('user/'. $user->uid); // They may have enabled these modules and/or feeds when the user was created, so let's try to delete all of them path_set_alias('blog/'. $user->uid); path_set_alias('blog/'. $user->uid .'/feed'); path_set_alias('user/'. $user->uid .'/track'); path_set_alias('user/'. $user->uid .'/track/feed'); break; default: break; } }