/[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.214 - (show annotations) (download) (as text)
Sun Nov 1 12:11:10 2009 UTC (4 weeks ago) by dries
Branch: MAIN
Changes since 1.213: +5 -5 lines
File MIME type: text/x-php
- Patch #595084 by c960657: use type hinting for .
1 <?php
2 // $Id: menu.module,v 1.213 2009/10/23 22:24:16 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 function menu_parent_options($menus, $item) {
305 // The menu_links table can be practically any size and we need a way to
306 // allow contrib modules to provide more scalable pattern choosers.
307 // hook_form_alter is too late in itself because all the possible parents are
308 // retrieved here, unless menu_override_parent_selector is set to TRUE.
309 if (variable_get('menu_override_parent_selector', FALSE)) {
310 return array();
311 }
312
313 $available_menus = array();
314 if (is_array($item)) {
315 // If $item is an array fill it with all menus given to this function.
316 $available_menus = $menus;
317 }
318 else {
319 // If $item is a node type, get all available menus for this type and
320 // prepare a dummy menu item for _menu_parent_depth_limit().
321 $type_menus = variable_get('menu_options_' . $item, array('main-menu' => 'main-menu'));
322 foreach ($type_menus as $menu) {
323 $available_menus[$menu] = $menu;
324 }
325 $item = array('mlid' => 0);
326 }
327
328 return _menu_get_options($menus, $available_menus, $item);
329 }
330
331 /**
332 * Page callback.
333 * Get all available menus and menu items as Javascript array.
334 */
335 function menu_parent_options_js() {
336 $available_menus = array();
337 if (isset($_POST['menus']) && count($_POST['menus'])) {
338 foreach ($_POST['menus'] as $menu) {
339 $available_menus[$menu] = $menu;
340 }
341 }
342 $options = _menu_get_options(menu_get_menus(), $available_menus, array('mlid' => 0));
343 error_log( var_export( $options, 1) );
344 print drupal_json_output($options);
345 }
346
347 /**
348 * Helper function to get the items of the given menu.
349 */
350 function _menu_get_options($menus, $available_menus, $item) {
351 // If the item has children, there is an added limit to the depth of valid parents.
352 if (isset($item['parent_depth_limit'])) {
353 $limit = $item['parent_depth_limit'];
354 }
355 else {
356 $limit = _menu_parent_depth_limit($item);
357 }
358
359 $options = array();
360 foreach ($menus as $menu_name => $title) {
361 if (isset($available_menus[$menu_name])) {
362 $tree = menu_tree_all_data($menu_name, NULL);
363 $options[$menu_name . ':0'] = '<' . $title . '>';
364 _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
365 }
366 }
367 return $options;
368 }
369
370 /**
371 * Recursive helper function for menu_parent_options().
372 */
373 function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
374 foreach ($tree as $data) {
375 if ($data['link']['depth'] > $depth_limit) {
376 // Don't iterate through any links on this level.
377 break;
378 }
379 if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
380 $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
381 if ($data['link']['hidden']) {
382 $title .= ' (' . t('disabled') . ')';
383 }
384 $options[$menu_name . ':' . $data['link']['mlid']] = $title;
385 if ($data['below']) {
386 _menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
387 }
388 }
389 }
390 }
391
392 /**
393 * Reset a system-defined menu item.
394 */
395 function menu_reset_item($item) {
396 $new_item = _menu_link_build(menu_get_item($item['router_path']));
397 foreach (array('mlid', 'has_children') as $key) {
398 $new_item[$key] = $item[$key];
399 }
400 menu_link_save($new_item);
401 return $new_item;
402 }
403
404 /**
405 * Implement hook_block_info().
406 */
407 function menu_block_info() {
408 $menus = menu_get_menus(FALSE);
409
410 $blocks = array();
411 foreach ($menus as $name => $title) {
412 // Default "Navigation" block is handled by user.module.
413 $blocks[$name]['info'] = check_plain($title);
414 // Menu blocks can't be cached because each menu item can have
415 // a custom access callback. menu.inc manages its own caching.
416 $blocks[$name]['cache'] = DRUPAL_NO_CACHE;
417 }
418 return $blocks;
419 }
420
421 /**
422 * Implement hook_block_view().
423 */
424 function menu_block_view($delta = '') {
425 $menus = menu_get_menus(FALSE);
426 $data['subject'] = check_plain($menus[$delta]);
427 $data['content'] = menu_tree($delta);
428 // Add contextual links for this block.
429 if (!empty($data['content'])) {
430 $data['content']['#contextual_links']['menu'] = menu_contextual_links('admin/structure/menu/manage', array($delta));
431 }
432 return $data;
433 }
434
435 /**
436 * Implement hook_block_view_alter().
437 */
438 function menu_block_view_alter(&$data, $block) {
439 // Add contextual links for system menu blocks.
440 if ($block->module == 'system' && !empty($data['content'])) {
441 $system_menus = menu_list_system_menus();
442 if (isset($system_menus[$block->delta])) {
443 $data['content']['#contextual_links']['menu'] = menu_contextual_links('admin/structure/menu/manage', array($block->delta));
444 }
445 }
446 }
447
448 /**
449 * Implement hook_node_insert().
450 */
451 function menu_node_insert(stdClass $node) {
452 if (isset($node->menu)) {
453 $item = &$node->menu;
454 if (!empty($item['delete'])) {
455 menu_link_delete($item['mlid']);
456 }
457 elseif (trim($item['link_title'])) {
458 $item['link_title'] = trim($item['link_title']);
459 $item['link_path'] = "node/$node->nid";
460 if (!$item['customized']) {
461 $item['options']['attributes']['title'] = trim($node->title[FIELD_LANGUAGE_NONE][0]['value']);
462 }
463 if (!menu_link_save($item)) {
464 drupal_set_message(t('There was an error saving the menu link.'), 'error');
465 }
466 }
467 }
468 }
469
470 /**
471 * Implement hook_node_update().
472 */
473 function menu_node_update(stdClass $node) {
474 if (isset($node->menu)) {
475 $item = &$node->menu;
476 if (!empty($item['delete'])) {
477 menu_link_delete($item['mlid']);
478 }
479 elseif (trim($item['link_title'])) {
480 $item['link_title'] = trim($item['link_title']);
481 $item['link_path'] = "node/$node->nid";
482 if (!$item['customized']) {
483 $item['options']['attributes']['title'] = trim($node->title[FIELD_LANGUAGE_NONE][0]['value']);
484 }
485 if (!menu_link_save($item)) {
486 drupal_set_message(t('There was an error saving the menu link.'), 'error');
487 }
488 }
489 }
490 }
491
492 /**
493 * Implement hook_node_delete().
494 */
495 function menu_node_delete(stdClass $node) {
496 // Delete all menu module links that point to this node.
497 $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));
498 foreach ($result as $m) {
499 menu_link_delete($m['mlid']);
500 }
501 }
502
503 /**
504 * Implement hook_node_prepare().
505 */
506 function menu_node_prepare(stdClass $node) {
507 if (empty($node->menu)) {
508 // Prepare the node for the edit form so that $node->menu always exists.
509 $menu_name = variable_get('menu_default_node_menu', 'navigation');
510 $item = array();
511 if (isset($node->nid)) {
512 // Give priority to the default menu
513 $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(
514 ':path' => 'node/' . $node->nid,
515 ':menu_name' => $menu_name,
516 ))->fetchField();
517 // Check all menus if a link does not exist in the default menu.
518 if (!$mlid) {
519 $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
520 ':path' => 'node/' . $node->nid,
521 ))->fetchField();
522 }
523 if ($mlid) {
524 $item = menu_link_load($mlid);
525 }
526 }
527 // Set default values.
528 $node->menu = $item + array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu_name, 'weight' => 0, 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0, 'customized' => 0);
529 }
530 // Find the depth limit for the parent select.
531 if (!isset($node->menu['parent_depth_limit'])) {
532 $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
533 }
534 }
535
536 /**
537 * Find the depth limit for items in the parent select.
538 */
539 function _menu_parent_depth_limit($item) {
540 return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
541 }
542
543 /**
544 * Implement hook_form_alter(). Adds menu item fields to the node form.
545 */
546 function menu_form_alter(&$form, $form_state, $form_id) {
547 if (!empty($form['#node_edit_form'])) {
548 // Generate a list of possible parents.
549 $type = $form['#node']->type;
550 $options = menu_parent_options(menu_get_menus(), $type);
551 if (count($options) == 0) {
552 // No possible parent menu items found so there is no need to display the
553 // menu options.
554 return;
555 }
556
557 // Note - doing this to make sure the delete checkbox stays in the form.
558 $form['#cache'] = TRUE;
559
560 $form['menu'] = array(
561 '#type' => 'fieldset',
562 '#title' => t('Menu settings'),
563 '#access' => user_access('administer menu'),
564 '#collapsible' => TRUE,
565 '#collapsed' => FALSE,
566 '#group' => 'additional_settings',
567 '#attached' => array(
568 'js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
569 ),
570 '#tree' => TRUE,
571 '#weight' => -2,
572 '#attributes' => array('class' => array('menu-item-form')),
573 );
574 $item = $form['#node']->menu;
575
576 if ($item['mlid']) {
577 // There is an existing link.
578 $form['menu']['delete'] = array(
579 '#type' => 'checkbox',
580 '#title' => t('Delete this menu item.'),
581 );
582 }
583 if (!$item['link_title']) {
584 $form['menu']['#collapsed'] = TRUE;
585 }
586
587 foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
588 $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
589 }
590 $form['menu']['#item'] = $item;
591
592 $form['menu']['link_title'] = array('#type' => 'textfield',
593 '#title' => t('Menu link title'),
594 '#default_value' => $item['link_title'],
595 '#description' => t('The link text corresponding to this item that should appear in the menu. Leave blank if you do not wish to add this post to the menu.'),
596 '#required' => FALSE,
597 );
598
599 $default = ($item['mlid'] ? $item['menu_name'] . ':' . $item['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
600 if (!isset($options[$default])) {
601 $default = 'navigation:0';
602 }
603 $form['menu']['parent'] = array(
604 '#type' => 'select',
605 '#title' => t('Parent item'),
606 '#default_value' => $default,
607 '#options' => $options,
608 '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
609 '#attributes' => array('class' => array('menu-title-select')),
610 );
611 $form['#submit'][] = 'menu_node_form_submit';
612
613 $form['menu']['weight'] = array(
614 '#type' => 'weight',
615 '#title' => t('Weight'),
616 '#delta' => 50,
617 '#default_value' => $item['weight'],
618 '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
619 );
620 }
621 }
622
623 /**
624 * Implement hook_form_FORM_ID_alter() for the node type form.
625 * Adds menu options to the node type form.
626 */
627 function menu_form_node_type_form_alter(&$form, $form_state) {
628 $menu_options = menu_get_menus();
629 $type = $form['#node_type'];
630 $form['menu'] = array(
631 '#type' => 'fieldset',
632 '#title' => t('Menu settings'),
633 '#collapsible' => TRUE,
634 '#collapsed' => TRUE,
635 '#attached' => array(
636 'js' => array(drupal_get_path('module', 'menu') . '/menu.admin.js'),
637 ),
638 '#group' => 'additional_settings',
639 );
640 $form['menu']['menu_options'] = array(
641 '#type' => 'checkboxes',
642 '#title' => t('Available menus'),
643 '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu' => 'main-menu')),
644 '#options' => $menu_options,
645 '#description' => t('The menus available to place links in for this content type.'),
646 );
647 // To avoid an 'illegal option' error after saving the form we have to load
648 // all available menu items.
649 // Otherwise it is not possible to dynamically add options to the list.
650 $options = menu_parent_options(menu_get_menus(), array('mlid' => 0));
651 $form['menu']['menu_parent'] = array(
652 '#type' => 'select',
653 '#title' => t('Default parent item'),
654 '#default_value' => variable_get('menu_parent_' . $type->type, 'main-menu:0'),
655 '#options' => $options,
656 '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
657 '#attributes' => array('class' => array('menu-title-select')),
658 );
659
660 // Call Drupal.menu_update_parent_list() to filter the list of
661 // available default parent menu items based on the selected menus.
662 drupal_add_js(
663 '(function ($) { Drupal.menu_update_parent_list(); })(jQuery);',
664 array('scope' => 'footer', 'type' => 'inline')
665 );
666 }
667
668 /**
669 * Decompose the selected menu parent option into the menu_name and plid.
670 */
671 function menu_node_form_submit($form, &$form_state) {
672 list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']);
673 }
674
675 /**
676 * Return an associative array of the custom menus names.
677 *
678 * @param $all
679 * If FALSE return only user-added menus, or if TRUE also include
680 * the menus defined by the system.
681 * @return
682 * An array with the machine-readable names as the keys, and human-readable
683 * titles as the values.
684 */
685 function menu_get_menus($all = TRUE) {
686 $system_menus = array_keys(menu_list_system_menus());
687 $query = db_select('menu_custom');
688 $query->addTag('translatable');
689 $query->addField('menu_custom', 'menu_name', 'menu_name');
690 $query->addField('menu_custom', 'title', 'title');
691 if (!$all) {
692 $query->condition('menu_name', $system_menus, 'NOT IN');
693 }
694 $query->orderBy('title');
695
696 return $query->execute()->fetchAllKeyed();
697 }
698

  ViewVC Help
Powered by ViewVC 1.1.2