'pathauto'); $info['path_alias_types'] = array('group' => 'pathauto'); return $info; } /** * Implements hook_module_implements_alter(). * * Adds pathauto support for core modules. */ function pathauto_module_implements_alter(&$implementations, $hook) { $hooks = pathauto_hook_info(); if (isset($hooks[$hook])) { $modules = array('node', 'taxonomy', 'user', 'forum', 'blog'); foreach ($modules as $module) { if (module_exists($module)) { $implementations[$module] = TRUE; } } // Move pathauto.module to get included first since it is responsible for // other modules. unset($implementations['pathauto']); $implementations = array_merge(array('pathauto' => 'pathauto'), $implementations); } } /** * Implements hook_help(). */ function pathauto_help($path, $arg) { switch ($path) { case 'admin/help#pathauto': module_load_include('inc', 'pathauto'); $output = '

' . t('About') . '

'; $output .= '

' . t('Provides a mechanism for modules to automatically generate aliases for the content they manage.') . '

'; $output .= '

' . t('Settings') . '

'; $output .= '
'; $output .= '
' . t('Maximum alias and component length') . '
'; $output .= '
' . t('The maximum alias length and maximum component length values default to 100 and have a limit of @max from Pathauto. This length is limited by the length of the "alias" column of the url_alias database table. The default database schema for this column is @max. If you set a length that is equal to that of the one set in the "alias" 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 "alias" 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.', array('@max' => _pathauto_get_schema_alias_maxlength())) . '
'; $output .= '
'; return $output; } } /** * Implements hook_permission(). */ function pathauto_permission() { return array( 'administer pathauto' => array( 'title' => t('Administer pathauto'), 'description' => t('Allows a user to configure patterns for automated aliases and bulk delete URL-aliases.'), ), 'notify of path changes' => array( 'title' => t('Notify of Path Changes'), 'description' => t('Determines whether or not users are notified.'), ), ); } /** * Implements hook_menu(). */ function pathauto_menu() { $items['admin/config/search/path/patterns'] = array( 'title' => 'Patterns', 'page callback' => 'drupal_get_form', 'page arguments' => array('pathauto_patterns_form'), 'access arguments' => array('administer pathauto'), 'type' => MENU_LOCAL_TASK, 'weight' => 10, 'file' => 'pathauto.admin.inc', ); $items['admin/config/search/path/settings'] = array( 'title' => 'Settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('pathauto_settings_form'), 'access arguments' => array('administer pathauto'), 'type' => MENU_LOCAL_TASK, 'weight' => 20, 'file' => 'pathauto.admin.inc', ); $items['admin/config/search/path/update_bulk'] = array( 'title' => 'Bulk update', 'page callback' => 'drupal_get_form', 'page arguments' => array('pathauto_bulk_update_form'), 'access arguments' => array('administer url aliases'), 'type' => MENU_LOCAL_TASK, 'weight' => 30, 'file' => 'pathauto.admin.inc', ); $items['admin/config/search/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, 'weight' => 40, 'file' => 'pathauto.admin.inc', ); return $items; } /** * Load an URL alias pattern by entity, bundle, and language. * * @param $entity * An entity (e.g. node, taxonomy, user, etc.) * @param $bundle * A bundle (e.g. node type, vocabulary ID, etc.) * @param $language * A language code, defaults to the LANGUAGE_NONE constant. */ function pathauto_pattern_load_by_entity($entity, $bundle = '', $language = LANGUAGE_NONE) { $patterns = &drupal_static(__FUNCTION__, array()); $pattern_id = "$entity:$bundle:$language"; if (!isset($patterns[$pattern_id])) { $variables = array(); if ($language != LANGUAGE_NONE) { $variables[] = "pathauto_{$entity}_{$bundle}_{$language}_pattern"; } if ($bundle) { $variables[] = "pathauto_{$entity}_{$bundle}_pattern"; } $variables[] = "pathauto_{$entity}_pattern"; foreach ($variables as $variable) { if ($pattern = trim(variable_get($variable, ''))) { break; } } $patterns[$pattern_id] = $pattern; } return $patterns[$pattern_id]; } /** * Return the proper SQL to perform cross-db and field-type concatenation. * * @return * A string of SQL with the concatenation. */ function _pathauto_sql_concat() { $args = func_get_args(); switch (db_driver()) { case 'mysql': return 'CONCAT(' . implode(', ', $args) . ')'; case 'mssql': return '(' . implode(' + ', $args) . ')'; default: // The ANSI standard of concatentation uses the double-pipe. return '(' . implode(' || ', $args) . ')'; } } /** * Delete multiple URL aliases. * * Intent of this is to abstract a potential path_delete_multiple() function * for Drupal 7 or 8. * * @param $pids * An array of path IDs to delete. */ function pathauto_path_delete_multiple($pids) { foreach ($pids as $pid) { path_delete(array('pid' => $pid)); } } /** * Delete an URL alias and any of its sub-paths. * * Given a source like 'node/1' this function will delete any alias that have * that specific source or any sources that match 'node/1/%'. * * @param $source * An string with a source URL path. */ function pathauto_path_delete_all($source) { $sql = "SELECT pid FROM {url_alias} WHERE source = :source OR source LIKE :source_wildcard"; $pids = db_query($sql, array(':source' => $source, ':source_wildcard' => $source . '/%'))->fetchCol(); if ($pids) { pathauto_path_delete_multiple($pids); } } /** * Delete an entity URL alias and any of its sub-paths. * * This function also checks to see if the default entity URI is different from * the current entity URI and will delete any of the default aliases. * * @param $entity_type * A string with the entity type. * @param $entity * An entity object. * @param $default_uri * The optional default uri path for the entity. */ function pathauto_entity_path_delete_all($entity_type, $entity, $default_uri = NULL) { $uri = entity_uri($entity_type, $entity); pathauto_path_delete_all($uri['path']); if (isset($default_uri) && $uri['path'] != $default_uri) { pathauto_path_delete_all($default_uri); } } //============================================================================== // Some node related functions. /** * Implements hook_node_presave(). */ function pathauto_node_presave($node) { // About to be saved (before insert/update) if (!empty($node->path['pathauto_perform_alias']) && isset($node->path['old_alias']) && $node->path['alias'] == '' && $node->path['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['alias'] = $node->path['old_alias']; } } /** * Implements hook_node_insert(). */ function pathauto_node_insert($node) { if (!isset($node->path['pathauto_perform_alias']) || !empty($node->path['pathauto_perform_alias'])) { pathauto_node_update_alias($node, 'insert'); } } /** * Implements hook_node_update(). */ function pathauto_node_update($node) { if (!isset($node->path['pathauto_perform_alias']) || !empty($node->path['pathauto_perform_alias'])) { pathauto_node_update_alias($node, 'update'); } } /** * Implements hook_node_delete(). */ function pathauto_node_delete($node) { pathauto_entity_path_delete_all('node', $node, "node/{$node->nid}"); } /** * Implements 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 (!empty($form['#node_edit_form'])) { $node = $form['#node']; // Find if there is an automatic alias pattern for this node type. $language = isset($node->language) ? $node->language : LANGUAGE_NONE; $pattern = pathauto_pattern_load_by_entity('node', $node->type, $language); // If there is a pattern, show the automatic alias checkbox. if ($pattern) { if (!isset($node->pathauto_perform_alias)) { if (!empty($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. module_load_include('inc', 'pathauto'); $uri = entity_uri('node', $node); $path = drupal_get_path_alias($uri['path'], $language); $pathauto_alias = pathauto_create_alias('node', 'return', $uri['path'], array('node' => $node), $node->type, $node->language); $node->pathauto_perform_alias = $path != $uri['path'] && $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. $form['path']['alias']['#states']['!enabled']['input[name="path[pathauto_perform_alias]"]'] = array('checked' => TRUE); // Override path.module's vertical tabs summary. $form['path']['#attached']['js'] = array( 'vertical-tabs' => 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 URL alias patterns.', array('@url-patterns' => url('admin/config/search/path/patterns'))); } if ($node->pathauto_perform_alias && !empty($node->old_alias) && empty($path['alias'])) { $form['path']['alias']['#default_value'] = $node->old_alias; $path['alias'] = $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 (!empty($path['alias'])) { $form['path']['old_alias'] = array( '#type' => 'value', '#value' => $path['alias'], ); } } } } /** * Implements hook_node_operations(). */ function pathauto_node_operations() { $operations['pathauto_update_alias'] = array( 'label' => t('Update URL alias'), 'callback' => 'pathauto_node_update_alias_multiple', 'callback arguments' => array('bulkupdate', array('message' => TRUE)), ); return $operations; } /** * Update the URL aliases for an individual node. * * @param $node * A node object. * @param $op * Operation being performed on the node ('insert', 'update' or 'bulkupdate'). * @param $options * An optional array of additional options. */ function pathauto_node_update_alias(stdClass $node, $op, array $options = array()) { // Skip processing if the term has no pattern. $language = isset($node->language) ? $node->language : LANGUAGE_NONE; if (!pathauto_pattern_load_by_entity('node', $node->type, $language)) { return; } module_load_include('inc', 'pathauto'); $uri = entity_uri('node', $node); pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $language); } /** * Update the URL aliases for multiple nodes. * * @param $nids * An array of node IDs. * @param $op * Operation being performed on the nodes ('insert', 'update' or * 'bulkupdate'). * @param $options * An optional array of additional options. */ function pathauto_node_update_alias_multiple(array $nids, $op, array $options = array()) { $options += array('message' => FALSE); $nodes = node_load_multiple($nids); foreach ($nodes as $node) { pathauto_node_update_alias($node, $op, $options); } if (!empty($options['message'])) { drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.')); } } //============================================================================== // Taxonomy related functions. /** * Implements hook_taxonomy_term_insert(). */ function pathauto_taxonomy_term_insert($term) { pathauto_taxonomy_term_update_alias($term, 'insert'); } /** * Implements hook_taxonomy_term_update(). */ function pathauto_taxonomy_term_update($term) { pathauto_taxonomy_term_update_alias($term, 'update', array('alias children' => TRUE)); } /** * Implements hook_taxonomy_term_delete(). */ function pathauto_taxonomy_term_delete($term) { pathauto_entity_path_delete_all('taxonomy_term', $term, "taxonomy/term/{$term->tid}"); } /** * Update the URL aliases for an individual taxonomy term. * * @param $term * A taxonomy term object. * @param $op * Operation being performed on the term ('insert', 'update' or 'bulkupdate'). * @param $options * An optional array of additional options. */ function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options = array()) { $options += array('alias children' => FALSE); $module = 'taxonomy'; if ($term->vid == variable_get('forum_nav_vocabulary', '')) { if (module_exists('forum')) { $module = 'forum'; } else { return; } } // Check that the term has its bundle, which is the vocabulary's machine name. if (!isset($term->vocabulary_machine_name)) { $vocabulary = taxonomy_vocabulary_load($term->vid); $term->vocabulary_machine_name = $vocabulary->machine_name; } // Skip processing if the term has no pattern. if (!pathauto_pattern_load_by_entity($module, $term->vocabulary_machine_name)) { return; } module_load_include('inc', 'pathauto'); $uri = entity_uri('taxonomy_term', $term); pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name); if (!empty($options['alias children'])) { // For all children generate new alias. $options['alias children'] = FALSE; foreach (taxonomy_get_tree($term->vid, $term->tid) as $subterm) { pathauto_taxonomy_term_update_alias($subterm, $op, $options); } } } /** * Update the URL aliases for multiple taxonomy terms. * * @param $tids * An array of term IDs. * @param $op * Operation being performed on the nodes ('insert', 'update' or * 'bulkupdate'). * @param $options * An optional array of additional options. */ function pathauto_taxonomy_term_update_alias_multiple(array $tids, $op, array $options = array()) { $options += array('message' => FALSE); $terms = taxonomy_term_load_multiple($tids); foreach ($terms as $term) { pathauto_taxonomy_term_update_alias($term, $op, $options); } if (!empty($options['message'])) { drupal_set_message(format_plural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.')); } } //============================================================================== // User related functions. For users, trackers, and blogs. /** * Implements hook_user_insert(). */ function pathauto_user_insert(&$edit, $account, $category) { pathauto_user_update_alias($account, 'insert'); } /** * Implements hook_user_update(). */ function pathauto_user_update(&$edit, $account, $category) { pathauto_user_update_alias($account, 'update'); } /** * Implements hook_user_cancel(). */ function pathauto_user_cancel($edit, $account, $method) { switch ($method) { case 'user_cancel_block': case 'user_cancel_block_unpublish': // Don't remove aliases because the user may become active again later. break; case 'user_cancel_reassign': case 'user_cancel_delete': // Do remove aliases since the account will be deleted. pathauto_entity_path_delete_all('user', $account, "user/{$account->uid}"); pathauto_path_delete_all("blog/{$account->uid}"); break; } } /** * Implements hook_user_operations(). */ function pathauto_user_operations() { $operations['pathauto_update_alias'] = array( 'label' => t('Update URL alias'), 'callback' => 'pathauto_user_update_alias_multiple', 'callback arguments' => array('bulkupdate', array('message' => TRUE)), ); return $operations; } /** * Update the URL aliases for an individual user account. * * @param $account * A user account object. * @param $op * Operation being performed on the account ('insert', 'update' or * 'bulkupdate'). * @param $options * An optional array of additional options. */ function pathauto_user_update_alias(stdClass $account, $op, array $options = array()) { $options += array('alias blog' => module_exists('blog')); // Skip processing if the account has no pattern. if (!pathauto_pattern_load_by_entity('user')) { return; } module_load_include('inc', 'pathauto'); $uri = entity_uri('user', $account); pathauto_create_alias('user', $op, $uri['path'], array('user' => $account)); // Because blogs are also associated with users, also generate the blog paths. if (!empty($options['alias blog'])) { pathauto_blog_update_alias($account, $op); } } /** * Update the URL aliases for multiple user accounts. * * @param $uids * An array of user account IDs. * @param $op * Operation being performed on the accounts ('insert', 'update' or * 'bulkupdate'). * @param $options * An optional array of additional options. */ function pathauto_user_update_alias_multiple(array $uids, $op, array $options = array()) { $options += array('message' => FALSE); $accounts = user_load_multiple($uids); foreach ($accounts as $account) { pathauto_user_update_alias($account, $op, $options); } if (!empty($options['message'])) { drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.')); } } /** * Update the blog URL aliases for an individual user account. * * @param $account * A user account object. * @param $op * Operation being performed on the blog ('insert', 'update' or * 'bulkupdate'). * @param $options * An optional array of additional options. */ function pathauto_blog_update_alias(stdClass $account, $op, array $options = array()) { // Skip processing if the blog has no pattern. if (!pathauto_pattern_load_by_entity('blog')) { return; } module_load_include('inc', 'pathauto'); if (node_access('create', 'blog', $account)) { pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account)); } else { pathauto_path_delete_all("blog/{$account->uid}"); } }