2006-11-23 19:46:24 +00:00
<?php
// $Id$
/**
* @file
* Enables users to rename URLs.
*/
/**
2009-05-27 18:34:03 +00:00
* Implement hook_help().
2006-11-23 19:46:24 +00:00
*/
2007-06-30 19:46:58 +00:00
function path_help($path, $arg) {
switch ($path) {
2006-11-23 19:46:24 +00:00
case 'admin/help#path':
2008-04-14 17:48:46 +00:00
$output = '<p>' . t('The path module allows you to specify aliases for Drupal URLs. Such aliases improve readability of URLs for your users and may help internet search engines to index your content more effectively. More than one alias may be created for a given page.') . '</p>';
$output .= '<p>' . t('Some examples of URL aliases are:') . '</p>';
$output .= '<ul><li>' . t('%alias for the path %path', array('%alias' => 'login', '%path' => 'user/login')) . '</li>';
$output .= '<li>' . t('%alias for the path %path', array('%alias' => 'store', '%path' => 'image/tid/16')) . '</li>';
$output .= '<li>' . t('%alias for the path %path', array('%alias' => 'store/products/whirlygigs', '%path' => 'taxonomy/term/7+19+20+21')) . '</li>';
$output .= '<li>' . t('%alias for the path %path', array('%alias' => 'contact', '%path' => 'node/3')) . '</li></ul>';
2009-04-22 15:53:00 +00:00
$output .= '<p>' . t('The path module enables appropriately permissioned users to specify an optional alias in all node input and editing forms, and provides an interface to view and edit all URL aliases. The two permissions related to URL aliasing are <em>administer url aliases</em> and <em>create url aliases</em>.') . ' </p>';
$output .= '<p>' . t('This module also provides user-defined mass URL aliasing capabilities, which is useful if you wish to uniformly use URLs different from the default. For example, you may want to have your URLs presented in a different language. Access to the Drupal source code on the web server is required to set up mass URL aliasing.') . ' </p>';
2008-04-14 17:48:46 +00:00
$output .= '<p>' . t('For more information, see the online handbook entry for <a href="@path">Path module</a>.', array('@path' => 'http://drupal.org/handbook/modules/path/')) . '</p>';
2006-11-23 19:46:24 +00:00
return $output;
case 'admin/build/path':
2008-04-14 17:48:46 +00:00
return '<p>' . t("Drupal provides complete control over URLs through aliasing, which is often used to make URLs more readable or easy to remember. For example, the alias 'about' may be mapped onto the post at the system path 'node/1', creating a more meaningful URL. Each system path can have multiple aliases.") . '</p>';
2006-11-23 19:46:24 +00:00
case 'admin/build/path/add':
2008-04-14 17:48:46 +00:00
return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
2006-11-23 19:46:24 +00:00
}
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_menu().
2006-11-23 19:46:24 +00:00
*/
2007-01-24 14:48:36 +00:00
function path_menu() {
$items['admin/build/path'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'URL aliases',
'description' => "Change your site's URL paths by aliasing them.",
2007-05-07 12:29:20 +00:00
'page callback' => 'path_admin_overview',
2007-01-24 14:48:36 +00:00
'access arguments' => array('administer url aliases'),
);
$items['admin/build/path/edit'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Edit alias',
2007-08-12 16:34:56 +00:00
'page callback' => 'path_admin_edit',
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer url aliases'),
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
$items['admin/build/path/delete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Delete alias',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('path_admin_delete_confirm'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer url aliases'),
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
$items['admin/build/path/list'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'List',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/path/add'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add alias',
2007-08-12 16:34:56 +00:00
'page callback' => 'path_admin_edit',
2007-01-24 14:48:36 +00:00
'access arguments' => array('administer url aliases'),
'type' => MENU_LOCAL_TASK,
);
2006-11-23 19:46:24 +00:00
return $items;
}
/**
* Post-confirmation; delete an URL alias.
*/
function path_admin_delete($pid = 0) {
2009-05-10 16:50:19 +00:00
db_delete('url_alias')
->condition('pid', $pid)
->execute();
2006-11-23 19:46:24 +00:00
drupal_set_message(t('The alias has been deleted.'));
}
/**
* Set an aliased path for a given Drupal path, preventing duplicates.
*/
2007-03-26 01:32:22 +00:00
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
2007-11-30 16:04:05 +00:00
$path = urldecode($path);
$alias = urldecode($alias);
// First we check if we deal with an existing alias and delete or modify it based on pid.
if ($pid) {
// An existing alias.
if (!$path || !$alias) {
// Delete the alias based on pid.
2009-05-10 16:50:19 +00:00
db_delete('url_alias')
->condition('pid', $pid)
->execute();
2007-11-30 16:04:05 +00:00
}
else {
// Update the existing alias.
2009-05-10 16:50:19 +00:00
db_update('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language))
->condition('pid', $pid)
->execute();
2007-11-30 16:04:05 +00:00
}
2006-11-23 19:46:24 +00:00
}
2008-10-12 04:30:09 +00:00
elseif ($path && $alias) {
2007-11-30 16:04:05 +00:00
// Check for existing aliases.
if ($alias == drupal_get_path_alias($path, $language)) {
// There is already such an alias, neutral or in this language.
// Update the alias based on alias; setting the language if not yet done.
2009-05-10 16:50:19 +00:00
db_update('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language
))
->condition('dst', $alias)
->execute();
2006-11-23 19:46:24 +00:00
}
2007-03-12 13:01:10 +00:00
else {
2007-11-30 16:04:05 +00:00
// A new alias. Add it to the database.
2009-05-10 16:50:19 +00:00
db_insert('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language,
))
->execute();
2006-11-23 19:46:24 +00:00
}
2007-11-30 16:04:05 +00:00
}
else {
// Delete the alias.
if ($alias) {
2009-05-10 16:50:19 +00:00
db_delete('url_alias')
->condition('dst', $alias)
->execute();
2006-11-23 19:46:24 +00:00
}
2007-11-30 16:04:05 +00:00
else {
2009-05-10 16:50:19 +00:00
db_delete('url_alias')
->condition('src', $path)
->execute();
2007-12-08 14:06:23 +00:00
}
2006-11-23 19:46:24 +00:00
}
2007-11-30 16:04:05 +00:00
drupal_clear_path_cache();
2006-11-23 19:46:24 +00:00
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_validate().
2006-11-23 19:46:24 +00:00
*/
2009-03-08 04:25:07 +00:00
function path_node_validate($node, $form) {
2008-10-06 12:55:56 +00:00
if (user_access('create url aliases') || user_access('administer url aliases')) {
if (isset($node->path)) {
$language = isset($node->language) ? $node->language : '';
$node->path = trim($node->path);
2009-05-10 16:50:19 +00:00
$has_alias = db_query("SELECT COUNT(dst) FROM {url_alias} WHERE src <> :src AND dst = :dst AND language = :language", array(
':src' => "node/$node->nid",
':dst' => $node->path,
':language' => $language,
))
->fetchField();
if ($has_alias) {
2008-10-06 12:55:56 +00:00
form_set_error('path', t('The path is already in use.'));
}
}
}
}
2006-11-23 19:46:24 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_load().
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function path_node_load($nodes, $types) {
2008-12-05 22:18:46 +00:00
foreach ($nodes as $node) {
$language = isset($node->language) ? $node->language : '';
$path = 'node/' . $node->nid;
$alias = drupal_get_path_alias($path, $language);
if ($path != $alias) {
$node->path = $alias;
}
2008-10-06 12:55:56 +00:00
}
}
2006-11-23 19:46:24 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_insert().
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function path_node_insert($node) {
2008-10-06 12:55:56 +00:00
if (user_access('create url aliases') || user_access('administer url aliases')) {
$language = isset($node->language) ? $node->language : '';
// Don't try to insert if path is NULL. We may have already set
// the alias ahead of time.
if (isset($node->path)) {
path_set_alias('node/' . $node->nid, $node->path, NULL, $language);
}
}
}
2006-11-23 19:46:24 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_update().
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function path_node_update($node) {
2008-10-06 12:55:56 +00:00
if (user_access('create url aliases') || user_access('administer url aliases')) {
$language = isset($node->language) ? $node->language : '';
path_set_alias('node/' . $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
}
}
2006-11-23 19:46:24 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_delete().
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function path_node_delete($node) {
2008-10-06 12:55:56 +00:00
if (user_access('create url aliases') || user_access('administer url aliases')) {
$language = isset($node->language) ? $node->language : '';
$path = 'node/' . $node->nid;
if (drupal_get_path_alias($path) != $path) {
path_set_alias($path);
2006-11-23 19:46:24 +00:00
}
}
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_form_alter().
2006-11-23 19:46:24 +00:00
*/
2007-05-28 06:08:47 +00:00
function path_form_alter(&$form, $form_state, $form_id) {
2008-09-27 20:37:01 +00:00
if (!empty($form['#node_edit_form'])) {
2007-01-31 15:49:26 +00:00
$path = isset($form['#node']->path) ? $form['#node']->path : NULL;
2006-11-23 19:46:24 +00:00
$form['path'] = array(
'#type' => 'fieldset',
'#title' => t('URL path settings'),
'#collapsible' => TRUE,
'#collapsed' => empty($path),
2009-04-11 22:19:46 +00:00
'#group' => 'additional_settings',
'#attached_js' => array(drupal_get_path('module', 'path') . '/path.js'),
2006-11-23 19:46:24 +00:00
'#access' => user_access('create url aliases'),
'#weight' => 30,
);
$form['path']['path'] = array(
'#type' => 'textfield',
2009-03-30 05:30:39 +00:00
'#title' => t('URL alias'),
2006-11-23 19:46:24 +00:00
'#default_value' => $path,
2009-06-11 04:59:26 +00:00
'#maxlength' => 255,
2006-11-23 19:46:24 +00:00
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Optionally specify an alternative URL by which this node can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
);
if ($path) {
$form['path']['pid'] = array(
'#type' => 'value',
2009-05-10 16:50:19 +00:00
'#value' => db_query("SELECT pid FROM {url_alias} WHERE dst = :dst AND language = :language", array(
':dst' => $path,
':language' => $form['#node']->language
))
->fetchField(),
2006-11-23 19:46:24 +00:00
);
}
}
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_perm().
2006-11-23 19:46:24 +00:00
*/
function path_perm() {
2008-02-20 13:46:43 +00:00
return array(
2008-10-09 15:15:55 +00:00
'administer url aliases' => array(
'title' => t('Administer URL aliases'),
'description' => t('Manage URL aliases across the entire website.'),
),
'create url aliases' => array(
'title' => t('Create URL aliases'),
'description' => t('Manage URL aliases on content.'),
),
2008-02-20 13:46:43 +00:00
);
2006-11-23 19:46:24 +00:00
}
/**
* Fetch a specific URL alias from the database.
*/
function path_load($pid) {
2009-05-10 16:50:19 +00:00
return db_query('SELECT * FROM {url_alias} WHERE pid = :pid', array(':pid' => $pid))->fetchAssoc();
2006-11-23 19:46:24 +00:00
}