path.module 8.46 KB
Newer Older
Steven Wittens's avatar
Steven Wittens committed
1
2
3
4
5
6
7
8
9
10
11
<?php
// $Id$

/**
 * @file
 * Enables users to rename URLs.
 */

/**
 * Implementation of hook_help().
 */
12
13
function path_help($path, $arg) {
  switch ($path) {
Steven Wittens's avatar
Steven Wittens committed
14
15
16
17
18
19
20
21
22
23
    case 'admin/help#path':
      $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 .= t('<p>Some examples of URL aliases are:</p>
<ul>
<li>user/login =&gt; login</li>
<li>image/tid/16 =&gt; store</li>
<li>taxonomy/term/7+19+20+21 =&gt; store/products/whirlygigs</li>
<li>node/3 =&gt; contact</li>
</ul>
');
24
      $output .= '<p>'. t('The path module enables an extra field for aliases in all node input and editing forms (when users have the appropriate permissions). It also provides an interface to view and edit all URL aliases. The two permissions related to URL aliasing are "administer url aliases" and "create url aliases". ') .'</p>';
Steven Wittens's avatar
Steven Wittens committed
25
26
27
28
      $output .= '<p>'. t('This module also comes with 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 these kinds of aliases. ') .'</p>';
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@path">Path page</a>.', array('@path' => 'http://drupal.org/handbook/modules/path/')) .'</p>';
      return $output;
    case 'admin/build/path':
29
      return '<p>'. t("Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.") .'</p>';
Steven Wittens's avatar
Steven Wittens committed
30
    case 'admin/build/path/add':
31
      return '<p>'. t('Enter the path you wish to create the alias for, followed by the name of the new alias.') .'</p>';
Steven Wittens's avatar
Steven Wittens committed
32
33
34
35
36
37
  }
}

/**
 * Implementation of hook_menu().
 */
38
39
function path_menu() {
  $items['admin/build/path'] = array(
40
41
    'title' => 'URL aliases',
    'description' => "Change your site's URL paths by aliasing them.",
42
    'page callback' => 'path_admin_overview',
43
    'access arguments' => array('administer url aliases'),
44
    'file' => 'path.admin.inc',
45
46
  );
  $items['admin/build/path/edit'] = array(
47
    'title' => 'Edit alias',
48
49
50
    'page callback' => 'drupal_get_form',
    'page arguments' => array('path_admin_edit'),
    'type' => MENU_CALLBACK,
51
    'file' => 'path.admin.inc',
52
53
  );
  $items['admin/build/path/delete'] = array(
54
    'title' => 'Delete alias',
55
56
57
    'page callback' => 'drupal_get_form',
    'page arguments' => array('path_admin_delete_confirm'),
    'type' => MENU_CALLBACK,
58
    'file' => 'path.admin.inc',
59
60
  );
  $items['admin/build/path/list'] = array(
61
    'title' => 'List',
62
63
64
65
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/build/path/add'] = array(
66
    'title' => 'Add alias',
67
68
69
70
    'page callback' => 'drupal_get_form',
    'page arguments' => array('path_admin_edit'),
    'access arguments' => array('administer url aliases'),
    'type' => MENU_LOCAL_TASK,
71
    'file' => 'path.admin.inc',
72
  );
Steven Wittens's avatar
Steven Wittens committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

  return $items;
}

/**
 * Post-confirmation; delete an URL alias.
 */
function path_admin_delete($pid = 0) {
  db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
  drupal_set_message(t('The alias has been deleted.'));
}

/**
 * Set an aliased path for a given Drupal path, preventing duplicates.
 */
88
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
Steven Wittens's avatar
Steven Wittens committed
89
  if ($path && !$alias) {
90
    // Delete based on path
91
    db_query("DELETE FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language);
Steven Wittens's avatar
Steven Wittens committed
92
93
94
    drupal_clear_path_cache();
  }
  else if (!$path && $alias) {
95
    // Delete based on alias
96
    db_query("DELETE FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language);
Steven Wittens's avatar
Steven Wittens committed
97
98
99
100
    drupal_clear_path_cache();
  }
  else if ($path && $alias) {
    $path = urldecode($path);
101
    $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language));
Steven Wittens's avatar
Steven Wittens committed
102
    $alias = urldecode($alias);
103
    // Alias count can only be 0 or 1.
104
    $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language));
