forked from drupalprojects/subpathauto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubpathauto.module
More file actions
140 lines (126 loc) · 4.29 KB
/
subpathauto.module
File metadata and controls
140 lines (126 loc) · 4.29 KB
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
<?php
// $Id$
/**
* Implements hook_menu().
*/
function subpathauto_menu() {
$items['admin/config/search/path/subpaths'] = array(
'title' => 'Sub-path settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('subpathauto_settings_form'),
'access arguments' => array('administer url aliases'),
'type' => MENU_LOCAL_TASK,
'weight' => 25,
'file' => 'subpathauto.admin.inc',
);
return $items;
}
/**
* Implements hook_url_inbound_alter().
*/
function subpathauto_url_inbound_alter(&$path, $original_path, $language) {
if ($source = subpathauto_lookup_subpath('source', $path, $original_path, $language)) {
$path = $source;
}
}
/**
* Implements hook_url_outbound_alter().
*/
function subpathauto_url_outbound_alter(&$path, &$options, $original_path) {
if (!empty($options['external'])) {
return;
}
$language = !empty($options['language']->language) ? $options['language']->language : NULL;
if ($alias = subpathauto_lookup_subpath('alias', $path, $original_path, $language)) {
$path = $alias;
}
}
/**
* Given an alias, return its Drupal system URL if one exists. Given a Drupal
* system URL return one of its aliases if such a one exists. Otherwise,
* return FALSE.
*
* @param $action
* One of the following values:
* - wipe: delete the alias cache.
* - alias: return an alias for a given Drupal system path (if one exists).
* - source: return the Drupal system URL for a path alias (if one exists).
* @param $path
* The path to investigate for corresponding aliases or system URLs.
* @param $path_language
* Optional language code to search the path with. Defaults to the page
* language. If there's no path defined for that language it will search\
* paths without language.
*
* @return
* Either a Drupal system path, an aliased path, or FALSE if no path was
* found.
*/
function subpathauto_lookup_subpath($action, $path = '', $original_path, $path_language = NULL) {
global $language_url;
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['cache'] = &drupal_static(__FUNCTION__);
}
$cache = &$drupal_static_fast['cache'];
if (!isset($cache)) {
$cache = array(
'map' => array(),
);
}
if ($action == 'wipe') {
$cache = array();
}
if ($path == '' || $path != $original_path || strpos($path, '/') === FALSE) {
return FALSE;
}
if (variable_get('subpathauto_ignore_admin', 1) && path_is_admin($path)) {
return FALSE;
}
if (drupal_match_path($path, "<front>\njs/*")) {
return FALSE;
}
static $max_depth;
if (!isset($max_depth)) {
$max_depth = variable_get('subpathauto_depth', 1);
}
if (!$max_depth) {
return FALSE;
}
// If no language is explicitly specified we default to the current URL
// language. If we used a language different from the one conveyed by the
// requested URL, we might end up being unable to check if there is a path
// alias matching the URL path.
$path_language = $path_language ? $path_language : $language_url->language;
$args = arg(NULL, $path);
$depth = min($max_depth, substr_count($path, '/'));
//$depth = min($max_depth, count($args));
$path_len = drupal_strlen($path);
$slash_pos = $path_len;
// Perform a search for each base path with the right-most segment removed.
//$base_paths = array();
for ($i = 1; $i <= $depth; $i++) {
//dpm("$path - $slash_pos");
$slash_pos = strrpos($path, '/', $path_len - $slash_pos);
$base_path = substr($path, 0, $slash_pos);
$sub_path = substr($path, $slash_pos + 1);
//dpm("$path - $slash_pos - $base_path - $sub_path");
//$base_path = implode('/', array_slice($args, 0, -$i));
//$sub_path = implode('/', array_slice($args, -$i));
//$base_paths[$base_path] = $sub_path;
if ($action == 'alias') {
$aliased_base_path = drupal_get_path_alias($base_path, $path_language);
if ($aliased_base_path != $base_path) {
return $aliased_base_path . '/' . $sub_path;
}
}
elseif ($action == 'source') {
$sourced_base_path = drupal_get_normal_path($base_path, $path_language);
if ($sourced_base_path != $base_path) {
return $sourced_base_path . '/' . $sub_path;
}
}
}
return FALSE;
}