summaryrefslogtreecommitdiff
path: root/modules/path/path.module
blob: 33b0e98cd58b0dc08120fd690d5b58664b9dc7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
// $Id$

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

/**
 * Implement hook_help().
 */
function path_help($path, $arg) {
  switch ($path) {
    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 .= '<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>';
      $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>';
      $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>';
      return $output;
    case 'admin/settings/path':
      return '<p>' . t("An alias defines a different name for an existing URL path - for example, the alias 'about' for the URL path 'node/1'. A URL path can have multiple aliases.") . '</p>';
    case 'admin/settings/path/add':
      return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
  }
}

/**
 * Implement hook_menu().
 */
function path_menu() {
  $items['admin/settings/path'] = array(
    'title' => 'URL aliases',
    'description' => "Change your site's URL paths by aliasing them.",
    'page callback' => 'path_admin_overview',
    'access arguments' => array('administer url aliases'),
    'file' => 'path.admin.inc',
  );
  $items['admin/settings/path/edit'] = array(
    'title' => 'Edit alias',
    'page callback' => 'path_admin_edit',
    'access arguments' => array('administer url aliases'),
    'type' => MENU_CALLBACK,
    'file' => 'path.admin.inc',
  );
  $items['admin/settings/path/delete'] = array(
    'title' => 'Delete alias',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('path_admin_delete_confirm'),
    'access arguments' => array('administer url aliases'),
    'type' => MENU_CALLBACK,
    'file' => 'path.admin.inc',
  );
  $items['admin/settings/path/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/settings/path/add'] = array(
    'title' => 'Add alias',
    'page callback' => 'path_admin_edit',
    'access arguments' => array('administer url aliases'),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'path.admin.inc',
  );

  return $items;
}

/**
 * Post-confirmation; delete an URL alias.
 */
function path_admin_delete($pid = 0) {
  db_delete('url_alias')
    ->condition('pid', $pid)
    ->execute();
  drupal_set_message(t('The alias has been deleted.'));
}

/**
 * Set an aliased path for a given Drupal path, preventing duplicates.
 */
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
  $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.
      db_delete('url_alias')
        ->condition('pid', $pid)
        ->execute();
    }
    else {
      // Update the existing alias.
      db_update('url_alias')
        ->fields(array(
          'src'      => $path,
          'dst'      => $alias,
          'language' => $language))
        ->condition('pid', $pid)
        ->execute();
    }
  }
  elseif ($path && $alias) {
    // 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.
      db_update('url_alias')
        ->fields(array(
          'src' => $path,
          'dst' => $alias,
          'language' => $language
        ))
        ->condition('dst', $alias)
        ->execute();
    }
    else {
      // A new alias. Add it to the database.
      db_insert('url_alias')
        ->fields(array(
          'src'     => $path,
          'dst'      => $alias,
          'language' => $language,
        ))
        ->execute();
    }
  }
  else {
    // Delete the alias.
    if ($alias) {
      db_delete('url_alias')
        ->condition('dst', $alias)
        ->execute();
    }
    else {
      db_delete('url_alias')
        ->condition('src', $path)
        ->execute();
    }
  }
  drupal_clear_path_cache();
}

/**
 * Implement hook_node_validate().
 */
function path_node_validate($node, $form) {
  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);
      $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) {
        form_set_error('path', t('The path is already in use.'));
      }
    }
  }
}

/**
 * Implement hook_node_load().
 */
function path_node_load($nodes, $types) {
  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;
    }
  }
}

/**
 * Implement hook_node_insert().
 */
function path_node_insert($node) {
  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);
    }
  }
}

/**
 * Implement hook_node_update().
 */
function path_node_update($node) {
  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);
  }
}

/**
 * Implement hook_node_delete().
 */
function path_node_delete($node) {
  path_set_alias('node/' . $node->nid);
}

/**
 * Implement hook_taxonomy_term_delete().
 */
function path_taxonomy_term_delete($term) {
  path_set_alias('taxonomy/term/' . $term->tid);
}

