summaryrefslogtreecommitdiff
path: root/modules/path/path.module
blob: ca3c16d555261da563a5e2eee1130476ff9d7f67 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?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/config/search/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/config/search/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_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.'),
    ),
  );
}

/**
 * Implement hook_menu().
 */
function path_menu() {
  $items['admin/config/search/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/config/search/path/edit/%path'] = array(
    'title' => 'Edit alias',
    'page callback' => 'path_admin_edit',
    'page arguments' => array(5),
    'access arguments' => array('administer url aliases'),
    'type' => MENU_CALLBACK,
    'file' => 'path.admin.inc',
  );
  $items['admin/config/search/path/delete/%path'] = array(
    'title' => 'Delete alias',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('path_admin_delete_confirm', 5),
    'access arguments' => array('administer url aliases'),
    'type' => MENU_CALLBACK,
    'file' => 'path.admin.inc',
  );
  $items['admin/config/search/path/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/search/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;
}

/**
 * Fetch a specific URL alias from the database.
 *
 * @param $conditions
 *   A string representing the source, a number representing the pid, or an
 *   array of query conditions.
 *
 * @return
 *   FALSE if no alias was found or an associative array containing the
 *   following keys:
 *   - source: The internal system path.
 *   - alias: The URL alias.
 *   - pid: Unique path alias identifier.
 *   - language: The language of the alias.
 */
function path_load($conditions) {
  if (is_numeric($conditions)) {
    $conditions = array('pid' => $conditions);
  }
  elseif (is_string($conditions)) {
    $conditions = array('source' => $conditions);
  }
  elseif (!is_array($conditions)) {
    return FALSE;
  }
  $select = db_select('url_alias');
  foreach ($conditions as $field => $value) {
    $select->condition($field, $value);
  }
  return $select
    ->fields('url_alias')
    ->execute()
    ->fetchAssoc();
}

/**
 * Save a path alias to the database.
 *
 * @param $path
 *   An associative array containing the following keys:
 *   - source: The internal system path.
 *   - alias: The URL alias.
 *   - pid: (optional) Unique path alias identifier.
 *   - language: (optional) The language of the alias.
 */
function path_save(&$path) {
  $path += array('pid' => NULL, 'language' => '');

  // Insert or update the alias.
  $status = drupal_write_record('url_alias', $path, (!empty($path['pid']) ? 'pid' : NULL));

  // Verify that a record was written.
  if (isset($status) && $status) {
    if ($status === SAVED_NEW) {
      module_invoke_all('path_insert', $path);
    }
    else {
      module_invoke_all('path_update', $path);
    }
    drupal_clear_path_cache();
  }
}

/**
 * Delete a URL alias.
 *
 * @param $criteria
 *   A number representing the pid or an array of criteria.
 */
function path_delete($criteria) {
  if (!is_array($criteria)) {
    $criteria = array('pid' => $criteria);
  }
  $path = path_load($criteria);
  $query = db_delete('url_alias');
  foreach ($criteria as $field => $value) {
    $query->condition($field, $value);
  }
  $query->execute();
  module_invoke_all('path_delete', $path);
  drupal_clear_path_cache();
}

/**
 * Implement hook_form_alter().
 */
function path_form_alter(&$form, $form_state, $form_id) {
  if (!empty($form['#node_edit_form'])) {
    $path = array();
    if (!empty($form['#node']->nid)) {
      $conditions = array('source' => 'node/' . $form['#node']->nid);
      if (!empty($form['#node']->language)) {
        $conditions['language'] = $form['#node']->language;
      }
      $path = path_load($conditions);
      if ($path === FALSE) {
        $path = array();
      }
    }
    $path += array(
      'pid' => NULL,
      'source' => isset($form['#node']->nid) ? 'node/' . $form['#node']->nid : NULL,
      'alias' => '',
      'language' => isset($form['#node']->language) ? $form['#node']->language : '',
    );

    $form['path'] = array(
      '#type' => 'fieldset',
      '#title' => t('URL path settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($path['alias']),
      '#group' => 'additional_settings',
      '#attached' => array(
        'js' => array(drupal_get_path('module', 'path') . '/path.js'),
      ),
      '#access' => user_access('create url aliases') || user_access('administer url aliases'),
      '#weight' => 30,
      '#tree' => TRUE,
      '#element_validate' => array('path_form_element_validate'),
    );
    $form['path']['alias'] = array(
      '#type' => 'textfield',
      '#title' => t('URL alias'),
      '#default_value' => $path['alias'],
      '#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.'),
    );
    $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
    $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
    $form['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
  }
}

/**
 * Form element validation handler for URL alias form element.
 */
function path_form_element_validate($element, &$form_state, $complete_form) {
  if (!empty($form_state['values']['path']['alias'])) {
    // Trim the submitted value.
    $alias = trim($form_state['values']['path']['alias']);
    form_set_value($element['alias'], $alias, $form_state);
    // Node language (Locale module) needs special care. Since the language of
    // the URL alias depends on the node language, and the node language can be
    // switched right within the same form, we need to conditionally overload
    // the originally assigned URL alias language.
    // @todo Remove this after converting Path module to a field, and, after
    //   stopping Locale module from abusing the content language system.
    if (isset($form_state['values']['language'])) {
      form_set_value($element['language'], $form_state['values']['language'], $form_state);
    }

    $path = $form_state['values']['path'];

    // Ensure that the submitted alias does not exist yet.
    $query = db_select('url_alias')
      ->condition('alias', $path['alias'])
      ->condition('language', $path['language']);
    if (!empty($path['source'])) {
      $query->condition('source', $path['source'], '<>');
    }
    $query->addExpression('1');
    $query->range(0, 1);
    if ($query->execute()->fetchField()) {
      form_set_error('alias', t('The alias is already in use.'));
    }
  }
}

/**
 * Implement hook_node_insert().
 */
function path_node_insert($node) {
  if (isset($node->path)) {
    $path = $node->path;
    $path['alias'] = trim($path['alias']);
    // Only save a non-empty alias.
    if (!empty($path['alias'])) {
      // Ensure fields for programmatic executions.
      $path['source'] = 'node/' . $node->nid;
      $path['language'] = isset($node->language) ? $node->language : '';
      path_save($path);
    }
  }
}

/**
 * Implement hook_node_update().
 */
function path_node_update($node) {
  if (isset($node->path)) {
    $path = $node->path;
    $path['alias'] = trim($path['alias']);
    // Delete old alias if user erased it.
    if (!empty($path['pid']) && empty($path['alias'])) {
      path_delete($path['pid']);
    }
    // Only save a non-empty alias.
    if (!empty($path['alias'])) {
      // Ensure fields for programmatic executions.
      $path['source'] = 'node/' . $node->nid;
      $path['language'] = isset($node->language) ? $node->language : '';
      path_save($path);
    }
  }
}

/**
 * Implement hook_node_delete().
 */
function path_node_delete($node) {
  // Delete all aliases associated with this node.
  path_delete(array('source' => 'node/' . $node->nid));
}

/**
 * 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'])) {
    $path = (isset($form['#term']['tid']) ? path_load('taxonomy/term/' . $form['#term']['tid']) : array());
    if ($path === FALSE) {
      $path = array();
    }
    $path += array(
      'pid' => NULL,
      'source' => isset($form['#term']['tid']) ? 'taxonomy/term/' . $form['#term']['tid'] : NULL,
      'alias' => '',
      'language' => '',
    );
    $form['identification']['path'] = array(
      '#access' => user_access('create url aliases') || user_access('administer url aliases'),
      '#tree' => TRUE,
      '#element_validate' => array('path_form_element_validate'),
    );
    $form['identification']['path']['alias'] = array(
      '#type' => 'textfield',
      '#title' => t('URL alias'),
      '#default_value' => $path['alias'],
      '#maxlength' => 255,
      '#weight' => 0,
      '#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."),
    );
    $form['identification']['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
    $form['identification']['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
    $form['identification']['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
  }
}

/**
 * Implement hook_taxonomy_term_insert().
 */
function path_taxonomy_term_insert($term) {
  if (isset($term->path)) {
    $path = $term->path;
    $path['alias'] = trim($path['alias']);
    // Only save a non-empty alias.
    if (!empty($path['alias'])) {
      // Ensure fields for programmatic executions.
      $path['source'] = 'taxonomy/term/' . $term->tid;
      $path['language'] = '';
      path_save($path);
    }
  }
}

/**
 * Implement hook_taxonomy_term_update().
 */
function path_taxonomy_term_update($term) {
  if (isset($term->path)) {
    $path = $term->path;
    $path['alias'] = trim($path['alias']);
    // Delete old alias if user erased it.
    if (!empty($path['pid']) && empty($path['alias'])) {
      path_delete($path['pid']);
    }
    // Only save a non-empty alias.
    if (!empty($path['alias'])) {
      // Ensure fields for programmatic executions.
      $path['source'] = 'taxonomy/term/' . $term->tid;
      $path['language'] = '';
      path_save($path);
    }
  }
}

/**
 * Implement hook_taxonomy_term_delete().
 */
function path_taxonomy_term_delete($term) {
  // Delete all aliases associated with this term.
  path_delete(array('source' => 'taxonomy/term/' . $term->tid));
}