menu_attributes.module
11.5 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* @file
* Alters the menu item form to allow the administrator to specify additional
* attributes for the menu link
*/
define('MENU_ATTRIBUTES_LINK', 'attributes');
define('MENU_ATTRIBUTES_ITEM', 'item_attributes');
/**
* Implements hook_permission().
*/
function menu_attributes_permission() {
return array(
'administer menu attributes' => array(
'title' => t('Administer menu attributes'),
'description' => t('Administer menu attributes.'),
),
);
}
/**
* Implements hook_menu_link_alter().
*/
function menu_attributes_menu_link_alter(&$item, $menu) {
if (isset($item['options']['attributes']) && is_array($item['options']['attributes'])) {
// Filter out blank attributes.
foreach ($item['options']['attributes'] as $key => $value) {
if ((is_array($value) && empty($value)) || is_string($value) && !drupal_strlen($value)) {
unset($item['options']['attributes'][$key]);
}
}
// Convert classes to an array.
if (isset($item['options']['attributes']['class']) && is_string($item['options']['attributes']['class'])) {
$item['options']['attributes']['class'] = array_filter(explode(' ', $item['options']['attributes']['class']));
}
}
}
/**
* Implements hook_menu_attribute_info().
*/
function menu_attributes_menu_attribute_info() {
$info['title'] = array(
'label' => t('Title'),
'description' => t('The description displayed when hovering over the link.'),
'form' => array(
'#type' => 'textarea',
'#rows' => 2,
),
'scope' => array(MENU_ATTRIBUTES_LINK),
);
$info['id'] = array(
'label' => t('ID'),
'description' => t('Specifies a unique ID for the link.'),
'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM),
);
$info['name'] = array(
'label' => t('Name'),
'scope' => array(MENU_ATTRIBUTES_LINK),
);
$info['rel'] = array(
'label' => t('Relationship'),
'description' => t("Specifies the relationship between the current page and the link. Enter 'nofollow' here to nofollow this link."),
'scope' => array(MENU_ATTRIBUTES_LINK),
);
$info['class'] = array(
'label' => t('Classes'),
'description' => t('Enter additional classes to be added to the link.'),
'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM),
);
$info['style'] = array(
'label' => t('Style'),
'description' => t('Enter additional styles to be applied to the link.'),
'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM),
);
$info['target'] = array(
'label' => t('Target'),
'description' => t('Specifies where to open the link. Using this attribute breaks XHTML validation.'),
'form' => array(
'#type' => 'select',
'#options' => array(
'' => t('None (i.e. same window)'),
'_blank' => t('New window (_blank)'),
'_top' => t('Top window (_top)'),
'_self' => t('Same window (_self)'),
'_parent' => t('Parent window (_parent)'),
),
),
'scope' => array(MENU_ATTRIBUTES_LINK),
);
$info['accesskey'] = array(
'label' => t('Access Key'),
'description' => t('Specifies a <a href="@accesskey">keyboard shortcut</a> to access this link.', array('@accesskey' => url('http://en.wikipedia.org/wiki/Access_keys'))),
'form' => array(
'#maxlength' => 1,
'#size' => 1,
),
'scope' => array(MENU_ATTRIBUTES_LINK),
);
return $info;
}
/**
* Fetch an array of menu attributes.
*/
function menu_attributes_get_menu_attribute_info() {
$attributes = module_invoke_all('menu_attribute_info');
// Merge in default values.
foreach ($attributes as $attribute => &$info) {
$info += array(
'form' => array(),
'enabled' => variable_get("menu_attributes_{$attribute}_enable", 1),
'default' => '',
);
$info['form'] += array(
'#type' => 'textfield',
'#title' => $info['label'],
'#description' => isset($info['description']) ? $info['description'] : '',
'#default_value' => variable_get("menu_attributes_{$attribute}_default", $info['default']),
);
}
drupal_alter('menu_attribute_info', $attributes);
return $attributes;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds menu attribute options to the edit menu item form.
*
* @see menu_edit_item()
* @see _menu_attributes_form_alter()
* @see menu_attributes_form_menu_edit_item_submit()
*/
function menu_attributes_form_menu_edit_item_alter(&$form, $form_state) {
$item = $form['original_item']['#value'];
_menu_attributes_form_alter($form, $item, $form);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds menu attribute options to the node's edit menu item form.
*
* @see _menu_attributes_form_alter()
*/
function menu_attributes_form_node_form_alter(&$form, $form_state) {
if (isset($form['menu']['link']) && isset($form['#node']->menu)) {
$item = $form['#node']->menu;
_menu_attributes_form_alter($form['menu']['link'], $item, $form);
}
}
/**
* Add the menu attributes to a menu item edit form.
*
* @param $form
* The menu item edit form passed by reference.
* @param $item
* The optional existing menu item for context.
*/
function _menu_attributes_form_alter(array &$form, array $item = array(), array &$complete_form) {
$form['options']['#tree'] = TRUE;
$form['options']['#weight'] = 50;
// Unset the previous value so that the new values get saved.
unset($form['options']['#value']['attributes']);
unset($form['options']['#value']['item_attributes']);
$form['options'][MENU_ATTRIBUTES_LINK] = array(
'#type' => 'fieldset',
'#title' => t('Menu link attributes'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
$form['options'][MENU_ATTRIBUTES_ITEM] = array(
'#type' => 'fieldset',
'#title' => t('Menu item attributes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$attributes = menu_attributes_get_menu_attribute_info();
foreach ($attributes as $attribute => $info) {
// If no scope is set, this attribute should be available to both link
// and item.
if (!isset($info['scope'])) {
$info['scope'] = array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM);
}
// Define fields for each scope.
foreach ($info['scope'] as $group) {
// Merge in the proper default value.
if (isset($item['options'][$group][$attribute])) {
// If the menu link already has this attribute, use it.
$info['form']['#default_value'] = $item['options'][$group][$attribute];
// Convert the classes array to a string for the form.
if ($attribute == 'class' && is_array($info['form']['#default_value'])) {
$info['form']['#default_value'] = implode(' ', $info['form']['#default_value']);
}
}
elseif ($item['mlid']) {
// If this is an existing link, use the raw default (usually empty).
$info['form']['#default_value'] = $info['default'];
}
$form['options'][$group][$attribute] = $info['form'] + array(
'#access' => $info['enabled'],
);
}
}
// Add form values for the reset of $item['options'] and
// $item['options']['attributes'] so the values will carry over during save.
foreach ($item['options'] as $key => $value) {
if ($key !== 'attributes' && !isset($form['options'][$key])) {
$form['options'][$key] = array(
'#type' => 'value',
'#value' => $value,
);
}
}
foreach (array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM) as $group) {
if (isset($item['options'][$group])) {
foreach ($item['options'][$group] as $key => $value) {
if (!isset($form['options'][$group][$key])) {
$form['options'][$group][$key] = array(
'#type' => 'value',
'#value' => $value,
);
}
}
}
}
// Hide the 'description' field since we will be using our own 'title' field.
if (isset($form['description'])) {
$form['description']['#access'] = FALSE;
// Because this form uses a special $form['description'] field which is
// really the 'title' attribute, we need to add special pre-submit handling
// to ensure our field gets saved as the title attribute.
array_unshift($complete_form['#submit'], '_menu_attributes_form_submit');
}
// Restrict access to the new form elements.
$has_visible_children = (bool) element_get_visible_children($form['options']['attributes']);
$user_has_access = user_access('administer menu attributes');
$form['options']['attributes']['#access'] = ($has_visible_children && $user_has_access);
}
/**
* Form submit handler for menu item links.
*
* Move the title attributes value into the 'description' value so that it
* will get properly saved.
*/
function _menu_attributes_form_submit($form, &$form_state) {
if (isset($form_state['values']['menu']['options']['attributes']['title'])) {
$form_state['values']['menu']['description'] = $form_state['values']['menu']['options']['attributes']['title'];
}
elseif (isset($form_state['values']['options']['attributes']['title'])) {
$form_state['values']['description'] = $form_state['values']['options']['attributes']['title'];
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters the menu settings form with our menu attribute settings.
*
* @see menu_configure_form()
*/
function menu_attributes_form_menu_configure_alter(&$form, $form_state) {
if (!user_access('administer menu attributes')) {
return;
}
$form['attributes_title'] = array(
'#type' => 'item',
'#title' => t('Menu item attribute options'),
);
$form['attributes_vertical_tabs'] = array(
'#type' => 'vertical_tabs',
'#attached' => array(
'js' => array(drupal_get_path('module', 'menu_attributes') . '/menu_attributes.js'),
),
);
$attributes = menu_attributes_get_menu_attribute_info();
foreach ($attributes as $attribute => $info) {
$form['attributes'][$attribute] = array(
'#type' => 'fieldset',
'#title' => $info['label'],
'#group' => 'attributes_vertical_tabs',
'#description' => $info['form']['#description'],
);
$form['attributes'][$attribute]["menu_attributes_{$attribute}_enable"] = array(
'#type' => 'checkbox',
'#title' => t('Enable the @attribute attribute.', array('@attribute' => drupal_strtolower($info['label']))),
'#default_value' => $info['enabled'],
);
$form['attributes'][$attribute]["menu_attributes_{$attribute}_default"] = array(
'#title' => t('Default'),
'#description' => '',
'#states' => array(
'invisible' => array(
'input[name="menu_attributes_' . $attribute . '_enable"]' => array('checked' => FALSE),
),
),
) + $info['form'];
}
}
/**
* Implements MODULE_preprocess_HOOK().
*
* Adds appropriate attributes to the list item.
*
* @see theme_menu_link()
*/
function menu_attributes_preprocess_menu_link(&$variables) {
$options = &$variables['element']['#localized_options'];
$attributes = &$variables['element']['#attributes'];
if (isset($options['item_attributes'])) {
foreach ($options['item_attributes'] as $attribute => $value) {
if (!empty($value)) {
// Class get's special treatment, as it's an array and it should not
// replace existing values.
if ($attribute == 'class') {
$value = explode(' ', $value);
if (isset($attributes[$attribute])) {
$value = array_merge($attributes[$attribute], $value);
}
}
// Override the attribute.
$attributes[$attribute] = $value;
}
}
// Clean up, so we're not passing this to l().
unset($options['item_attributes']);
}
}