/**
 * Implement hook_form_alter().
 */
function path_form_alter(&$form, $form_state, $form_id) {
  if (!empty($form['#node_edit_form'])) {
    $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
    $form['path'] = array(
      '#type' => 'fieldset',
      '#title' => t('URL path settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($path),
      '#group' => 'additional_settings',
      '#attached_js' => array(drupal_get_path('module', 'path') . '/path.js'),
      '#access' => user_access('create url aliases'),
      '#weight' => 30,
    );
    $form['path']['path'] = array(
      '#type' => 'textfield',
      '#title' => t('URL alias'),
      '#default_value' => $path,
      '#maxlength' => 255,
      '#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_query("SELECT pid FROM {url_alias} WHERE dst = :dst AND language = :language", array(
          ':dst' => $path,
          ':language' => $form['#node']->language
        ))
        ->fetchField(),
      );
    }
  }
}

/**
 * Implement hook_form_FORM_ID_alter().
 */
function path_form_taxonomy_form_term_alter(&$form, $form_state) {
  // Make sure this does not show up on the delete confirmation form.
  if (empty($form_state['confirm_delete'])) {
    // After a new term is added, populate the path field if it was set.
    if (!empty($form['#term']['path'])) {
      $path = $form['#term']['path'];
    }
    else {
      $url = 'taxonomy/term/' . $form['#term']['tid'];
      $alias = drupal_get_path_alias($url);

      // Since drupal_get_path_alias() can return the default path, check if we really have an alias.
      if ($alias != $url) {
        $path = $alias;
      }
      else {
        $path = NULL;
      }
    }
    $form['#validate'][] = 'path_taxonomy_term_validate';
    $form['#submit'][] = 'path_taxonomy_term_submit';
    $form['identification']['path'] = array(
      '#type' => 'textfield',
      '#title' => t('URL alias'),
      '#default_value' => $path,
      '#maxlength' => 255,
      '#weight' => 0,
      '#access' => (user_access('create url aliases') || user_access('administer url aliases')),
      '#description' => t("Optionally specify an alternative URL by which this term can be accessed. Use a relative path and don't add a trailing slash or the URL alias won't work."),
    );
    if ($path) {
      // Populate with pid so we can update existing path entry instead of creating a new one.
      $form['identification']['path']['pid'] = array(
        '#type' => 'value',
        '#access' => (user_access('create url aliases') || user_access('administer url aliases')),
        '#value' => db_query("SELECT pid FROM {url_alias} WHERE dst = :dst", array(':dst' => $path))->fetchField(),
      );
    }
  }
}

/**
 * Path validation callback for taxonomy_form_term.
 */
function path_taxonomy_term_validate($form, &$form_state) {
  $pid = db_query("SELECT pid FROM {url_alias} WHERE dst = :dst", array(':dst' => $form_state['values']['path']))->fetchField();
  if ($pid) {
    // If the pid matches the one in use for this term then we are fine.
    if (isset($form_state['values']['pid']) && $pid == $form_state['values']['pid']) {
      return;
    }
    form_set_error('path', 'The URL alias is already in use.');
  }
}

/**
 * Path submission callback for taxonomy_form_term.
 */
function path_taxonomy_term_submit($form, &$form_state) {
  // Make sure this is not triggered on the delete confirmation form.
  if (empty($form_state['confirm_delete'])) {
    $url = 'taxonomy/term/' . $form_state['tid'];
    $alias = isset($form_state['values']['path']) ? $form_state['values']['path'] : NULL;
    $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : NULL;
    path_set_alias($url, $alias, $pid);
  }
}

/**
 * Implement hook_permission().
 */
function path_permission() {
  return array(
    '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.'),
    ),
  );
}

/**
 * Fetch a specific URL alias from the database.
 */
function path_load($pid) {
  return db_query('SELECT * FROM {url_alias} WHERE pid = :pid', array(':pid' => $pid))->fetchAssoc();
}