Révision c8740e19
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/menu_attributes/menu_attributes.module | ||
---|---|---|
6 | 6 |
* attributes for the menu link |
7 | 7 |
*/ |
8 | 8 |
|
9 |
define('MENU_ATTRIBUTES_LINK', 'attributes'); |
|
10 |
define('MENU_ATTRIBUTES_ITEM', 'item_attributes'); |
|
11 |
|
|
9 | 12 |
/** |
10 | 13 |
* Implements hook_permission(). |
11 | 14 |
*/ |
... | ... | |
32 | 35 |
|
33 | 36 |
// Convert classes to an array. |
34 | 37 |
if (isset($item['options']['attributes']['class']) && is_string($item['options']['attributes']['class'])) { |
35 |
$item['options']['attributes']['class'] = explode(' ', $item['options']['attributes']['class']);
|
|
38 |
$item['options']['attributes']['class'] = array_filter(explode(' ', $item['options']['attributes']['class']));
|
|
36 | 39 |
} |
37 | 40 |
} |
38 | 41 |
} |
39 | 42 |
|
40 | 43 |
/** |
41 |
* Implements hook_menu_attriute_info(). |
|
44 |
* Implements hook_menu_attribute_info().
|
|
42 | 45 |
*/ |
43 | 46 |
function menu_attributes_menu_attribute_info() { |
44 | 47 |
$info['title'] = array( |
... | ... | |
48 | 51 |
'#type' => 'textarea', |
49 | 52 |
'#rows' => 2, |
50 | 53 |
), |
54 |
'scope' => array(MENU_ATTRIBUTES_LINK), |
|
51 | 55 |
); |
52 | 56 |
$info['id'] = array( |
53 | 57 |
'label' => t('ID'), |
54 | 58 |
'description' => t('Specifies a unique ID for the link.'), |
59 |
'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM), |
|
55 | 60 |
); |
56 | 61 |
$info['name'] = array( |
57 | 62 |
'label' => t('Name'), |
63 |
'scope' => array(MENU_ATTRIBUTES_LINK), |
|
58 | 64 |
); |
59 | 65 |
$info['rel'] = array( |
60 | 66 |
'label' => t('Relationship'), |
61 | 67 |
'description' => t("Specifies the relationship between the current page and the link. Enter 'nofollow' here to nofollow this link."), |
68 |
'scope' => array(MENU_ATTRIBUTES_LINK), |
|
62 | 69 |
); |
63 | 70 |
$info['class'] = array( |
64 | 71 |
'label' => t('Classes'), |
65 | 72 |
'description' => t('Enter additional classes to be added to the link.'), |
73 |
'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM), |
|
66 | 74 |
); |
67 | 75 |
$info['style'] = array( |
68 | 76 |
'label' => t('Style'), |
69 | 77 |
'description' => t('Enter additional styles to be applied to the link.'), |
78 |
'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM), |
|
70 | 79 |
); |
71 | 80 |
$info['target'] = array( |
72 | 81 |
'label' => t('Target'), |
... | ... | |
74 | 83 |
'form' => array( |
75 | 84 |
'#type' => 'select', |
76 | 85 |
'#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)',
|
|
86 |
'' => t('None (i.e. same window)'),
|
|
87 |
'_blank' => t('New window (_blank)'),
|
|
88 |
'_top' => t('Top window (_top)'),
|
|
89 |
'_self' => t('Same window (_self)'),
|
|
90 |
'_parent' => t('Parent window (_parent)'),
|
|
82 | 91 |
), |
83 | 92 |
), |
93 |
'scope' => array(MENU_ATTRIBUTES_LINK), |
|
84 | 94 |
); |
85 | 95 |
$info['accesskey'] = array( |
86 | 96 |
'label' => t('Access Key'), |
... | ... | |
89 | 99 |
'#maxlength' => 1, |
90 | 100 |
'#size' => 1, |
91 | 101 |
), |
102 |
'scope' => array(MENU_ATTRIBUTES_LINK), |
|
92 | 103 |
); |
93 | 104 |
return $info; |
94 | 105 |
} |
... | ... | |
98 | 109 |
*/ |
99 | 110 |
function menu_attributes_get_menu_attribute_info() { |
100 | 111 |
$attributes = module_invoke_all('menu_attribute_info'); |
101 |
drupal_alter('menu_attribute_info', $attributes); |
|
102 | 112 |
|
103 |
// Merge in defaul values. |
|
113 |
// Merge in default values.
|
|
104 | 114 |
foreach ($attributes as $attribute => &$info) { |
105 | 115 |
$info += array( |
106 | 116 |
'form' => array(), |
... | ... | |
114 | 124 |
'#default_value' => variable_get("menu_attributes_{$attribute}_default", $info['default']), |
115 | 125 |
); |
116 | 126 |
} |
127 |
drupal_alter('menu_attribute_info', $attributes); |
|
117 | 128 |
|
118 | 129 |
return $attributes; |
119 | 130 |
} |
... | ... | |
140 | 151 |
* @see _menu_attributes_form_alter() |
141 | 152 |
*/ |
142 | 153 |
function menu_attributes_form_node_form_alter(&$form, $form_state) { |
143 |
if (isset($form['menu']['link'])) { |
|
154 |
if (isset($form['menu']['link']) && isset($form['#node']->menu)) {
|
|
144 | 155 |
$item = $form['#node']->menu; |
145 | 156 |
_menu_attributes_form_alter($form['menu']['link'], $item, $form); |
146 |
$form['menu']['link']['options']['attributes']['#type'] = 'container'; |
|
147 | 157 |
} |
148 | 158 |
} |
149 | 159 |
|
... | ... | |
156 | 166 |
* The optional existing menu item for context. |
157 | 167 |
*/ |
158 | 168 |
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 | 169 |
$form['options']['#tree'] = TRUE; |
163 | 170 |
$form['options']['#weight'] = 50; |
164 | 171 |
|
165 | 172 |
// Unset the previous value so that the new values get saved. |
166 | 173 |
unset($form['options']['#value']['attributes']); |
174 |
unset($form['options']['#value']['item_attributes']); |
|
167 | 175 |
|
168 |
$form['options']['attributes'] = array(
|
|
176 |
$form['options'][MENU_ATTRIBUTES_LINK] = array(
|
|
169 | 177 |
'#type' => 'fieldset', |
170 |
'#title' => t('Menu item attributes'),
|
|
178 |
'#title' => t('Menu link attributes'),
|
|
171 | 179 |
'#collapsible' => TRUE, |
172 | 180 |
'#collapsed' => FALSE, |
173 | 181 |
'#tree' => TRUE, |
174 | 182 |
); |
175 | 183 |
|
184 |
$form['options'][MENU_ATTRIBUTES_ITEM] = array( |
|
185 |
'#type' => 'fieldset', |
|
186 |
'#title' => t('Menu item attributes'), |
|
187 |
'#collapsible' => TRUE, |
|
188 |
'#collapsed' => TRUE, |
|
189 |
'#tree' => TRUE, |
|
190 |
); |
|
191 |
|
|
176 | 192 |
$attributes = menu_attributes_get_menu_attribute_info(); |
177 | 193 |
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 |
} |
|
194 |
// If no scope is set, this attribute should be available to both link |
|
195 |
// and item. |
|
196 |
if (!isset($info['scope'])) { |
|
197 |
$info['scope'] = array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM); |
|
186 | 198 |
} |
187 |
elseif ($item['mlid']) { |
|
188 |
// If this is an existing link, use the raw default (usually empty). |
|
189 |
$info['form']['#default_value'] = $info['default']; |
|
199 |
|
|
200 |
// Define fields for each scope. |
|
201 |
foreach ($info['scope'] as $group) { |
|
202 |
// Merge in the proper default value. |
|
203 |
if (isset($item['options'][$group][$attribute])) { |
|
204 |
// If the menu link already has this attribute, use it. |
|
205 |
$info['form']['#default_value'] = $item['options'][$group][$attribute]; |
|
206 |
// Convert the classes array to a string for the form. |
|
207 |
if ($attribute == 'class' && is_array($info['form']['#default_value'])) { |
|
208 |
$info['form']['#default_value'] = implode(' ', $info['form']['#default_value']); |
|
209 |
} |
|
210 |
} |
|
211 |
elseif ($item['mlid']) { |
|
212 |
// If this is an existing link, use the raw default (usually empty). |
|
213 |
$info['form']['#default_value'] = $info['default']; |
|
214 |
} |
|
215 |
$form['options'][$group][$attribute] = $info['form'] + array( |
|
216 |
'#access' => $info['enabled'], |
|
217 |
); |
|
190 | 218 |
} |
191 |
$form['options']['attributes'][$attribute] = $info['form'] + array( |
|
192 |
'#access' => $info['enabled'], |
|
193 |
); |
|
194 | 219 |
} |
195 | 220 |
|
196 | 221 |
// Add form values for the reset of $item['options'] and |
... | ... | |
203 | 228 |
); |
204 | 229 |
} |
205 | 230 |
} |
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 |
); |
|
231 |
foreach (array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM) as $group) { |
|
232 |
if (isset($item['options'][$group])) { |
|
233 |
foreach ($item['options'][$group] as $key => $value) { |
|
234 |
if (!isset($form['options'][$group][$key])) { |
|
235 |
$form['options'][$group][$key] = array( |
|
236 |
'#type' => 'value', |
|
237 |
'#value' => $value, |
|
238 |
); |
|
239 |
} |
|
213 | 240 |
} |
214 | 241 |
} |
215 | 242 |
} |
... | ... | |
224 | 251 |
array_unshift($complete_form['#submit'], '_menu_attributes_form_submit'); |
225 | 252 |
} |
226 | 253 |
|
227 |
$form['options']['attributes']['#access'] = (bool) element_get_visible_children($form['options']['attributes']); |
|
254 |
// Restrict access to the new form elements. |
|
255 |
$has_visible_children = (bool) element_get_visible_children($form['options']['attributes']); |
|
256 |
$user_has_access = user_access('administer menu attributes'); |
|
257 |
$form['options']['attributes']['#access'] = ($has_visible_children && $user_has_access); |
|
228 | 258 |
} |
229 | 259 |
|
230 | 260 |
/** |
... | ... | |
289 | 319 |
) + $info['form']; |
290 | 320 |
} |
291 | 321 |
} |
322 |
|
|
323 |
/** |
|
324 |
* Implements MODULE_preprocess_HOOK(). |
|
325 |
* |
|
326 |
* Adds appropriate attributes to the list item. |
|
327 |
* |
|
328 |
* @see theme_menu_link() |
|
329 |
*/ |
|
330 |
function menu_attributes_preprocess_menu_link(&$variables) { |
|
331 |
$options = &$variables['element']['#localized_options']; |
|
332 |
$attributes = &$variables['element']['#attributes']; |
|
333 |
|
|
334 |
if (isset($options['item_attributes'])) { |
|
335 |
foreach ($options['item_attributes'] as $attribute => $value) { |
|
336 |
if (!empty($value)) { |
|
337 |
// Class get's special treatment, as it's an array and it should not |
|
338 |
// replace existing values. |
|
339 |
if ($attribute == 'class') { |
|
340 |
$value = explode(' ', $value); |
|
341 |
if (isset($attributes[$attribute])) { |
|
342 |
$value = array_merge($attributes[$attribute], $value); |
|
343 |
} |
|
344 |
} |
|
345 |
// Override the attribute. |
|
346 |
$attributes[$attribute] = $value; |
|
347 |
} |
|
348 |
} |
|
349 |
|
|
350 |
// Clean up, so we're not passing this to l(). |
|
351 |
unset($options['item_attributes']); |
|
352 |
} |
|
353 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules