/[drupal]/drupal/modules/menu/menu.module
ViewVC logotype

Contents of /drupal/modules/menu/menu.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.216 - (show annotations) (download) (as text)
Sat Nov 7 13:35:20 2009 UTC (3 weeks ago) by dries
Branch: MAIN
Changes since 1.215: +3 -3 lines
File MIME type: text/x-php
- Patch #607244 by sun: added permission to decrease performance impact of contextual links.
1 <?php
2 // $Id: menu.module,v 1.215 2009/11/06 03:17:59 webchick Exp $
3
4 /**
5 * @file
6 * Allows administrators to customize the site navigation menu.
7 */
8
9 /**
10 * Maximum length of menu name as entered by the user. Database length is 32
11 * and we add a menu- prefix.
12 */
13 define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
14
15 /**
16 * Implement hook_help().
17 */
18 function menu_help($path, $arg) {
19 switch ($path) {
20 case 'admin/help#menu':
21 $output = '<p>' . t("The menu module provides an interface to control and customize Drupal's powerful menu system. Menus are a hierarchical collection of links used to navigate a website. Each menu is rendered in a block that may be positioned and displayed using Drupal's flexible block system. Five menus are provided by Drupal and are always present: <em>Navigation</em>, <em>Management</em>, <em>User menu</em>, <em>Main menu</em>, and <em>Secondary menu</em>. The <em>Management</em> menu contains links for administration and content creation, while the <em>Navigation</em> menu is the default location for site navigation links created by newly enabled modules. Both of these are often displayed in either the left or right sidebar. Most Drupal themes also provide support for the <em>Main links</em> and <em>Secondary links</em>, by displaying them in either the header or footer of each page. The <em>Main menu</em> is the default source for the <em>Main links</em> and the <em>User menu</em> is the default source for the <em>Secondary links</em>. By default, the <em>User menu</em> has links to take the current user to their account or allow them to log out, while the <em>Main menu</em> and <em>Secondary menu</em> contain no menu links but may be configured to contain custom menu items specific to your site. You may create an unlimited number of additional menus, each of which will automatically have an associated block.") . '</p>';
22 $output .= '<p>' . t('The <a href="@menu">menus page</a> displays all menus currently available on your site. Select a menu from this list to add or edit a menu link, or to rearrange links within the menu. Create new menus using the <a href="@add-menu">add menu page</a> (the block containing a new menu must also be enabled on the <a href="@blocks">blocks administration page</a>).', array('@menu' => url('admin/structure/menu'), '@add-menu' => url('admin/structure/menu/add'), '@blocks' => url('admin/structure/block'))) . '</p>';
23 $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@menu">Menu module</a>.', array('@menu' => 'http://drupal.org/handbook/modules/menu/')) . '</p>';
24 return $output;
25 case 'admin/structure/menu/add':
26 return '<p>' . t('Enter the name for your new menu. Remember to enable the newly created block in the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
27 case 'admin/structure/menu/item/add':
28 return '<p>' . t('Enter the title and path for your new menu link.') . '</p>';
29 }
30 if ($path == 'admin/structure/menu' && module_exists('block')) {
31 return '<p>' . t('Each menu has a corresponding block that is managed on the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
32 }
33 }
34
35 /**
36 * Implement hook_permission().
37 */
38 function menu_permission() {
39 return array(
40 'administer menu' => array(
41 'title' => t('Administer menu'),
42 'description' => t('Manage menus and menu items.'),
43 ),
44 );
45 }
46
47 /**
48 * Implement hook_menu().
49 */
50 function menu_menu() {
51 $items['admin/structure/menu'] = array(
52 'title' => 'Menus',
53 'description' => 'Add new menus to your site, edit existing menus, and rename and reorganize menu links.',
54 'page callback' => 'menu_overview_page',
55 'access callback' => 'user_access',
56 'access arguments' => array('administer menu'),
57 'file' => 'menu.admin.inc',
58 );
59 $items['admin/structure/menu/parents'] = array(
60 'title' => 'Parent menu items',
61 'page callback' => 'menu_parent_options_js',
62 'type' => MENU_CALLBACK,
63 'access arguments' => array(TRUE),
64 );
65 $items['admin/structure/menu/list'] = array(
66 'title' => 'List menus',
67 'type' => MENU_DEFAULT_LOCAL_TASK,
68 'weight' => -10,
69 );
70 $items['admin/structure/menu/add'] = array(
71 'title' => 'Add menu',
72 'page callback' => 'drupal_get_form',
73 'page arguments' => array('menu_edit_menu', 'add'),
74 'access arguments' => array('administer menu'),
75 'type' => MENU_LOCAL_ACTION,
76 'file' => 'menu.admin.inc',
77 );
78 $items['admin/structure/menu/settings'] = array(
79 'title' => 'Settings',
80 'page callback' => 'drupal_get_form',
81 'page arguments' => array('menu_configure'),
82 'access arguments' => array('administer menu'),
83 'type' => MENU_LOCAL_TASK,
84 'weight' => 5,
85 'file' => 'menu.admin.inc',
86 );
87 $items['admin/structure/menu/manage/%menu'] = array(
88 'title' => 'Customize menu',
89 'page callback' => 'drupal_get_form',
90 'page arguments' => array('menu_overview_form', 4),
91 'title callback' => 'menu_overview_title',
92 'title arguments' => array(4),
93 'access arguments' => array('administer menu'),
94 'type' => MENU_CALLBACK,
95 'file' => 'menu.admin.inc',
96 );
97 $items['admin/structure/menu/manage/%menu/list'] = array(
98 'title' => 'List links',
99 'weight' => -10,
100 'type' => MENU_DEFAULT_LOCAL_TASK,
101 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
102 );
103 $items['admin/structure/menu/manage/%menu/add'] = array(
104 'title' => 'Add link',
105 'page callback' => 'drupal_get_form',
106 'page arguments' => array('menu_edit_item', 'add', NULL, 4),
107 'access arguments' => array('administer menu'),
108 'type' => MENU_LOCAL_ACTION,
109 'file' => 'menu.admin.inc',
110 );
111 $items['admin/structure/menu/manage/%menu/edit'] = array(
112 'title' => 'Edit menu',
113 'page callback' => 'drupal_get_form',
114 'page arguments' => array('menu_edit_menu', 'edit', 4),
115 'access arguments' => array('administer menu'),
116 'type' => MENU_LOCAL_TASK,
117 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
118 'file' => 'menu.admin.inc',
119 );
120 $items['admin/structure/menu/manage/%menu/delete'] = array(
121 'title' => 'Delete menu',
122 'page callback' => 'menu_delete_menu_page',
123 'page arguments' => array(4),
124 'access arguments' => array('administer menu'),
125 'type' => MENU_CALLBACK,
126 'file' => 'menu.admin.inc',
127 );
128 $items['admin/structure/menu/item/%menu_link/edit'] = array(
129 'title' => 'Edit menu link',
130 'page callback' => 'drupal_get_form',
131 'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
132 'access arguments' => array('administer menu'),
133 'type' => MENU_CALLBACK,
134 'file' => 'menu.admin.inc',
135 );
136 $items['admin/structure/menu/item/%menu_link/reset'] = array(
137 'title' => 'Reset menu link',
138 'page callback' => 'drupal_get_form',
139 'page arguments' => array('menu_reset_item_confirm', 4),
140 'access arguments' => array('administer menu'),
141 'type' => MENU_CALLBACK,
142 'file' => 'menu.admin.inc',
143 );
144 $items['admin/structure/menu/item/%menu_link/delete'] = array(
145 'title' => 'Delete menu link',
146 'page callback' => 'menu_item_delete_page',
147 'page arguments' => array(4),
148 'access arguments' => array('administer menu'),
149 'type' => MENU_CALLBACK,
150 'file' => 'menu.admin.inc',
151 );
152 return $items;
153 }
154
155 /**
156 * Implement hook_theme().
157 */
158 function menu_theme() {
159 return array(
160 'menu_overview_form' => array(
161 'file' => 'menu.admin.inc',
162 'render element' => 'form',
163 ),
164 'menu_admin_overview' => array(
165 'file' => 'menu.admin.inc',
166 'variables' => array('title' => NULL, 'name' => NULL, 'description' => NULL),
167 ),
168 );
169 }
170
171 /**
172 * Implement hook_enable().
173 *
174 * Add a link for each custom menu.
175 */
176 function menu_enable() {
177 menu_rebuild();
178 $base_link = db_query("SELECT mlid AS plid, menu_name FROM {menu_links} WHERE link_path = 'admin/structure/menu' AND module = 'system'")->fetchAssoc();
179 $base_link['router_path'] = 'admin/structure/menu/manage/%';
180 $base_link['module'] = 'menu';
181 $result = db_query("SELECT * FROM {menu_custom}", array(), array('fetch' => PDO::FETCH_ASSOC));
182 foreach ($result as $menu) {
183 // $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
184 $link = $base_link;
185 $link['mlid'] = 0;
186 $link['link_title'] = $menu['title'];
187 $link['link_path'] = 'admin/structure/menu/manage/' . $menu['menu_name'];
188 $menu_link = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND plid = :plid", array(
189 ':path' => $link['link_path'],
190 ':plid' => $link['plid']
191 ))
192 ->fetchField();
193 if (!$menu_link) {
194 menu_link_save($link);
195 }
196 }
197 menu_cache_clear_all();
198 }
199
200 /**
201 * Title callback for the menu overview page and links.
202 */
203 function menu_overview_title($menu) {
204 return $menu['title'];
205 }
206
207 /**
208 * Load the data for a single custom menu.
209 *
210 * @param $menu_name
211 * The unique name of a custom menu to load.
212 */
213 function menu_load($menu_name) {
214 return db_query("SELECT * FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu_name))->fetchAssoc();
215 }
216
217 /**
218 * Save a custom menu.
219 *
220 * @param $menu
221 * An array representing a custom menu:
222 * - menu_name: The unique name of the custom menu.
223 * - title: The human readable menu title.
224 * - description: The custom menu description.
225 * - old_name: For existing menus, the current 'menu_name', otherwise empty.
226 * Decides whether hook_menu_insert() or hook_menu_update() will be invoked.
227 *
228 * Modules should always pass a fully populated $menu when saving a custom
229 * menu, so other modules are able to output proper status or watchdog messages.
230 *
231 * @see menu_load()
232 */
233 function menu_save($menu) {
234 db_merge('menu_custom')
235 ->key(array('menu_name' => $menu['menu_name']))
236 ->fields(array(
237 'title' => $menu['title'],
238 'description' => $menu['description'],
239 ))
240 ->execute();
241
242 // Since custom menus are keyed by name and their machine-name cannot be
243 // changed, there is no real differentiation between inserting and updating a
244 // menu. To overcome this, we define the existing menu_name as 'old_name' in
245 // menu_edit_menu().
246 // @todo Replace this condition when db_merge() returns the proper query
247 // result (insert/update).
248 if (!empty($menu['old_name'])) {
249 module_invoke_all('menu_update', $menu);
250 }
251 else {
252 module_invoke_all('menu_insert', $menu);
253 }
254 }
255
256 /**
257 * Delete a custom menu and all contained links.
258 *
259 * Note that this function deletes all menu links in a custom menu. While menu
260 * links derived from router paths may be restored by rebuilding the menu, all
261 * customized and custom links will be irreversibly gone. Therefore, this
262 * function should usually be called from a user interface (form submit) handler
263 * only, which allows the user to confirm the action.
264 *
265 * @param $menu
266 * An array representing a custom menu:
267 * - menu_name: The unique name of the custom menu.
268 * - title: The human readable menu title.
269 * - description: The custom menu description.
270 *
271 * Modules should always pass a fully populated $menu when deleting a custom
272 * menu, so other modules are able to output proper status or watchdog messages.
273 *
274 * @see menu_load()
275 *
276 * menu_delete_links() will take care of clearing the page cache. Other modules
277 * should take care of their menu-related data by implementing
278 * hook_menu_delete().
279 */
280 function menu_delete($menu) {
281 // Delete all links from the menu.
282 menu_delete_links($menu['menu_name']);
283
284 // Delete the custom menu.
285 db_delete('menu_custom')
286 ->condition('menu_name', $menu['menu_name'])
287 ->execute();
288
289 module_invoke_all('menu_delete', $menu);
290 }
291
292 /**
293 * Return a list of menu items that are valid possible parents for the given menu item.
294 *
295 * @param $menus
296 * An array of menu names and titles, such as from menu_get_menus().
297 * @param $item
298 * The menu item or the node type for which to generate a list of parents.
299 * If $item['mlid'] == 0 then the complete tree is returned.
300 * @return
301 * An array of menu link titles keyed on the a string containing the menu name
302 * and mlid. The list excludes the given item and its children.
303 *
304 * @todo This has to be turned into a #process form element callback. The
305 * 'menu_override_parent_selector' variable is entirely superfluous.
306 */
307 function menu_parent_options($menus, $item) {
308 // The menu_links table can be practically any size and we need a way to
309 // allow contrib modules to provide more scalable pattern choosers.
310 // hook_form_alter is too late in itself because all the possible parents are
311 // retrieved here, unless menu_override_parent_selector is set to TRUE.
312 if (variable_get('menu_override_parent_selector', FALSE)) {
313 return array();
314 }
315
316 $available_menus = array();
317 if (is_array($item)) {
318 // If $item is an array fill it with all menus given to this function.
319 $available_menus = $menus;
320 }
321 else {
322 // If $item is a node type, get all available menus for this type and
323 // prepare a dummy menu item for _menu_parent_depth_limit().
324 $type_menus = variable_get('menu_options_' . $item, array('main-menu' => 'main-menu'));
325 foreach ($type_menus as $menu) {
326 $available_menus[$menu] = $menu;
327 }
328 $item = array('mlid' => 0);
329 }
330
331 return _menu_get_options($menus, $available_menus, $item);
332 }
333
334 /**
335 * Page callback.
336 * Get all available menus and menu items as Javascript array.
337 */
338 function menu_parent_options_js() {
339 $available_menus = array();
340 if (isset($_POST['menus']) && count($_POST['menus'])) {
341 foreach ($_POST['menus'] as $menu) {
342 $available_menus[$menu] = $menu;
343 }
344 }
345 $options = _menu_get_options(menu_get_menus(), $available_menus, array('mlid' => 0));
346 error_log( var_export( $options, 1) );
347 print drupal_json_output($options);
348 }
349
350 /**
351 * Helper function to get the items of the given menu.
352 */
353 function _menu_get_options($menus, $available_menus, $item) {
354 // If the item has children, there is an added limit to the depth of valid parents.
355 if (isset($item['parent_depth_limit'])) {
356 $limit = $item['parent_depth_limit'];
357 }
358 else {
359 $limit = _menu_parent_depth_limit($item);
360 }
361
362 $options = array();
363 foreach ($menus as $menu_name => $title) {
364 if (isset($available_menus[$menu_name])) {
365 $tree = menu_tree_all_data($menu_name, NULL);
366 $options[$menu_name . ':0'] = '<' . $title . '>';
367 _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
368 }
369 }
370 return $options;
371 }
372
373 /**
374 * Recursive helper function for menu_parent_options().
375 */
376 function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
377 foreach ($tree as $data) {
378 if ($data['link']['depth'] > $depth_limit) {
379 // Don't iterate through any links on this level.
380 break;
381 }
382 if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
383 $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
384 if ($data['link']['hidden']) {
385 $title .= ' (' . t('disabled') . ')';
386 }
387 $options[$menu_name . ':' . $data['link']['mlid']] = $title;
388 if ($data['below']) {
389 _menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
390 }
391 }
392 }
393 }
394
395 /**
396 * Reset a system-defined menu item.
397 */
398 function menu_reset_item($item) {
399 $new_item = _menu_link_build(menu_get_item($item['router_path']));
400 foreach (array('mlid', 'has_children') as $key) {
401 $new_item[$key] = $item[$key];
402 }
403 menu_link_save($new_item);
404 return $new_item;
405 }
406
407 /**
408 * Implement hook_block_info().
409 */
410 function menu_block_info() {
411 $menus = menu_get_menus(FALSE);
412
413 $blocks = array();
414 foreach ($menus as $name => $title) {
415 // Default "Navigation" block is handled by user.module.
416 $blocks[$name]['info'] = check_plain($title);
417 // Menu blocks can't be cached because each menu item can have
418 // a custom access callback. menu.inc manages its own caching.
419 $blocks[$name]['cache'] = DRUPAL_NO_CACHE;
420 }
421 return $blocks;
422 }
423
424 /**
425 * Implement hook_block_view().
426 */
427 function menu_block_view($delta = '') {
428 $menus = menu_get_menus(FALSE);
429 $data['subject'] = check_plain($menus[$delta]);
430 $data['content'] = menu_tree($delta);
431 // Add contextual links for this block.
432 if (!empty($data['content'])) {
433 $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($delta));
434 }
435 return $data;
436 }
437
438 /**
439 * Implement hook_block_view_alter().
440 */
441 function menu_block_view_alter(&$data, $block) {
442 // Add contextual links for system menu blocks.
443 if ($block->module == 'system' && !empty($data['content'])) {
444 $system_menus = menu_list_system_menus();
445 if (isset($system_menus[$block->delta])) {
446 $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
447 }
448 }
449 }
450
451 /**
452 * Implement hook_node_insert().
453 */
454 function menu_node_insert(stdClass $node) {
455 menu_node_save($node);
456 }
457
458 /**
459 * Implement hook_node_update().
460 */
461 function menu_node_update(stdClass $node) {
462 menu_node_save($node);
463 }
464
465 /**
466 * Helper for hook_node_insert() and hook_node_update().
467 */
468 function menu_node_save(stdClass $node) {
469 if (isset($node->menu)) {
470 $link = &$node->menu;
471 if (empty($link['enabled'])) {
472 if (!empty($link['mlid'])) {
473 menu_link_delete($link['mlid']);
474 }
475 }
476 elseif (trim($link['link_title'])) {
477 $link['link_title'] = trim($link['link_title']);
478 $link['link_path'] = "node/$node->nid";
479 // If not already set, use the node's title as link title attribute.
480 if (empty($link['options']['attributes']['title']) && !$link['customized']) {
481 $link['options']['attributes']['title'] = trim($node->title[FIELD_LANGUAGE_NONE][0]['value']);
482 }
483 if (!menu_link_save($link)) {
484 drupal_set_message(t('There was an error saving the menu link.'), 'error');
485 }
486 }
487 }
488 }
489
490 /**
491 * Implement hook_node_delete().
492 */
493 function menu_node_delete(stdClass $node) {
494 // Delete all menu module links that point to this node.
495 $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu'", array(':path' => 'node/' . $node->nid), array('fetch' => PDO::FETCH_ASSOC));
496 foreach ($result as $m) {
497 menu_link_delete($m['mlid']);
498 }
499 }
500
501 /**
502 * Implement hook_node_prepare().
503 */
504 function menu_node_prepare(stdClass $node) {
505 if (empty($node->menu)) {
506 // Prepare the node for the edit form so that $node->menu always exists.
507 $menu_name = variable_get('menu_default_node_menu', 'navigation');
508 $item = array();
509 if (isset($node->nid)) {
510 // Give priority to the default menu
511 $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
512 ':path' => 'node/' . $node->nid,
513 ':menu_name' => $menu_name,
514 ))->fetchField();
515 // Check all menus if a link does not exist in the default menu.
516 if (!$mlid) {
517 $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
518 ':path' => 'node/' . $node->nid,
519 ))->fetchField();
520 }
521 if ($mlid) {
522 $item = menu_link_load($mlid);
523 }
524 }
525 // Set default values.
526 $node->menu = $item + array(
527 'link_title' => '',
528 'mlid' => 0,
529 'plid' => 0,
530 'menu_name' => $menu_name,
531 'weight' => 0,
532 'options' => array(),
533 'module' => 'menu',
534 'expanded' => 0,
535 'hidden' => 0,
536 'has_children' => 0,
537 'customized' => 0,
538 );
539 }
540 // Find the depth limit for the parent select.
541 if (!isset($node->menu['parent_depth_limit'])) {
542 $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
543 }
544 }
545
546 /**
547 * Find the depth limit for items in the parent select.
548 */
549 function _menu_parent_depth_limit($item) {
550 return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
551 }
552
553 /**
554 * Implement hook_form_alter(). Adds menu item fields to the node form.
555 */
556 function menu_form_alter(&$form, $form_state, $form_id) {
557 if (!empty($form['#node_edit_form'])) {
558 // Generate a list of possible parents.
559 // @todo This must be handled in a #process handler.
560 $type = $form['#node']->type;
561 $options = menu_parent_options(menu_get_menus(), $type);
562 // If no possible parent menu items were found, there is nothing to display.
563 if (empty($options)) {
564 return;
565 }
566 $link = $form['#node']->menu;
567 $form['#submit'][] = 'menu_node_form_submit';
568
569 $form['menu'] = array(
570 '#type' => 'fieldset',
571 '#title' => t('Menu settings'),
572 '#access' => user_access('administer menu'),
573 '#collapsible' => TRUE,
574 '#collapsed' => !$link['link_title'],
575 '#group' => 'additional_settings',
576 '#attached' => array(
577 'js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
578 ),
579 '#tree' => TRUE,
580 '#weight' => -2,
581 '#attributes' => array('class' => array('menu-link-form')),
582 );
583 $form['menu']['enabled'] = array(
584 '#type' => 'checkbox',
585 '#title' => t('Provide a menu link'),
586 '#default_value' => (int) (bool) $link['mlid'],
587 );
588 $form['menu']['link'] = array(
589 '#type' => 'container',
590 '#parents' => array('menu'),
591 '#states' => array(
592 'invisible' => array(
593 'input[name="menu[enabled]"]' => array('checked' => FALSE),
594 ),
595 ),
596 );
597
598 // Populate the element with the link data.
599 foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
600 $form['menu']['link'][$key] = array('#type' => 'value', '#value' => $link[$key]);
601 }
602
603 $form['menu']['link']['link_title'] = array(
604 '#type' => 'textfield',
605 '#title' => t('Menu link title'),
606 '#default_value' => $link['link_title'],
607 );
608
609 $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
610 // @todo This will fail with the new selective menus per content type.
611 if (!isset($options[$default])) {
612 $default = 'navigation:0';
613 }
614 $form['menu']['link']['parent'] = array(
615 '#type' => 'select',
616 '#title' => t('Parent item'),
617 '#default_value' => $default,
618 '#options' => $options,
619 '#attributes' => array('class' => array('menu-parent-select')),
620 );
621 $form['menu']['link']['weight'] = array(
622 '#type' => 'weight',
623 '#title' => t('Weight'),
624 '#delta' => 50,
625 '#default_value' => $link['weight'],
626 '#description' => t('Menu links with smaller weights are displayed before links with larger weights.'),
627 );
628 }
629 }
630
631 /**
632 * Submit handler for node form.
633 *
634 * @see menu_form_alter()
635 */
636 function menu_node_form_submit($form, &$form_state) {
637 // Decompose the selected menu parent option into 'menu_name' and 'plid', if
638 // the form used the default parent selection widget.
639 if (!empty($form_state['values']['menu']['parent'])) {
640 list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']);
641 }
642 }
643
644 /**
645 * Implement hook_form_FORM_ID_alter() for the node type form.
646 * Adds menu options to the node type form.
647 */
648 function menu_form_node_type_form_alter(&$form, $form_state) {
649 $menu_options = menu_get_menus();
650 $type = $form['#node_type'];
651 $form['menu'] = array(
652 '#type' => 'fieldset',
653 '#title' => t('Menu settings'),
654 '#collapsible' => TRUE,
655 '#collapsed' => TRUE,
656 '#attached' => array(
657 'js' => array(drupal_get_path('module', 'menu') . '/menu.admin.js'),
658 ),
659 '#group' => 'additional_settings',
660 );
661 $form['menu']['menu_options'] = array(
662 '#type' => 'checkboxes',
663 '#title' => t('Available menus'),
664 '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu' => 'main-menu')),
665 '#options' => $menu_options,
666 '#description' => t('The menus available to place links in for this content type.'),
667 );
668 // To avoid an 'illegal option' error after saving the form we have to load
669 // all available menu items.
670 // Otherwise it is not possible to dynamically add options to the list.
671 // @todo Convert menu_parent_options() into a #process callback.
672 $options = menu_parent_options(menu_get_menus(), array('mlid' => 0));
673 $form['menu']['menu_parent'] = array(
674 '#type' => 'select',
675 '#title' => t('Default parent item'),
676 '#default_value' => variable_get('menu_parent_' . $type->type, 'main-menu:0'),
677 '#options' => $options,
678 '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
679 '#attributes' => array('class' => array('menu-title-select')),
680 );
681
682 // Call Drupal.menu_update_parent_list() to filter the list of
683 // available default parent menu items based on the selected menus.
684 drupal_add_js(
685 '(function ($) { Drupal.menu_update_parent_list(); })(jQuery);',
686 array('scope' => 'footer', 'type' => 'inline')
687 );
688 }
689
690 /**
691 * Return an associative array of the custom menus names.
692 *
693 * @param $all
694 * If FALSE return only user-added menus, or if TRUE also include
695 * the menus defined by the system.
696 * @return
697 * An array with the machine-readable names as the keys, and human-readable
698 * titles as the values.
699 */
700 function menu_get_menus($all = TRUE) {
701 $system_menus = array_keys(menu_list_system_menus());
702 $query = db_select('menu_custom');
703 $query->addTag('translatable');
704 $query->addField('menu_custom', 'menu_name', 'menu_name');
705 $query->addField('menu_custom', 'title', 'title');
706 if (!$all) {
707 $query->condition('menu_name', $system_menus, 'NOT IN');
708 }
709 $query->orderBy('title');
710
711 return $query->execute()->fetchAllKeyed();
712 }
713

  ViewVC Help
Powered by ViewVC 1.1.2