Steven Wittens's avatar
Steven Wittens committed
105

106
    if ($alias_count == 0) {
Steven Wittens's avatar
Steven Wittens committed
107
      if ($pid) {
108
109
        // Existing path changed data
        db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
Steven Wittens's avatar
Steven Wittens committed
110
111
      }
      else {
112
113
        // No such alias yet in this language
        db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
Steven Wittens's avatar
Steven Wittens committed
114
115
      }
    }
116
117
118
119
    // The alias exists.
    else {
      // This path has no alias yet, so we redirect the alias here.
      if ($path_count == 0) {
120
        db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s' AND language = '%s'", $path, $alias, $language);
121
122
123
      }
      else {
        // This will delete the path that alias was originally pointing to.
124
        path_set_alias(NULL, $alias, NULL, $language);
125
        // This will remove the current aliases of the path.
126
127
        path_set_alias($path, NULL, NULL, $language);
        path_set_alias($path, $alias, NULL, $language);
128
      }
Steven Wittens's avatar
Steven Wittens committed
129
    }
130
131
    if ($alias_count == 0 || $path_count == 0) {
      drupal_clear_path_cache();
Steven Wittens's avatar
Steven Wittens committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 *
 * Allows URL aliases for nodes to be specified at node edit time rather
 * than through the administrative interface.
 */
function path_nodeapi(&$node, $op, $arg) {
  if (user_access('create url aliases') || user_access('administer url aliases')) {
    switch ($op) {
      case 'validate':
        $node->path = trim($node->path);
147
148
        $language = isset($node->language) ? $node->language : '';
        if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s' AND language = '%s'", $node->path, "node/$node->nid", $language))) {
Steven Wittens's avatar
Steven Wittens committed
149
150
151
152
153
154
          form_set_error('path', t('The path is already in use.'));
        }
        break;

      case 'load':
        $path = "node/$node->nid";
155
156
157
158
        $language = isset($node->language) ? $node->language : '';
        $alias = drupal_get_path_alias($path, $language);
        if ($path != $alias) {
          $node->path = $alias;
Steven Wittens's avatar
Steven Wittens committed
159
160
161
162
163
164
        }
        break;

      case 'insert':
        // Don't try to insert if path is NULL. We may have already set
        // the alias ahead of time.
165
        if (isset($node->path)) {
Steven Wittens's avatar
Steven Wittens committed
166
167
168
169
170
          path_set_alias("node/$node->nid", $node->path);
        }
        break;

      case 'update':
171
        path_set_alias("node/$node->nid", isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL);
Steven Wittens's avatar
Steven Wittens committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
        break;

      case 'delete':
        $path = "node/$node->nid";
        if (drupal_get_path_alias($path) != $path) {
          path_set_alias($path);
        }
        break;
    }
  }
}

/**
 * Implementation of hook_form_alter().
 */
187
function path_form_alter(&$form, $form_state, $form_id) {
188
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
189
    $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
Steven Wittens's avatar
Steven Wittens committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    $form['path'] = array(
      '#type' => 'fieldset',
      '#title' => t('URL path settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($path),
      '#access' => user_access('create url aliases'),
      '#weight' => 30,
    );
    $form['path']['path'] = array(
      '#type' => 'textfield',
      '#default_value' => $path,
      '#maxlength' => 250,
      '#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',
        '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $path))
      );
    }
  }
}

/**
 * Implementation of hook_perm().
 */
function path_perm() {
  return array('create url aliases', 'administer url aliases');
}

/**
 * Fetch a specific URL alias from the database.
 */
function path_load($pid) {
  return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
}