1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Alters the menu item form to allow the administrator to specify additional
|
6
|
* attributes for the menu link
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Implements hook_permission().
|
11
|
*/
|
12
|
function menu_attributes_permission() {
|
13
|
return array(
|
14
|
'administer menu attributes' => array(
|
15
|
'title' => t('Administer menu attributes'),
|
16
|
'description' => t('Administer menu attributes.'),
|
17
|
),
|
18
|
);
|
19
|
}
|
20
|
|
21
|
/**
|
22
|
* Implements hook_menu_link_alter().
|
23
|
*/
|
24
|
function menu_attributes_menu_link_alter(&$item, $menu) {
|
25
|
if (isset($item['options']['attributes']) && is_array($item['options']['attributes'])) {
|
26
|
// Filter out blank attributes.
|
27
|
foreach ($item['options']['attributes'] as $key => $value) {
|
28
|
if ((is_array($value) && empty($value)) || is_string($value) && !drupal_strlen($value)) {
|
29
|
unset($item['options']['attributes'][$key]);
|
30
|
}
|
31
|
}
|
32
|
|
33
|
// Convert classes to an array.
|
34
|
if (isset($item['options']['attributes']['class']) && is_string($item['options']['attributes']['class'])) {
|
35
|
$item['options']['attributes']['class'] = explode(' ', $item['options']['attributes']['class']);
|
36
|
}
|
37
|
}
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Implements hook_menu_attriute_info().
|
42
|
*/
|
43
|
function menu_attributes_menu_attribute_info() {
|
44
|
$info['title'] = array(
|
45
|
'label' => t('Title'),
|
46
|
'description' => t('The description displayed when hovering over the link.'),
|
47
|
'form' => array(
|
48
|
'#type' => 'textarea',
|
49
|
'#rows' => 2,
|
50
|
),
|
51
|
);
|
52
|
$info['id'] = array(
|
53
|
'label' => t('ID'),
|
54
|
'description' => t('Specifies a unique ID for the link.'),
|
55
|
);
|
56
|
$info['name'] = array(
|
57
|
'label' => t('Name'),
|
58
|
);
|
59
|
$info['rel'] = array(
|
60
|
'label' => t('Relationship'),
|
61
|
'description' => t("Specifies the relationship between the current page and the link. Enter 'nofollow' here to nofollow this link."),
|
62
|
);
|
63
|
$info['class'] = array(
|
64
|
'label' => t('Classes'),
|
65
|
'description' => t('Enter additional classes to be added to the link.'),
|
66
|
);
|
67
|
$info['style'] = array(
|
68
|
'label' => t('Style'),
|
69
|
'description' => t('Enter additional styles to be applied to the link.'),
|
70
|
);
|
71
|
$info['target'] = array(
|
72
|
'label' => t('Target'),
|
73
|
'description' => t('Specifies where to open the link. Using this attribute breaks XHTML validation.'),
|
74
|
'form' => array(
|
75
|
'#type' => 'select',
|
76
|
'#options' => array(
|
77
|
'' => 'None (i.e. same window)',
|
78
|
'_blank' => 'New window (_blank)',
|
79
|
'_top' => 'Top window (_top)',
|
80
|
'_self' => 'Same window (_self)',
|
81
|
'_parent' => 'Parent window (_parent)',
|
82
|
),
|
83
|
),
|
84
|
);
|
85
|
$info['accesskey'] = array(
|
86
|
'label' => t('Access Key'),
|
87
|
'description' => t('Specifies a <a href="@accesskey">keyboard shortcut</a> to access this link.', array('@accesskey' => url('http://en.wikipedia.org/wiki/Access_keys'))),
|
88
|
'form' => array(
|
89
|
'#maxlength' => 1,
|
90
|
'#size' => 1,
|
91
|
),
|
92
|
);
|
93
|
return $info;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Fetch an array of menu attributes.
|
98
|
*/
|
99
|
function menu_attributes_get_menu_attribute_info() {
|
100
|
$attributes = module_invoke_all('menu_attribute_info');
|
101
|
drupal_alter('menu_attribute_info', $attributes);
|
102
|
|
103
|
// Merge in defaul values.
|
104
|
foreach ($attributes as $attribute => &$info) {
|
105
|
$info += array(
|
106
|
'form' => array(),
|
107
|
'enabled' => variable_get("menu_attributes_{$attribute}_enable", 1),
|
108
|
'default' => '',
|
109
|
);
|
110
|
$info['form'] += array(
|
111
|
'#type' => 'textfield',
|
112
|
'#title' => $info['label'],
|
113
|
'#description' => isset($info['description']) ? $info['description'] : '',
|
114
|
'#default_value' => variable_get("menu_attributes_{$attribute}_default", $info['default']),
|
115
|
);
|
116
|
}
|
117
|
|
118
|
return $attributes;
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Implements hook_form_FORM_ID_alter().
|
123
|
*
|
124
|
* Adds menu attribute options to the edit menu item form.
|
125
|
*
|
126
|
* @see menu_edit_item()
|
127
|
* @see _menu_attributes_form_alter()
|
128
|
* @see menu_attributes_form_menu_edit_item_submit()
|
129
|
*/
|
130
|
function menu_attributes_form_menu_edit_item_alter(&$form, $form_state) {
|
131
|
$item = $form['original_item']['#value'];
|
132
|
_menu_attributes_form_alter($form, $item, $form);
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Implements hook_form_FORM_ID_alter().
|
137
|
*
|
138
|
* Adds menu attribute options to the node's edit menu item form.
|
139
|
*
|
140
|
* @see _menu_attributes_form_alter()
|
141
|
*/
|
142
|
function menu_attributes_form_node_form_alter(&$form, $form_state) {
|
143
|
if (isset($form['menu']['link'])) {
|
144
|
$item = $form['#node']->menu;
|
145
|
_menu_attributes_form_alter($form['menu']['link'], $item, $form);
|
146
|
$form['menu']['link']['options']['attributes']['#type'] = 'container';
|
147
|
}
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* Add the menu attributes to a menu item edit form.
|
152
|
*
|
153
|
* @param $form
|
154
|
* The menu item edit form passed by reference.
|
155
|
* @param $item
|
156
|
* The optional existing menu item for context.
|
157
|
*/
|
158
|
function _menu_attributes_form_alter(array &$form, array $item = array(), array &$complete_form) {
|
159
|
// Restrict access to the new form elements.
|
160
|
$form['options']['attributes']['#access'] = user_access('administer menu attributes');
|
161
|
|
162
|
$form['options']['#tree'] = TRUE;
|
163
|
$form['options']['#weight'] = 50;
|
164
|
|
165
|
// Unset the previous value so that the new values get saved.
|
166
|
unset($form['options']['#value']['attributes']);
|
167
|
|
168
|
$form['options']['attributes'] = array(
|
169
|
'#type' => 'fieldset',
|
170
|
'#title' => t('Menu item attributes'),
|
171
|
'#collapsible' => TRUE,
|
172
|
'#collapsed' => FALSE,
|
173
|
'#tree' => TRUE,
|
174
|
);
|
175
|
|
176
|
$attributes = menu_attributes_get_menu_attribute_info();
|
177
|
foreach ($attributes as $attribute => $info) {
|
178
|
// Merge in the proper default value.
|
179
|
if (isset($item['options']['attributes'][$attribute])) {
|
180
|
// If the menu link already has this attribute, use it.
|
181
|
$info['form']['#default_value'] = $item['options']['attributes'][$attribute];
|
182
|
// Convert the classes array to a string for the form.
|
183
|
if ($attribute == 'class' && is_array($info['form']['#default_value'])) {
|
184
|
$info['form']['#default_value'] = implode(' ', $info['form']['#default_value']);
|
185
|
}
|
186
|
}
|
187
|
elseif ($item['mlid']) {
|
188
|
// If this is an existing link, use the raw default (usually empty).
|
189
|
$info['form']['#default_value'] = $info['default'];
|
190
|
}
|
191
|
$form['options']['attributes'][$attribute] = $info['form'] + array(
|
192
|
'#access' => $info['enabled'],
|
193
|
);
|
194
|
}
|
195
|
|
196
|
// Add form values for the reset of $item['options'] and
|
197
|
// $item['options']['attributes'] so the values will carry over during save.
|
198
|
foreach ($item['options'] as $key => $value) {
|
199
|
if ($key !== 'attributes' && !isset($form['options'][$key])) {
|
200
|
$form['options'][$key] = array(
|
201
|
'#type' => 'value',
|
202
|
'#value' => $value,
|
203
|
);
|
204
|
}
|
205
|
}
|
206
|
if (isset($item['options']['attributes'])) {
|
207
|
foreach ($item['options']['attributes'] as $key => $value) {
|
208
|
if (!isset($form['options']['attributes'][$key])) {
|
209
|
$form['options']['attributes'][$key] = array(
|
210
|
'#type' => 'value',
|
211
|
'#value' => $value,
|
212
|
);
|
213
|
}
|
214
|
}
|
215
|
}
|
216
|
|
217
|
// Hide the 'description' field since we will be using our own 'title' field.
|
218
|
if (isset($form['description'])) {
|
219
|
$form['description']['#access'] = FALSE;
|
220
|
|
221
|
// Because this form uses a special $form['description'] field which is
|
222
|
// really the 'title' attribute, we need to add special pre-submit handling
|
223
|
// to ensure our field gets saved as the title attribute.
|
224
|
array_unshift($complete_form['#submit'], '_menu_attributes_form_submit');
|
225
|
}
|
226
|
|
227
|
$form['options']['attributes']['#access'] = (bool) element_get_visible_children($form['options']['attributes']);
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Form submit handler for menu item links.
|
232
|
*
|
233
|
* Move the title attributes value into the 'description' value so that it
|
234
|
* will get properly saved.
|
235
|
*/
|
236
|
function _menu_attributes_form_submit($form, &$form_state) {
|
237
|
if (isset($form_state['values']['menu']['options']['attributes']['title'])) {
|
238
|
$form_state['values']['menu']['description'] = $form_state['values']['menu']['options']['attributes']['title'];
|
239
|
}
|
240
|
elseif (isset($form_state['values']['options']['attributes']['title'])) {
|
241
|
$form_state['values']['description'] = $form_state['values']['options']['attributes']['title'];
|
242
|
}
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Implements hook_form_FORM_ID_alter().
|
247
|
*
|
248
|
* Alters the menu settings form with our menu attribute settings.
|
249
|
*
|
250
|
* @see menu_configure_form()
|
251
|
*/
|
252
|
function menu_attributes_form_menu_configure_alter(&$form, $form_state) {
|
253
|
if (!user_access('administer menu attributes')) {
|
254
|
return;
|
255
|
}
|
256
|
|
257
|
$form['attributes_title'] = array(
|
258
|
'#type' => 'item',
|
259
|
'#title' => t('Menu item attribute options'),
|
260
|
);
|
261
|
$form['attributes_vertical_tabs'] = array(
|
262
|
'#type' => 'vertical_tabs',
|
263
|
'#attached' => array(
|
264
|
'js' => array(drupal_get_path('module', 'menu_attributes') . '/menu_attributes.js'),
|
265
|
),
|
266
|
);
|
267
|
|
268
|
$attributes = menu_attributes_get_menu_attribute_info();
|
269
|
foreach ($attributes as $attribute => $info) {
|
270
|
$form['attributes'][$attribute] = array(
|
271
|
'#type' => 'fieldset',
|
272
|
'#title' => $info['label'],
|
273
|
'#group' => 'attributes_vertical_tabs',
|
274
|
'#description' => $info['form']['#description'],
|
275
|
);
|
276
|
$form['attributes'][$attribute]["menu_attributes_{$attribute}_enable"] = array(
|
277
|
'#type' => 'checkbox',
|
278
|
'#title' => t('Enable the @attribute attribute.', array('@attribute' => drupal_strtolower($info['label']))),
|
279
|
'#default_value' => $info['enabled'],
|
280
|
);
|
281
|
$form['attributes'][$attribute]["menu_attributes_{$attribute}_default"] = array(
|
282
|
'#title' => t('Default'),
|
283
|
'#description' => '',
|
284
|
'#states' => array(
|
285
|
'invisible' => array(
|
286
|
'input[name="menu_attributes_' . $attribute . '_enable"]' => array('checked' => FALSE),
|
287
|
),
|
288
|
),
|
289
|
) + $info['form'];
|
290
|
}
|
291
|
}
|