-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_unlink.module
225 lines (207 loc) · 6.25 KB
/
menu_unlink.module
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
<?php
define ('MENU_UNLINK_TYPE_NONE', -1, TRUE);
define ('MENU_UNLINK_TYPE_REMOVE', 0, TRUE);
define ('MENU_UNLINK_TYPE_FRAGMENT', 1, TRUE);
define ('MENU_UNLINK_DEFAULT_FRAGMENT', 'main-menu', TRUE);
/**
* Implementation of hook_menu().
*/
function menu_unlink_menu() {
$items['admin/settings/menu_unlink'] = array(
'title' => t('Menu unlink'),
'page callback' => 'menu_unlink_overview',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'menu_unlink.admin.inc',
);
$items['admin/settings/menu_unlink/list'] = array(
'title' => t('List'),
'access arguments' => array('administer site configuration'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/settings/menu_unlink/bulk'] = array(
'title' => t('Bulk'),
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_unlink_bulk_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'menu_unlink.admin.inc',
);
$items['admin/settings/menu_unlink/%/delete'] = array(
'title' => t('Delete'),
'page callback' => 'menu_unlink_delete',
'page arguments' => array(3, 'admin/settings/menu_unlink'),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_form_alter().
*/
function menu_unlink_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'menu_edit_item') {
$form['menu']['menu_unlink_fs'] = array(
'#type' => 'fieldset',
'#title' => t('Unlink options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'menu_unlink_type' => array(
'#type' => 'radios',
'#title' => t('Select options'),
'#description' => t('Leave this alone if you do not want to change.'),
'#default_value' => variable_get('menu_unlink_type_mlid_'. arg(4), MENU_UNLINK_TYPE_NONE),
'#options' => array(
MENU_UNLINK_TYPE_NONE => t('None'),
MENU_UNLINK_TYPE_REMOVE => t('Remove'),
MENU_UNLINK_TYPE_FRAGMENT => t('Fragment')
),
),
'menu_unlink_fragment' => array(
'#type' => 'textfield',
'#title' => t('Fragment'),
'#description' => t('If you select fragment above, you can define fragment on your own.'),
'#default_value' => variable_get('menu_unlink_fragment_mlid_'. arg(4), MENU_UNLINK_DEFAULT_FRAGMENT),
),
);
$form['#submit'] = array_merge(array('menu_unlink_form_submit'), $form['#submit']);
}
}
/**
* Submit callback function.
*/
function menu_unlink_form_submit($form, &$form_state) {
$values = $form_state['values'];
$type = (int) $values['menu']['menu_unlink_fs']['menu_unlink_type'];
$fragment = $values['menu']['menu_unlink_fs']['menu_unlink_fragment'];
if ($type === MENU_UNLINK_TYPE_NONE) {
menu_unlink_delete($values['menu']['mlid']);
}
else {
menu_unlink_save($values['menu']['mlid'], $type, $fragment);
}
}
/**
* Implementation of hook_theme().
*/
function menu_unlink_theme() {
return array(
'menu_item_link' => array(
'arguments' => array('link' => NULL),
'function' => 'menu_unlink_menu_item_link',
),
);
}
/**
* Override theme_menu_item_link().
*/
function menu_unlink_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
$current_path = drupal_get_path_alias($_GET['q']);
$current_path = preg_replace('/(\?|\/*\?).*$/', '', $current_path) .'/';
if (strpos($current_path, drupal_get_path_alias($link['href']) .'/') !== FALSE) {
if (isset($link['localized_options']['attributes']['class'])) {
$link['localized_options']['attributes']['class'] .= ' active';
}
else {
$link['localized_options']['attributes']['class'] = 'active';
}
}
if ($_link = menu_unlink_check($link)) {
return $_link;
}
return l($link['title'], $link['href'], $link['localized_options']);
}
/**
* Check links.
*/
function menu_unlink_check($link) {
$type = variable_get('menu_unlink_type_mlid_'. $link['mlid'], FALSE);
if (is_numeric($type)) {
switch ($type) {
case MENU_UNLINK_TYPE_REMOVE:
return $link['title'];
break;
case MENU_UNLINK_TYPE_FRAGMENT:
$fragment = variable_get('menu_unlink_fragment_mlid_'. $link['mlid'], MENU_UNLINK_DEFAULT_FRAGMENT);
$href = base_path();
$href .= drupal_get_path_alias($_GET['q']);
$href .= '#'. (empty($fragment) ? MENU_UNLINK_DEFAULT_FRAGMENT : $fragment);
$link['localized_options']['attributes']['href'] = $href;
$attributes = '';
foreach ($link['localized_options']['attributes'] as $key => $value) {
$attributes .= " $key=".'"'. $value .'"';
}
return "<a $attributes>{$link['title']}</a>";
break;
}
}
return FALSE;
}
/**
* Return array of mlids
*/
function menu_unlink_get_links() {
$links = nl2br(variable_get('menu_unlink_mlids', ''));
$lines = explode('<br />', $links);
$mlids = array();
foreach ($lines as $line) {
$tmp = explode(',', $line);
foreach ($tmp as $mlid) {
$mlids[] = trim($mlid);
}
}
return $mlids;
}
/**
* Extract textarea value to mlids array.
*/
function menu_unlink_extract($value) {
$links = nl2br($value);
$lines = explode('<br />', $links);
$mlids = array();
foreach ($lines as $line) {
$tmp = explode(',', $line);
foreach ($tmp as $mlid) {
$mlids[] = trim($mlid);
}
}
return $mlids;
}
/**
* Delete configure.
*/
function menu_unlink_delete($mlid, $redirect = FALSE) {
variable_del('menu_unlink_type_mlid_'. $mlid);
variable_del('menu_unlink_fragment_mlid_'. $mlid);
if ($redirect) {
drupal_goto($redirect);
}
}
/**
* Save configure.
*/
function menu_unlink_save($mlid, $type, $fragment) {
variable_set('menu_unlink_type_mlid_'. $mlid, $type);
variable_set('menu_unlink_fragment_mlid_'. $mlid, $fragment);
}
/**
* Return readable type.
*/
function menu_unlink_type_readable($type) {
switch ($type) {
case MENU_UNLINK_TYPE_NONE:
$type = t('None');
break;
case MENU_UNLINK_TYPE_REMOVE:
$type = t('Remove');
break;
case MENU_UNLINK_TYPE_FRAGMENT:
$type = t('Fragment');
break;
}
return $type;
}