1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Module to help test hook_url_inbound_alter() and hook_url_outbound_alter().
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_menu().
|
10
|
*/
|
11
|
function url_alter_test_menu() {
|
12
|
$items['url-alter-test/foo'] = array(
|
13
|
'title' => 'Foo',
|
14
|
'page callback' => 'url_alter_test_foo',
|
15
|
'access arguments' => array('access content'),
|
16
|
'type' => MENU_CALLBACK,
|
17
|
);
|
18
|
return $items;
|
19
|
}
|
20
|
|
21
|
/**
|
22
|
* Menu callback.
|
23
|
*/
|
24
|
function url_alter_test_foo() {
|
25
|
print 'current_path=' . current_path() . ' request_path=' . request_path();
|
26
|
exit;
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Implements hook_url_inbound_alter().
|
31
|
*/
|
32
|
function url_alter_test_url_inbound_alter(&$path, $original_path, $path_language) {
|
33
|
if (!request_path() && !empty($_GET['q'])) {
|
34
|
drupal_set_message("\$_GET['q'] is non-empty with an empty request path.");
|
35
|
}
|
36
|
|
37
|
// Rewrite user/username to user/uid.
|
38
|
if (preg_match('!^user/([^/]+)(/.*)?!', $path, $matches)) {
|
39
|
if ($account = user_load_by_name($matches[1])) {
|
40
|
$matches += array(2 => '');
|
41
|
$path = 'user/' . $account->uid . $matches[2];
|
42
|
}
|
43
|
}
|
44
|
|
45
|
// Rewrite community/ to forum/.
|
46
|
if ($path == 'community' || strpos($path, 'community/') === 0) {
|
47
|
$path = 'forum' . substr($path, 9);
|
48
|
}
|
49
|
|
50
|
if ($path == 'url-alter-test/bar') {
|
51
|
$path = 'url-alter-test/foo';
|
52
|
}
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* Implements hook_url_outbound_alter().
|
57
|
*/
|
58
|
function url_alter_test_url_outbound_alter(&$path, &$options, $original_path) {
|
59
|
// Rewrite user/uid to user/username.
|
60
|
if (preg_match('!^user/([0-9]+)(/.*)?!', $path, $matches)) {
|
61
|
if ($account = user_load($matches[1])) {
|
62
|
$matches += array(2 => '');
|
63
|
$path = 'user/' . $account->name . $matches[2];
|
64
|
}
|
65
|
}
|
66
|
|
67
|
// Rewrite forum/ to community/.
|
68
|
if ($path == 'forum' || strpos($path, 'forum/') === 0) {
|
69
|
$path = 'community' . substr($path, 5);
|
70
|
}
|
71
|
}
|