-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial_menu_items.module
232 lines (200 loc) · 7.85 KB
/
special_menu_items.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
226
227
228
229
230
231
232
<?php
/**
* @file
* Module to enable placeholder or separator menu items.Placeholder is a menu item which is
* actually not a link. Something like this is useful with drop down menus where we want to
* have a parent link which is actually not linking to a page but which is just acting as a
* parent and grouping some children below it.
* A separator menu item is something like "-------" which is also not linking anywhere but
* merely a mean to structure menus.
*
* Written by Tamir Al Zoubi and Karim Djelid - Servit Open Source Solutions - www.servit.ch
*/
/**
*Implementation of hook_menu()
*/
function special_menu_items_menu() {
$items['<nolink>'] = array(
'page callback' => 'drupal_not_found',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['<separator>'] = array(
'page callback' => 'drupal_not_found',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/config/system/special_menu_items'] = array(
'title' => 'Special Menu Items',
'description' => 'Configure Special Menu Items.',
'page callback' => 'drupal_get_form',
'page arguments' => array('special_menu_items_admin_settings_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Override of theme_link()
* This function will render link if it is "nolink" or "separator". Otherwise it will call originally
* overwriten menu_item_link function.
*/
function special_menu_items_link(array $variables) {
if (preg_match('/^(\<nolink\>|\<separator\>)/', $variables['path'])) {
return '<span' . drupal_attributes($variables['options']['attributes']) . '>'
. ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text']))
. '</span>';
}
// Call the original theme function for normal menu link.
return theme('link_default',$variables);
}
/**
* Override of theme_menu_item_link()
* This function will render link if it is "nolink" or "separator". Otherwise it will call originally
* overwriten menu_item_link function.
*/
function special_menu_items_menu_link(array $variables) {
// Find special menu link
if (preg_match('/^(\<nolink\>|\<separator\>)/', $variables['element']['#href'], $matches)) {
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
switch ($matches[1]) {
case '<nolink>':
$tag = variable_get('special_menu_items_nolink_tag', '<span>');
$title = strip_tags(l($element['#title'], $element['#href'], $element['#localized_options']));
$css = NULL;
// Set a class if the link is in the active trail.
if (!empty($element['#original_link']['in_active_trail'])) {
$css = 'active-trail';
}
$output = special_menu_items_render_menu_item($tag, $title, $css);
$element['#attributes']['class'][] = 'nolink';
break;
case '<separator>':
$output = variable_get('special_menu_items_separator_value', '<hr>');
$element['#attributes']['class'][] = 'separator';
break;
}
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
// Call the original theme function for normal menu link.
return theme('menu_link_default',$variables);
}
/**
* Returns menu item rendered.
*/
function special_menu_items_render_menu_item($tag, $value, $css = NULL) {
$length = strlen($tag);
//Validate the tags
if ($tag[0] == '<' && $tag[$length - 1] == '>') {
$closingtag = str_replace('<', '</', $tag);
if ($css) {
$tag = str_replace('>', ' class="' . $css . '">', $tag);
}
} else {
if ($css) {
$classtag = '<' . $tag . ' class="' . $css . '">';
$tag = '<' . $tag . '>';
$closingtag = str_replace('<', '</', $tag);
$tag = $classtag;
} else {
$tag = '<' . $tag . '>';
$closingtag = str_replace('<', '</', $tag);
}
}
return $tag . $value . $closingtag;
}
/**
* Alter the theme's primary and secondary links.
*
*/
function special_menu_items_preprocess_page(&$vars, $hook) {
foreach (array('main_menu', 'secondary_menu') as $menu) {
foreach (array_keys($vars[$menu]) as $key) {
if (preg_match('/^(\<nolink\>|\<separator\>)/', $vars[$menu][$key]['href'], $matches)) {
switch ($matches[1]) {
case '<nolink>':
$tag = variable_get('special_menu_items_nolink_tag', '<span>');
$title = $vars[$menu][$key]['title'];
$vars[$menu][$key]['title'] = special_menu_items_render_menu_item($tag, $title);
$vars[$menu][$key]['attributes']['class'][] = 'nolink';
break;
case '<separator>':
$vars[$menu][$key]['title'] = variable_get('special_menu_items_separator_value', '<hr>');
$vars[$menu][$key]['attributes']['class'][] = 'separator';
break;
}
//render in HTML
$vars[$menu][$key]['html'] = TRUE;
unset($vars[$menu][$key]['attributes']['title']);
unset($vars[$menu][$key]['href']);
}
//unset($vars[$menu][$key]['attributes']['title']);
}
}
}
/**
* Implementation of hook_theme_registry_alter()
* We replace theme_menu_item_link with our own function.
*/
function special_menu_items_theme_registry_alter(&$registry) {
// Save previous value from registry in case another theme overwrites menu_item_link
$registry['menu_link_default'] = $registry['menu_link'];
$registry['menu_link']['function'] = 'special_menu_items_menu_link';
$registry['link_default'] = $registry['link'];
$registry['link']['function'] = 'special_menu_items_link';
}
/**
* Implementation of hook_form_FROM_ID_alter()
* Description changed, added nolink and separator as path types.
*/
function special_menu_items_form_menu_edit_item_alter(&$form, &$form_state) {
$form['link_path']['#description'] .= ' ' . t('Enter "%nolink" to generate non-linkable item, enter "%separator" to generate separator item.', array('%nolink' => '<nolink>', '%separator' => '<separator>'));
}
/**
* Special Menu Items admin settings form.
*
* @return
* The settings form used by Special Menu Items.
*/
function special_menu_items_admin_settings_form() {
$form['special_menu_items_nolink_tag'] = array(
'#type' => 'textfield',
'#title' => t('HTML tag for "nolink"'),
'#description' => t('By default, Special Menu Items will use a span tag for the nolink menu item. Here you can specify your own tag.'),
'#default_value' => variable_get('special_menu_items_nolink_tag', '<span>'),
);
$form['special_menu_items_separator_tag'] = array(
'#type' => 'textfield',
'#title' => t('HTML tag for "separator"'),
'#description' => t('By default, Special Menu Items will use a span tag for the separator menu item. Here you can specify your own tag.'),
'#default_value' => variable_get('special_menu_items_separator_tag', '<span>'),
);
$form['special_menu_items_separator_value'] = array(
'#type' => 'textfield',
'#title' => t('Value to be displayed for the "separator"'),
'#description' => t('By default, Special Menu Items will use a "<hr>" value for the separator. You can specify your own value for the separator.'),
'#default_value' => variable_get('special_menu_items_separator_value', '<hr>'),
);
return system_settings_form($form);
}
/**
* Implements hook_menu_link_update()
*
*/
/*
function special_menu_items_menu_link_update($link) {
//do all links in db
global $db_type;
if ($db_type == 'pgsql') {
db_query("UPDATE {menu_links} SET link_path=link_path||'/'||mlid WHERE (link_path='<nolink>' OR link_path='<separator>') AND hidden != -1");
}
else {
db_query("UPDATE {menu_links} SET link_path=CONCAT(CONCAT(link_path,'/'),mlid) WHERE (link_path='<nolink>' OR link_path='<separator>') AND hidden!=-1");
}
}
*
*/