-
Notifications
You must be signed in to change notification settings - Fork 0
/
special_menu_items.module
209 lines (176 loc) · 6.75 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
<?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.
*
*/
/**
* Implements hook_config_info().
*/
function special_menu_items_config_info() {
$prefixes['special_menu_items.settings'] = array(
'label' => t('Special Menu Items settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
*Implementation of hook_menu()
*/
function special_menu_items_menu() {
$items['<nolink>'] = array(
'page callback' => 'backdrop_not_found',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['<separator>'] = array(
'page callback' => 'backdrop_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' => 'backdrop_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_menu_link()
* This function will render link if it is "nolink" or "separator". Otherwise it will call originally
* overwritten menu_item_link function.
*/
function special_menu_items_menu_link($variables) {
if (in_array($variables['element']['#href'], array('<nolink>', '<separator>'))) {
$config = config('special_menu_items.settings');
switch ($variables['element']['#href']) {
case '<nolink>':
$tag = $config->get('nolink_tag');
$title = $variables['element']['#title'];
$variables['element']['#attributes']['class'][] = 'nolink';
break;
case '<separator>':
$tag = $config->get('separator_tag');
$title = $config->get('separator_value');
$variables['element']['#attributes']['class'][] = 'separator';
break;
}
$attributes = backdrop_attributes($variables['element']['#attributes']);
if ($tag != '<a>') {
// <a> tags can have these but a <span> cannot, so we remove them.
foreach (array('accesskey', 'target', 'rel', 'name') as $attribute) {
$attributes = preg_replace("/ $attribute=\".*\"/i", "", $attributes);
}
}
return special_menu_items_render_menu_item($tag, $title, $attributes);
}
// Call the original theme function for normal menu link.
return theme('special_menu_items_link_default', $variables);
}
/**
* Returns menu item rendered.
*/
function special_menu_items_render_menu_item($tag, $value, $attrs = array()) {
// $attrs may be a string already or an array
if (is_array($attrs)) {
$attrs = backdrop_attributes($attrs);
}
$length = strlen($tag);
if ($tag[0] == '<' && $tag[$length - 1] == '>') {
$tag = substr($tag, 1, $length-2);
}
$closingtag = explode(' ', $tag,2);
$closingtag = '</' . $closingtag[0] . '>';
$tag = '<' . $tag . $attrs . '>';
return $tag . $value . $closingtag;
}
/**
* 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['special_menu_items_link_default'] = $registry['menu_link'];
$registry['menu_link']['function'] = 'special_menu_items_menu_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) {
// Some menu items have a pre-defined path which cannot be modified hence no default_value
if (isset($form['link_path']['#default_value'])) {
$default_value = $form['link_path']['#default_value'];
if (preg_match('/^<nolink>\/[0-9]+$/', $default_value)) {
$default_value = '<nolink>';
}
elseif (preg_match('/^<separator>\/[0-9]+$/', $default_value)) {
$default_value = '<separator>';
}
$form['link_path']['#default_value'] = $default_value;
$form['link_path']['#description'] .= ' ' . t('Enter "%nolink" to generate non-linkable item, enter "%separator" to generate separator item.', array('%nolink' => '<nolink>', '%separator' => '<separator>'));
}
}
/**
* Implementation of hook_init().
*/
function special_menu_items_init() {
// Make breadcrumb of nolink menu item nonlinkable.
$breadcrumb = backdrop_get_breadcrumb();
foreach($breadcrumb as $key => $crumb){
if (strlen(strstr($crumb,'<nolink>')) > 0) {
$crumb = strip_tags($crumb);
$tag = config_get('special_menu_items.settings', 'nolink_tag');
$breadcrumb[$key] = special_menu_items_render_menu_item($tag, $crumb);
}
}
backdrop_set_breadcrumb($breadcrumb);
}
/**
* Special Menu Items admin settings form.
*
* @return
* The settings form used by Special Menu Items.
*/
function special_menu_items_admin_settings_form() {
$config = config('special_menu_items.settings');
$form['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' => $config->get('nolink_tag'),
);
$form['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' => $config->get('separator_tag'),
);
$form['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' => $config->get('separator_value'),
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function special_menu_items_admin_settings_form_submit($form, &$form_state) {
$config = config('special_menu_items.settings');
$config->set('nolink_tag', $form_state['values']['nolink_tag']);
$config->set('separator_tag', $form_state['values']['separator_tag']);
$config->set('separator_value', $form_state['values']['separator_value']);
$config->save();
backdrop_set_message(t('Settings saved'), 'status');
}