Projet

Général

Profil

Paste
Télécharger (11,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / path / path.module @ 01dfd3b5

1
<?php
2

    
3
/**
4
 * @file
5
 * Enables users to rename URLs.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function path_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/help#path':
14
      $output = '';
15
      $output .= '<h3>' . t('About') . '</h3>';
16
      $output .= '<p>' . t('The Path module allows you to specify an alias, or custom URL, for any existing internal system path. Aliases should not be confused with URL redirects, which allow you to forward a changed or inactive URL to a new URL. In addition to making URLs more readable, aliases also help search engines index content more effectively. Multiple aliases may be used for a single internal system path. To automate the aliasing of paths, you can install the contributed module <a href="@pathauto">Pathauto</a>. For more information, see the online handbook entry for the <a href="@path">Path module</a>.', array('@path' => 'http://drupal.org/documentation/modules/path', '@pathauto' => 'http://drupal.org/project/pathauto')) . '</p>';
17
      $output .= '<h3>' . t('Uses') . '</h3>';
18
      $output .= '<dl>';
19
      $output .= '<dt>' . t('Creating aliases') . '</dt>';
20
      $output .= '<dd>' . t('Users with sufficient <a href="@permissions">permissions</a> can create aliases under the <em>URL path settings</em> section when they create or edit content. Some examples of aliases are: ', array('@permissions' => url('admin/people/permissions', array('fragment' => 'module-path'))));
21
      $output .= '<ul><li>' . t('<em>member/jane-smith</em> aliased to internal path <em>user/123</em>') . '</li>';
22
      $output .= '<li>' . t('<em>about-us/team</em> aliased to internal path <em>node/456</em>') . '</li>';
23
      $output .= '</ul></dd>';
24
      $output .= '<dt>' . t('Managing aliases') . '</dt>';
25
      $output .= '<dd>' . t('The Path module provides a way to search and view a <a href="@aliases">list of all aliases</a> that are in use on your website. Aliases can be added, edited and deleted through this list.', array('@aliases' => url('admin/config/search/path'))) . '</dd>';
26
      $output .= '</dl>';
27
      return $output;
28

    
29
    case 'admin/config/search/path':
30
      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>';
31

    
32
    case 'admin/config/search/path/add':
33
      return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
34
  }
35
}
36

    
37
/**
38
 * Implements hook_permission().
39
 */
40
function path_permission() {
41
  return array(
42
    'administer url aliases' => array(
43
      'title' => t('Administer URL aliases'),
44
    ),
45
    'create url aliases' => array(
46
      'title' => t('Create and edit URL aliases'),
47
    ),
48
  );
49
}
50

    
51
/**
52
 * Implements hook_menu().
53
 */
54
function path_menu() {
55
  $items['admin/config/search/path'] = array(
56
    'title' => 'URL aliases',
57
    'description' => "Change your site's URL paths by aliasing them.",
58
    'page callback' => 'path_admin_overview',
59
    'access arguments' => array('administer url aliases'),
60
    'weight' => -5,
61
    'file' => 'path.admin.inc',
62
  );
63
  $items['admin/config/search/path/edit/%path'] = array(
64
    'title' => 'Edit alias',
65
    'page callback' => 'path_admin_edit',
66
    'page arguments' => array(5),
67
    'access arguments' => array('administer url aliases'),
68
    'file' => 'path.admin.inc',
69
  );
70
  $items['admin/config/search/path/delete/%path'] = array(
71
    'title' => 'Delete alias',
72
    'page callback' => 'drupal_get_form',
73
    'page arguments' => array('path_admin_delete_confirm', 5),
74
    'access arguments' => array('administer url aliases'),
75
    'file' => 'path.admin.inc',
76
  );
77
  $items['admin/config/search/path/list'] = array(
78
    'title' => 'List',
79
    'type' => MENU_DEFAULT_LOCAL_TASK,
80
    'weight' => -10,
81
  );
82
  $items['admin/config/search/path/add'] = array(
83
    'title' => 'Add alias',
84
    'page callback' => 'path_admin_edit',
85
    'access arguments' => array('administer url aliases'),
86
    'type' => MENU_LOCAL_ACTION,
87
    'file' => 'path.admin.inc',
88
  );
89

    
90
  return $items;
91
}
92

    
93
/**
94
 * Implements hook_form_BASE_FORM_ID_alter() for node_form().
95
 *
96
 * @see path_form_element_validate()
97
 */
98
function path_form_node_form_alter(&$form, $form_state) {
99
  $path = array();
100
  if (!empty($form['#node']->nid)) {
101
    $conditions = array('source' => 'node/' . $form['#node']->nid);
102
    $langcode = entity_language('node', $form['#node']);
103
    if ($langcode != LANGUAGE_NONE) {
104
      $conditions['language'] = $langcode;
105
    }
106
    $path = path_load($conditions);
107
    if ($path === FALSE) {
108
      $path = array();
109
    }
110
  }
111
  $path += array(
112
    'pid' => NULL,
113
    'source' => isset($form['#node']->nid) ? 'node/' . $form['#node']->nid : NULL,
114
    'alias' => '',
115
    'language' => isset($langcode) ? $langcode : LANGUAGE_NONE,
116
  );
117

    
118
  $form['path'] = array(
119
    '#type' => 'fieldset',
120
    '#title' => t('URL path settings'),
121
    '#collapsible' => TRUE,
122
    '#collapsed' => empty($path['alias']),
123
    '#group' => 'additional_settings',
124
    '#attributes' => array(
125
      'class' => array('path-form'),
126
    ),
127
    '#attached' => array(
128
      'js' => array(drupal_get_path('module', 'path') . '/path.js'),
129
    ),
130
    '#access' => user_access('create url aliases') || user_access('administer url aliases'),
131
    '#weight' => 30,
132
    '#tree' => TRUE,
133
    '#element_validate' => array('path_form_element_validate'),
134
  );
135
  $form['path']['alias'] = array(
136
    '#type' => 'textfield',
137
    '#title' => t('URL alias'),
138
    '#default_value' => $path['alias'],
139
    '#maxlength' => 255,
140
    '#description' => t('Optionally specify an alternative URL by which this content 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.'),
141
  );
142
  $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
143
  $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
144
  $form['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
145
}
146

    
147
/**
148
 * Form element validation handler for URL alias form element.
149
 *
150
 * @see path_form_node_form_alter()
151
 */
152
function path_form_element_validate($element, &$form_state, $complete_form) {
153
  // Trim the submitted value.
154
  $alias = trim($form_state['values']['path']['alias']);
155
  if (!empty($alias)) {
156
    form_set_value($element['alias'], $alias, $form_state);
157
    // Node language (Locale module) needs special care. Since the language of
158
    // the URL alias depends on the node language, and the node language can be
159
    // switched right within the same form, we need to conditionally overload
160
    // the originally assigned URL alias language.
161
    // @todo Remove this after converting Path module to a field, and, after
162
    //   stopping Locale module from abusing the content language system.
163
    if (isset($form_state['values']['language'])) {
164
      form_set_value($element['language'], $form_state['values']['language'], $form_state);
165
    }
166

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

    
169
    // Ensure that the submitted alias does not exist yet.
170
    $query = db_select('url_alias')
171
      ->condition('alias', $path['alias'])
172
      ->condition('language', $path['language']);
173
    if (!empty($path['source'])) {
174
      $query->condition('source', $path['source'], '<>');
175
    }
176
    $query->addExpression('1');
177
    $query->range(0, 1);
178
    if ($query->execute()->fetchField()) {
179
      form_error($element, t('The alias is already in use.'));
180
    }
181
  }
182
}
183

    
184
/**
185
 * Implements hook_node_insert().
186
 */
187
function path_node_insert($node) {
188
  if (isset($node->path) && isset($node->path['alias'])) {
189
    $path = $node->path;
190
    $path['alias'] = trim($path['alias']);
191
    // Only save a non-empty alias.
192
    if (!empty($path['alias'])) {
193
      // Ensure fields for programmatic executions.
194
      $langcode = entity_language('node', $node);
195
      $path['source'] = 'node/' . $node->nid;
196
      $path['language'] = isset($langcode) ? $langcode : LANGUAGE_NONE;
197
      path_save($path);
198
    }
199
  }
200
}
201

    
202
/**
203
 * Implements hook_node_update().
204
 */
205
function path_node_update($node) {
206
  if (isset($node->path)) {
207
    $path = $node->path;
208
    $path['alias'] = isset($path['alias']) ? trim($path['alias']) : '';
209
    // Delete old alias if user erased it.
210
    if (!empty($path['pid']) && !$path['alias']) {
211
      path_delete($path['pid']);
212
    }
213
    path_node_insert($node);
214
  }
215
}
216

    
217
/**
218
 * Implements hook_node_delete().
219
 */
220
function path_node_delete($node) {
221
  // Delete all aliases associated with this node.
222
  path_delete(array('source' => 'node/' . $node->nid));
223
}
224

    
225
/**
226
 * Implements hook_form_FORM_ID_alter() for taxonomy_form_term().
227
 */
228
function path_form_taxonomy_form_term_alter(&$form, $form_state) {
229
  // Make sure this does not show up on the delete confirmation form.
230
  if (empty($form_state['confirm_delete'])) {
231
    $langcode = entity_language('taxonomy_term', (object) $form['#term']);
232
    $langcode = !empty($langcode) ? $langcode : LANGUAGE_NONE;
233
    $conditions = array('source' => 'taxonomy/term/' . $form['#term']['tid'], 'language' => $langcode);
234
    $path = (isset($form['#term']['tid']) ? path_load($conditions) : array());
235
    if ($path === FALSE) {
236
      $path = array();
237
    }
238
    $path += array(
239
      'pid' => NULL,
240
      'source' => isset($form['#term']['tid']) ? 'taxonomy/term/' . $form['#term']['tid'] : NULL,
241
      'alias' => '',
242
      'language' => $langcode,
243
    );
244
    $form['path'] = array(
245
      '#access' => user_access('create url aliases') || user_access('administer url aliases'),
246
      '#tree' => TRUE,
247
      '#element_validate' => array('path_form_element_validate'),
248
    );
249
    $form['path']['alias'] = array(
250
      '#type' => 'textfield',
251
      '#title' => t('URL alias'),
252
      '#default_value' => $path['alias'],
253
      '#maxlength' => 255,
254
      '#weight' => 0,
255
      '#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."),
256
    );
257
    $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
258
    $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
259
    $form['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
260
  }
261
}
262

    
263
/**
264
 * Implements hook_taxonomy_term_insert().
265
 */
266
function path_taxonomy_term_insert($term) {
267
  if (isset($term->path)) {
268
    $path = $term->path;
269
    $path['alias'] = trim($path['alias']);
270
    // Only save a non-empty alias.
271
    if (!empty($path['alias'])) {
272
      // Ensure fields for programmatic executions.
273
      $path['source'] = 'taxonomy/term/' . $term->tid;
274
      // Core does not provide a way to store the term language but contrib
275
      // modules can do it so we need to take this into account.
276
      $langcode = entity_language('taxonomy_term', $term);
277
      $path['language'] = !empty($langcode) ? $langcode : LANGUAGE_NONE;
278
      path_save($path);
279
    }
280
  }
281
}
282

    
283
/**
284
 * Implements hook_taxonomy_term_update().
285
 */
286
function path_taxonomy_term_update($term) {
287
  if (isset($term->path)) {
288
    $path = $term->path;
289
    $path['alias'] = trim($path['alias']);
290
    // Delete old alias if user erased it.
291
    if (!empty($path['pid']) && empty($path['alias'])) {
292
      path_delete($path['pid']);
293
    }
294
    // Only save a non-empty alias.
295
    if (!empty($path['alias'])) {
296
      // Ensure fields for programmatic executions.
297
      $path['source'] = 'taxonomy/term/' . $term->tid;
298
      // Core does not provide a way to store the term language but contrib
299
      // modules can do it so we need to take this into account.
300
      $langcode = entity_language('taxonomy_term', $term);
301
      $path['language'] = !empty($langcode) ? $langcode : LANGUAGE_NONE;
302
      path_save($path);
303
    }
304
  }
305
}
306

    
307
/**
308
 * Implements hook_taxonomy_term_delete().
309
 */
310
function path_taxonomy_term_delete($term) {
311
  // Delete all aliases associated with this term.
312
  path_delete(array('source' => 'taxonomy/term/' . $term->tid));
313
}