/[drupal]/drupal/modules/path/path.module
ViewVC logotype

Contents of /drupal/modules/path/path.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.174 - (show annotations) (download) (as text)
Sun Nov 1 12:11:10 2009 UTC (3 weeks, 6 days ago) by dries
Branch: MAIN
Changes since 1.173: +4 -4 lines
File MIME type: text/x-php
- Patch #595084 by c960657: use type hinting for .
1 <?php
2 // $Id: path.module,v 1.173 2009/10/22 01:27:18 dries Exp $
3
4 /**
5 * @file
6 * Enables users to rename URLs.
7 */
8
9 /**
10 * Implement hook_help().
11 */
12 function path_help($path, $arg) {
13 switch ($path) {
14 case 'admin/help#path':
15 $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>';
16 $output .= '<p>' . t('Some examples of URL aliases are:') . '</p>';
17 $output .= '<ul><li>' . t('%alias for the path %path', array('%alias' => 'login', '%path' => 'user/login')) . '</li>';
18 $output .= '<li>' . t('%alias for the path %path', array('%alias' => 'store', '%path' => 'image/tid/16')) . '</li>';
19 $output .= '<li>' . t('%alias for the path %path', array('%alias' => 'store/products/whirlygigs', '%path' => 'taxonomy/term/7+19+20+21')) . '</li>';
20 $output .= '<li>' . t('%alias for the path %path', array('%alias' => 'contact', '%path' => 'node/3')) . '</li></ul>';
21 $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>';
22 $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>';
23 $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>';
24 return $output;
25
26 case 'admin/config/search/path':
27 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>';
28
29 case 'admin/config/search/path/add':
30 return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
31 }
32 }
33
34 /**
35 * Implement hook_permission().
36 */
37 function path_permission() {
38 return array(
39 'administer url aliases' => array(
40 'title' => t('Administer URL aliases'),
41 'description' => t('Manage URL aliases across the entire website.'),
42 ),
43 'create url aliases' => array(
44 'title' => t('Create URL aliases'),
45 'description' => t('Manage URL aliases on content.'),
46 ),
47 );
48 }
49
50 /**
51 * Implement hook_menu().
52 */
53 function path_menu() {
54 $items['admin/config/search/path'] = array(
55 'title' => 'URL aliases',
56 'description' => "Change your site's URL paths by aliasing them.",
57 'page callback' => 'path_admin_overview',
58 'access arguments' => array('administer url aliases'),
59 'file' => 'path.admin.inc',
60 );
61 $items['admin/config/search/path/edit/%path'] = array(
62 'title' => 'Edit alias',
63 'page callback' => 'path_admin_edit',
64 'page arguments' => array(5),
65 'access arguments' => array('administer url aliases'),
66 'type' => MENU_CALLBACK,
67 'file' => 'path.admin.inc',
68 );
69 $items['admin/config/search/path/delete/%path'] = array(
70 'title' => 'Delete alias',
71 'page callback' => 'drupal_get_form',
72 'page arguments' => array('path_admin_delete_confirm', 5),
73 'access arguments' => array('administer url aliases'),
74 'type' => MENU_CALLBACK,
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 * Implement hook_form_alter().
95 */
96 function path_form_alter(&$form, $form_state, $form_id) {
97 if (!empty($form['#node_edit_form'])) {
98 $path = array();
99 if (!empty($form['#node']->nid)) {
100 $conditions = array('source' => 'node/' . $form['#node']->nid);
101 if (!empty($form['#node']->language)) {
102 $conditions['language'] = $form['#node']->language;
103 }
104 $path = path_load($conditions);
105 if ($path === FALSE) {
106 $path = array();
107 }
108 }
109 $path += array(
110 'pid' => NULL,
111 'source' => isset($form['#node']->nid) ? 'node/' . $form['#node']->nid : NULL,
112 'alias' => '',
113 'language' => isset($form['#node']->language) ? $form['#node']->language : '',
114 );
115
116 $form['path'] = array(
117 '#type' => 'fieldset',
118 '#title' => t('URL path settings'),
119 '#collapsible' => TRUE,
120 '#collapsed' => empty($path['alias']),
121 '#group' => 'additional_settings',
122 '#attached' => array(
123 'js' => array(drupal_get_path('module', 'path') . '/path.js'),
124 ),
125 '#access' => user_access('create url aliases') || user_access('administer url aliases'),
126 '#weight' => 30,
127 '#tree' => TRUE,
128 '#element_validate' => array('path_form_element_validate'),
129 );
130 $form['path']['alias'] = array(
131 '#type' => 'textfield',
132 '#title' => t('URL alias'),
133 '#default_value' => $path['alias'],
134 '#maxlength' => 255,
135 '#collapsible' => TRUE,
136 '#collapsed' => TRUE,
137 '#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.'),
138 );
139 $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
140 $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
141 $form['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
142 }
143 }
144
145 /**
146 * Form element validation handler for URL alias form element.
147 */
148 function path_form_element_validate($element, &$form_state, $complete_form) {
149 if (!empty($form_state['values']['path']['alias'])) {
150 // Trim the submitted value.
151 $alias = trim($form_state['values']['path']['alias']);
152 form_set_value($element['alias'], $alias, $form_state);
153 // Node language (Locale module) needs special care. Since the language of
154 // the URL alias depends on the node language, and the node language can be
155 // switched right within the same form, we need to conditionally overload
156 // the originally assigned URL alias language.
157 // @todo Remove this after converting Path module to a field, and, after
158 // stopping Locale module from abusing the content language system.
159 if (isset($form_state['values']['language'])) {
160 form_set_value($element['language'], $form_state['values']['language'], $form_state);
161 }
162
163 $path = $form_state['values']['path'];
164
165 // Ensure that the submitted alias does not exist yet.
166 $query = db_select('url_alias')
167 ->condition('alias', $path['alias'])
168 ->condition('language', $path['language']);
169 if (!empty($path['source'])) {
170 $query->condition('source', $path['source'], '<>');
171 }
172 $query->addExpression('1');
173 $query->range(0, 1);
174 if ($query->execute()->fetchField()) {
175 form_set_error('alias', t('The alias is already in use.'));
176 }
177 }
178 }
179
180 /**
181 * Implement hook_node_insert().
182 */
183 function path_node_insert(stdClass $node) {
184 if (isset($node->path)) {
185 $path = $node->path;
186 $path['alias'] = trim($path['alias']);
187 // Only save a non-empty alias.
188 if (!empty($path['alias'])) {
189 // Ensure fields for programmatic executions.
190 $path['source'] = 'node/' . $node->nid;
191 $path['language'] = isset($node->language) ? $node->language : '';
192 path_save($path);
193 }
194 }
195 }
196
197 /**
198 * Implement hook_node_update().
199 */
200 function path_node_update(stdClass $node) {
201 if (isset($node->path)) {
202 $path = $node->path;
203 $path['alias'] = trim($path['alias']);
204 // Delete old alias if user erased it.
205 if (!empty($path['pid']) && empty($path['alias'])) {
206 path_delete($path['pid']);
207 }
208 // Only save a non-empty alias.
209 if (!empty($path['alias'])) {
210 // Ensure fields for programmatic executions.
211 $path['source'] = 'node/' . $node->nid;
212 $path['language'] = isset($node->language) ? $node->language : '';
213 path_save($path);
214 }
215 }
216 }
217
218 /**
219 * Implement hook_node_delete().
220 */
221 function path_node_delete(stdClass $node) {
222 // Delete all aliases associated with this node.
223 path_delete(array('source' => 'node/' . $node->nid));
224 }
225
226 /**
227 * Implement hook_form_FORM_ID_alter().
228 */
229 function path_form_taxonomy_form_term_alter(&$form, $form_state) {
230 // Make sure this does not show up on the delete confirmation form.
231 if (empty($form_state['confirm_delete'])) {
232 $path = (isset($form['#term']['tid']) ? path_load('taxonomy/term/' . $form['#term']['tid']) : array());
233 if ($path === FALSE) {
234 $path = array();
235 }
236 $path += array(
237 'pid' => NULL,
238 'source' => isset($form['#term']['tid']) ? 'taxonomy/term/' . $form['#term']['tid'] : NULL,
239 'alias' => '',
240 'language' => '',
241 );
242 $form['identification']['path'] = array(
243 '#access' => user_access('create url aliases') || user_access('administer url aliases'),
244 '#tree' => TRUE,
245 '#element_validate' => array('path_form_element_validate'),
246 );
247 $form['identification']['path']['alias'] = array(
248 '#type' => 'textfield',
249 '#title' => t('URL alias'),
250 '#default_value' => $path['alias'],
251 '#maxlength' => 255,
252 '#weight' => 0,
253 '#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."),
254 );
255 $form['identification']['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
256 $form['identification']['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
257 $form['identification']['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
258 }
259 }
260
261 /**
262 * Implement hook_taxonomy_term_insert().
263 */
264 function path_taxonomy_term_insert($term) {
265 if (isset($term->path)) {
266 $path = $term->path;
267 $path['alias'] = trim($path['alias']);
268 // Only save a non-empty alias.
269 if (!empty($path['alias'])) {
270 // Ensure fields for programmatic executions.
271 $path['source'] = 'taxonomy/term/' . $term->tid;
272 $path['language'] = '';
273 path_save($path);
274 }
275 }
276 }
277
278 /**
279 * Implement hook_taxonomy_term_update().
280 */
281 function path_taxonomy_term_update($term) {
282 if (isset($term->path)) {
283 $path = $term->path;
284 $path['alias'] = trim($path['alias']);
285 // Delete old alias if user erased it.
286 if (!empty($path['pid']) && empty($path['alias'])) {
287 path_delete($path['pid']);
288 }
289 // Only save a non-empty alias.
290 if (!empty($path['alias'])) {
291 // Ensure fields for programmatic executions.
292 $path['source'] = 'taxonomy/term/' . $term->tid;
293 $path['language'] = '';
294 path_save($path);
295 }
296 }
297 }
298
299 /**
300 * Implement hook_taxonomy_term_delete().
301 */
302 function path_taxonomy_term_delete($term) {
303 // Delete all aliases associated with this term.
304 path_delete(array('source' => 'taxonomy/term/' . $term->tid));
305 }
306

  ViewVC Help
Powered by ViewVC 1.1.2