Projet

Général

Profil

Paste
Télécharger (27,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / menu / menu.module @ 76597ebf

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Allows administrators to customize the site's navigation menus.
6
 *
7
 * A menu (in this context) is a hierarchical collection of links, generally
8
 * used for navigation. This is not to be confused with the
9
 * @link menu Menu system @endlink of menu.inc and hook_menu(), which defines
10
 * page routing requests for Drupal, and also allows the defined page routing
11
 * URLs to be added to the main site navigation menu.
12
 */
13
14
/**
15
 * Maximum length of menu name as entered by the user. Database length is 32
16
 * and we add a menu- prefix.
17
 */
18
define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
19
20
/**
21
 * Implements hook_help().
22
 */
23
function menu_help($path, $arg) {
24
  switch ($path) {
25
    case 'admin/help#menu':
26
      $output = '';
27
      $output .= '<h3>' . t('About') . '</h3>';
28
      $output .= '<p>' . t('The Menu module provides an interface for managing menus. A menu is a hierarchical collection of links, which can be within or external to the site, generally used for navigation. Each menu is rendered in a block that can be enabled and positioned through the <a href="@blocks">Blocks administration page</a>. You can view and manage menus on the <a href="@menus">Menus administration page</a>. For more information, see the online handbook entry for the <a href="@menu">Menu module</a>.', array('@blocks' => url('admin/structure/block'), '@menus' => url('admin/structure/menu'), '@menu' => 'http://drupal.org/documentation/modules/menu/')) . '</p>';
29
      $output .= '<h3>' . t('Uses') . '</h3>';
30
      $output .= '<dl>';
31
      $output .= '<dt>' . t('Managing menus') . '</dt>';
32
      $output .= '<dd>' . t('Users with the <em>Administer menus and menu items</em> permission can add, edit and delete custom menus on the <a href="@menu">Menus administration page</a>. Custom menus can be special site menus, menus of external links, or any combination of internal and external links. You may create an unlimited number of additional menus, each of which will automatically have an associated block. By selecting <em>list links</em>, you can add, edit, or delete links for a given menu. The links listing page provides a drag-and-drop interface for controlling the order of links, and creating a hierarchy within the menu.', array('@menu' => url('admin/structure/menu'), '@add-menu' => url('admin/structure/menu/add'))) . '</dd>';
33
      $output .= '<dt>' . t('Displaying menus') . '</dt>';
34
      $output .= '<dd>' . t('After you have created a menu, you must enable and position the associated block on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
35
      $output .= '</dl>';
36
      return $output;
37
    case 'admin/structure/menu/add':
38
      return '<p>' . t('You can enable the newly-created block for this menu on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
39
  }
40
  if ($path == 'admin/structure/menu' && module_exists('block')) {
41
    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>';
42
  }
43
}
44
45
/**
46
 * Implements hook_permission().
47
 */
48
function menu_permission() {
49
  return array(
50
    'administer menu' => array(
51
      'title' => t('Administer menus and menu items'),
52
    ),
53
  );
54
}
55
56
/**
57
 * Implements hook_menu().
58
 */
59
function menu_menu() {
60
  $items['admin/structure/menu'] = array(
61
    'title' => 'Menus',
62
    'description' => 'Add new menus to your site, edit existing menus, and rename and reorganize menu links.',
63
    'page callback' => 'menu_overview_page',
64
    'access callback' => 'user_access',
65
    'access arguments' => array('administer menu'),
66
    'file' => 'menu.admin.inc',
67
  );
68
  $items['admin/structure/menu/parents'] = array(
69
    'title' => 'Parent menu items',
70
    'page callback' => 'menu_parent_options_js',
71
    'type' => MENU_CALLBACK,
72 b4adf10d Assos Assos
    'access arguments' => array('administer menu'),
73 85ad3d82 Assos Assos
  );
74
  $items['admin/structure/menu/list'] = array(
75
    'title' => 'List menus',
76
    'type' => MENU_DEFAULT_LOCAL_TASK,
77
    'weight' => -10,
78
  );
79
  $items['admin/structure/menu/add'] = array(
80
    'title' => 'Add menu',
81
    'page callback' => 'drupal_get_form',
82
    'page arguments' => array('menu_edit_menu', 'add'),
83
    'access arguments' => array('administer menu'),
84
    'type' => MENU_LOCAL_ACTION,
85
    'file' => 'menu.admin.inc',
86
  );
87
  $items['admin/structure/menu/settings'] = array(
88
    'title' => 'Settings',
89
    'page callback' => 'drupal_get_form',
90
    'page arguments' => array('menu_configure'),
91
    'access arguments' => array('administer menu'),
92
    'type' => MENU_LOCAL_TASK,
93
    'weight' => 5,
94
    'file' => 'menu.admin.inc',
95
  );
96
  $items['admin/structure/menu/manage/%menu'] = array(
97
    'title' => 'Customize menu',
98
    'page callback' => 'drupal_get_form',
99
    'page arguments' => array('menu_overview_form', 4),
100
    'title callback' => 'menu_overview_title',
101
    'title arguments' => array(4),
102
    'access arguments' => array('administer menu'),
103
    'file' => 'menu.admin.inc',
104
  );
105
  $items['admin/structure/menu/manage/%menu/list'] = array(
106
    'title' => 'List links',
107
    'weight' => -10,
108
    'type' => MENU_DEFAULT_LOCAL_TASK,
109
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
110
  );
111
  $items['admin/structure/menu/manage/%menu/add'] = array(
112
    'title' => 'Add link',
113
    'page callback' => 'drupal_get_form',
114
    'page arguments' => array('menu_edit_item', 'add', NULL, 4),
115
    'access arguments' => array('administer menu'),
116
    'type' => MENU_LOCAL_ACTION,
117
    'file' => 'menu.admin.inc',
118
  );
119
  $items['admin/structure/menu/manage/%menu/edit'] = array(
120
    'title' => 'Edit menu',
121
    'page callback' => 'drupal_get_form',
122
    'page arguments' => array('menu_edit_menu', 'edit', 4),
123
    'access arguments' => array('administer menu'),
124
    'type' => MENU_LOCAL_TASK,
125
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
126
    'file' => 'menu.admin.inc',
127
  );
128
  $items['admin/structure/menu/manage/%menu/delete'] = array(
129
    'title' => 'Delete menu',
130
    'page callback' => 'menu_delete_menu_page',
131
    'page arguments' => array(4),
132
    'access arguments' => array('administer menu'),
133
    'file' => 'menu.admin.inc',
134
  );
135
  $items['admin/structure/menu/item/%menu_link/edit'] = array(
136
    'title' => 'Edit menu link',
137
    'page callback' => 'drupal_get_form',
138
    'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
139
    'access arguments' => array('administer menu'),
140
    'file' => 'menu.admin.inc',
141
  );
142
  $items['admin/structure/menu/item/%menu_link/reset'] = array(
143
    'title' => 'Reset menu link',
144
    'page callback' => 'drupal_get_form',
145
    'page arguments' => array('menu_reset_item_confirm', 4),
146
    'access arguments' => array('administer menu'),
147
    'file' => 'menu.admin.inc',
148
  );
149
  $items['admin/structure/menu/item/%menu_link/delete'] = array(
150
    'title' => 'Delete menu link',
151
    'page callback' => 'menu_item_delete_page',
152
    'page arguments' => array(4),
153
    'access arguments' => array('administer menu'),
154
    'file' => 'menu.admin.inc',
155
  );
156
  return $items;
157
}
158
159
/**
160
 * Implements hook_theme().
161
 */
162
function menu_theme() {
163
  return array(
164
    'menu_overview_form' => array(
165
      'file' => 'menu.admin.inc',
166
      'render element' => 'form',
167
    ),
168
    'menu_admin_overview' => array(
169
      'file' => 'menu.admin.inc',
170
      'variables' => array('title' => NULL, 'name' => NULL, 'description' => NULL),
171
    ),
172
  );
173
}
174
175
/**
176
 * Implements hook_enable().
177
 *
178
 * Add a link for each custom menu.
179
 */
180
function menu_enable() {
181
  menu_rebuild();
182
  $base_link = db_query("SELECT mlid AS plid, menu_name FROM {menu_links} WHERE link_path = 'admin/structure/menu' AND module = 'system'")->fetchAssoc();
183
  $base_link['router_path'] = 'admin/structure/menu/manage/%';
184
  $base_link['module'] = 'menu';
185
  $result = db_query("SELECT * FROM {menu_custom}", array(), array('fetch' => PDO::FETCH_ASSOC));
186
  foreach ($result as $menu) {
187
    // $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
188
    $link = $base_link;
189
    $link['mlid'] = 0;
190
    $link['link_title'] = $menu['title'];
191
    $link['link_path'] = 'admin/structure/menu/manage/' . $menu['menu_name'];
192
    $menu_link = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND plid = :plid", array(
193
      ':path' => $link['link_path'],
194
      ':plid' => $link['plid']
195
    ))
196
    ->fetchField();
197
    if (!$menu_link) {
198
      menu_link_save($link);
199
    }
200
  }
201
  menu_cache_clear_all();
202
}
203
204
/**
205
 * Title callback for the menu overview page and links.
206
 */
207
function menu_overview_title($menu) {
208
  return $menu['title'];
209
}
210
211
/**
212
 * Load the data for a single custom menu.
213
 *
214
 * @param $menu_name
215
 *   The unique name of a custom menu to load.
216
 * @return
217
 *   Array defining the custom menu, or FALSE if the menu doesn't exist.
218
 */
219
function menu_load($menu_name) {
220
  $all_menus = menu_load_all();
221
  return isset($all_menus[$menu_name]) ? $all_menus[$menu_name] : FALSE;
222
}
223
224
/**
225
 * Load all custom menu data.
226
 *
227
 * @return
228
 *   Array of custom menu data.
229
 */
230
function menu_load_all() {
231
  $custom_menus = &drupal_static(__FUNCTION__);
232
  if (!isset($custom_menus)) {
233
    if ($cached = cache_get('menu_custom', 'cache_menu')) {
234
      $custom_menus = $cached->data;
235
    }
236
    else {
237
      $custom_menus = db_query('SELECT * FROM {menu_custom}')->fetchAllAssoc('menu_name', PDO::FETCH_ASSOC);
238
      cache_set('menu_custom', $custom_menus, 'cache_menu');
239
    }
240
  }
241
  return $custom_menus;
242
}
243
244
/**
245
 * Save a custom menu.
246
 *
247
 * @param $menu
248
 *   An array representing a custom menu:
249
 *   - menu_name: The unique name of the custom menu (composed of lowercase
250
 *     letters, numbers, and hyphens).
251
 *   - title: The human readable menu title.
252
 *   - description: The custom menu description.
253
 *
254
 * Modules should always pass a fully populated $menu when saving a custom
255
 * menu, so other modules are able to output proper status or watchdog messages.
256
 *
257
 * @see menu_load()
258
 */
259
function menu_save($menu) {
260
  $status = db_merge('menu_custom')
261
    ->key(array('menu_name' => $menu['menu_name']))
262
    ->fields(array(
263
      'title' => $menu['title'],
264
      'description' => $menu['description'],
265
    ))
266
    ->execute();
267
  menu_cache_clear_all();
268
269
  switch ($status) {
270
    case SAVED_NEW:
271
      // Make sure the menu is present in the active menus variable so that its
272
      // items may appear in the menu active trail.
273
      // @see menu_set_active_menu_names()
274
      $active_menus = variable_get('menu_default_active_menus', array_keys(menu_get_menus()));
275
      if (!in_array($menu['menu_name'], $active_menus)) {
276
        $active_menus[] = $menu['menu_name'];
277
        variable_set('menu_default_active_menus', $active_menus);
278
      }
279
280
      module_invoke_all('menu_insert', $menu);
281
      break;
282
283
    case SAVED_UPDATED:
284
      module_invoke_all('menu_update', $menu);
285
      break;
286
  }
287
}
288
289
/**
290
 * Delete a custom menu and all contained links.
291
 *
292
 * Note that this function deletes all menu links in a custom menu. While menu
293
 * links derived from router paths may be restored by rebuilding the menu, all
294
 * customized and custom links will be irreversibly gone. Therefore, this
295
 * function should usually be called from a user interface (form submit) handler
296
 * only, which allows the user to confirm the action.
297
 *
298
 * @param $menu
299
 *   An array representing a custom menu:
300
 *   - menu_name: The unique name of the custom menu.
301
 *   - title: The human readable menu title.
302
 *   - description: The custom menu description.
303
 *
304
 * Modules should always pass a fully populated $menu when deleting a custom
305
 * menu, so other modules are able to output proper status or watchdog messages.
306
 *
307
 * @see menu_load()
308
 *
309
 * menu_delete_links() will take care of clearing the page cache. Other modules
310
 * should take care of their menu-related data by implementing
311
 * hook_menu_delete().
312
 */
313
function menu_delete($menu) {
314
  // Delete all links from the menu.
315
  menu_delete_links($menu['menu_name']);
316
317
  // Remove menu from active menus variable.
318
  $active_menus = variable_get('menu_default_active_menus', array_keys(menu_get_menus()));
319
  foreach ($active_menus as $i => $menu_name) {
320
    if ($menu['menu_name'] == $menu_name) {
321
      unset($active_menus[$i]);
322
      variable_set('menu_default_active_menus', $active_menus);
323
    }
324
  }
325
326
  // Delete the custom menu.
327
  db_delete('menu_custom')
328
    ->condition('menu_name', $menu['menu_name'])
329
    ->execute();
330
331
  menu_cache_clear_all();
332
  module_invoke_all('menu_delete', $menu);
333
}
334
335
/**
336
 * Return a list of menu items that are valid possible parents for the given menu item.
337
 *
338
 * @param $menus
339
 *   An array of menu names and titles, such as from menu_get_menus().
340
 * @param $item
341
 *   The menu item or the node type for which to generate a list of parents.
342
 *   If $item['mlid'] == 0 then the complete tree is returned.
343
 * @param $type
344
 *   The node type for which to generate a list of parents.
345
 *   If $item itself is a node type then $type is ignored.
346
 * @return
347
 *   An array of menu link titles keyed on the a string containing the menu name
348
 *   and mlid. The list excludes the given item and its children.
349
 *
350
 * @todo This has to be turned into a #process form element callback. The
351
 *   'menu_override_parent_selector' variable is entirely superfluous.
352
 */
353
function menu_parent_options($menus, $item, $type = '') {
354
  // The menu_links table can be practically any size and we need a way to
355
  // allow contrib modules to provide more scalable pattern choosers.
356
  // hook_form_alter is too late in itself because all the possible parents are
357
  // retrieved here, unless menu_override_parent_selector is set to TRUE.
358
  if (variable_get('menu_override_parent_selector', FALSE)) {
359
    return array();
360
  }
361
362
  $available_menus = array();
363
  if (!is_array($item)) {
364
    // If $item is not an array then it is a node type.
365
    // Use it as $type and prepare a dummy menu item for _menu_get_options().
366
    $type = $item;
367
    $item = array('mlid' => 0);
368
  }
369
  if (empty($type)) {
370
    // If no node type is set, use all menus given to this function.
371
    $available_menus = $menus;
372
  }
373
  else {
374
    // If a node type is set, use all available menus for this type.
375
    $type_menus = variable_get('menu_options_' . $type, array('main-menu' => 'main-menu'));
376
    foreach ($type_menus as $menu) {
377
      $available_menus[$menu] = $menu;
378
    }
379
  }
380
381
  return _menu_get_options($menus, $available_menus, $item);
382
}
383
384
/**
385
 * Page callback.
386
 * Get all the available menus and menu items as a JavaScript array.
387
 */
388
function menu_parent_options_js() {
389
  $available_menus = array();
390
  if (isset($_POST['menus']) && count($_POST['menus'])) {
391
    foreach ($_POST['menus'] as $menu) {
392
      $available_menus[$menu] = $menu;
393
    }
394
  }
395
  $options = _menu_get_options(menu_get_menus(), $available_menus, array('mlid' => 0));
396
397
  drupal_json_output($options);
398
}
399
400
/**
401
 * Helper function to get the items of the given menu.
402
 */
403
function _menu_get_options($menus, $available_menus, $item) {
404
  // If the item has children, there is an added limit to the depth of valid parents.
405
  if (isset($item['parent_depth_limit'])) {
406
    $limit = $item['parent_depth_limit'];
407
  }
408
  else {
409
    $limit = _menu_parent_depth_limit($item);
410
  }
411
412
  $options = array();
413
  foreach ($menus as $menu_name => $title) {
414
    if (isset($available_menus[$menu_name])) {
415
      $tree = menu_tree_all_data($menu_name, NULL);
416
      $options[$menu_name . ':0'] = '<' . $title . '>';
417
      _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
418
    }
419
  }
420
  return $options;
421
}
422
423
/**
424
 * Recursive helper function for menu_parent_options().
425
 */
426
function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
427
  foreach ($tree as $data) {
428
    if ($data['link']['depth'] > $depth_limit) {
429
      // Don't iterate through any links on this level.
430
      break;
431
    }
432
    if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
433
      $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
434
      if ($data['link']['hidden']) {
435
        $title .= ' (' . t('disabled') . ')';
436
      }
437
      $options[$menu_name . ':' . $data['link']['mlid']] = $title;
438
      if ($data['below']) {
439
        _menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
440
      }
441
    }
442
  }
443
}
444
445
/**
446
 * Reset a system-defined menu link.
447
 */
448
function menu_reset_item($link) {
449
  // To reset the link to its original values, we need to retrieve its
450
  // definition from hook_menu(). Otherwise, for example, the link's menu would
451
  // not be reset, because properties like the original 'menu_name' are not
452
  // stored anywhere else. Since resetting a link happens rarely and this is a
453
  // one-time operation, retrieving the full menu router does no harm.
454
  $menu = menu_get_router();
455
  $router_item = $menu[$link['router_path']];
456
  $new_link = _menu_link_build($router_item);
457
  // Merge existing menu link's ID and 'has_children' property.
458
  foreach (array('mlid', 'has_children') as $key) {
459
    $new_link[$key] = $link[$key];
460
  }
461
  menu_link_save($new_link);
462
  return $new_link;
463
}
464
465
/**
466
 * Implements hook_block_info().
467
 */
468
function menu_block_info() {
469
  $menus = menu_get_menus(FALSE);
470
471
  $blocks = array();
472
  foreach ($menus as $name => $title) {
473
    $blocks[$name]['info'] = check_plain($title);
474
    // Menu blocks can't be cached because each menu item can have
475
    // a custom access callback. menu.inc manages its own caching.
476
    $blocks[$name]['cache'] = DRUPAL_NO_CACHE;
477
  }
478
  return $blocks;
479
}
480
481
/**
482
 * Implements hook_block_view().
483
 */
484
function menu_block_view($delta = '') {
485
  $menus = menu_get_menus(FALSE);
486
  $data['subject'] = check_plain($menus[$delta]);
487
  $data['content'] = menu_tree($delta);
488
  // Add contextual links for this block.
489
  if (!empty($data['content'])) {
490
    $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($delta));
491
  }
492
  return $data;
493
}
494
495
/**
496
 * Implements hook_block_view_alter().
497
 */
498
function menu_block_view_alter(&$data, $block) {
499
  // Add contextual links for system menu blocks.
500
  if ($block->module == 'system' && !empty($data['content'])) {
501
    $system_menus = menu_list_system_menus();
502
    if (isset($system_menus[$block->delta])) {
503
      $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
504
    }
505
  }
506
}
507
508
/**
509
 * Implements hook_node_insert().
510
 */
511
function menu_node_insert($node) {
512
  menu_node_save($node);
513
}
514
515
/**
516
 * Implements hook_node_update().
517
 */
518
function menu_node_update($node) {
519
  menu_node_save($node);
520
}
521
522
/**
523
 * Helper for hook_node_insert() and hook_node_update().
524
 */
525
function menu_node_save($node) {
526
  if (isset($node->menu)) {
527
    $link = &$node->menu;
528
    if (empty($link['enabled'])) {
529
      if (!empty($link['mlid'])) {
530
        menu_link_delete($link['mlid']);
531
      }
532
    }
533
    elseif (trim($link['link_title'])) {
534
      $link['link_title'] = trim($link['link_title']);
535
      $link['link_path'] = "node/$node->nid";
536
      if (trim($link['description'])) {
537
        $link['options']['attributes']['title'] = trim($link['description']);
538
      }
539
      else {
540
        // If the description field was left empty, remove the title attribute
541
        // from the menu link.
542
        unset($link['options']['attributes']['title']);
543
      }
544
      if (!menu_link_save($link)) {
545
        drupal_set_message(t('There was an error saving the menu link.'), 'error');
546
      }
547
    }
548
  }
549
}
550
551
/**
552
 * Implements hook_node_delete().
553
 */
554
function menu_node_delete($node) {
555
  // Delete all menu module links that point to this node.
556
  $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));
557
  foreach ($result as $m) {
558
    menu_link_delete($m['mlid']);
559
  }
560
}
561
562
/**
563
 * Implements hook_node_prepare().
564
 */
565
function menu_node_prepare($node) {
566
  if (empty($node->menu)) {
567
    // Prepare the node for the edit form so that $node->menu always exists.
568
    $menu_name = strtok(variable_get('menu_parent_' . $node->type, 'main-menu:0'), ':');
569
    $item = array();
570
    if (isset($node->nid)) {
571
      $mlid = FALSE;
572
      // Give priority to the default menu
573
      $type_menus = variable_get('menu_options_' . $node->type, array('main-menu' => 'main-menu'));
574
      if (in_array($menu_name, $type_menus)) {
575
        $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(
576
          ':path' => 'node/' . $node->nid,
577
          ':menu_name' => $menu_name,
578
        ))->fetchField();
579
      }
580
      // Check all allowed menus if a link does not exist in the default menu.
581
      if (!$mlid && !empty($type_menus)) {
582
        $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' AND menu_name IN (:type_menus) ORDER BY mlid ASC", 0, 1, array(
583
          ':path' => 'node/' . $node->nid,
584
          ':type_menus' => array_values($type_menus),
585
        ))->fetchField();
586
      }
587
      if ($mlid) {
588
        $item = menu_link_load($mlid);
589
      }
590
    }
591
    // Set default values.
592
    $node->menu = $item + array(
593
      'link_title' => '',
594
      'mlid' => 0,
595
      'plid' => 0,
596
      'menu_name' => $menu_name,
597
      'weight' => 0,
598
      'options' => array(),
599
      'module' => 'menu',
600
      'expanded' => 0,
601
      'hidden' => 0,
602
      'has_children' => 0,
603
      'customized' => 0,
604
    );
605
  }
606
  // Find the depth limit for the parent select.
607
  if (!isset($node->menu['parent_depth_limit'])) {
608
    $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
609
  }
610
}
611
612
/**
613
 * Find the depth limit for items in the parent select.
614
 */
615
function _menu_parent_depth_limit($item) {
616
  return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
617
}
618
619
/**
620
 * Implements hook_form_BASE_FORM_ID_alter().
621
 *
622
 * Adds menu item fields to the node form.
623
 *
624
 * @see menu_node_submit()
625
 */
626
function menu_form_node_form_alter(&$form, $form_state) {
627
  // Generate a list of possible parents (not including this link or descendants).
628
  // @todo This must be handled in a #process handler.
629
  $link = $form['#node']->menu;
630
  $type = $form['#node']->type;
631
  // menu_parent_options() is goofy and can actually handle either a menu link
632
  // or a node type both as second argument. Pick based on whether there is
633
  // a link already (menu_node_prepare() sets mlid default to 0).
634
  $options = menu_parent_options(menu_get_menus(), $link['mlid'] ? $link : $type, $type);
635
  // If no possible parent menu items were found, there is nothing to display.
636
  if (empty($options)) {
637
    return;
638
  }
639
640
  $form['menu'] = array(
641
    '#type' => 'fieldset',
642
    '#title' => t('Menu settings'),
643
    '#access' => user_access('administer menu'),
644
    '#collapsible' => TRUE,
645
    '#collapsed' => !$link['link_title'],
646
    '#group' => 'additional_settings',
647
    '#attached' => array(
648
      'js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
649
    ),
650
    '#tree' => TRUE,
651
    '#weight' => -2,
652
    '#attributes' => array('class' => array('menu-link-form')),
653
  );
654
  $form['menu']['enabled'] = array(
655
    '#type' => 'checkbox',
656
    '#title' => t('Provide a menu link'),
657
    '#default_value' => (int) (bool) $link['mlid'],
658
  );
659
  $form['menu']['link'] = array(
660
    '#type' => 'container',
661
    '#parents' => array('menu'),
662
    '#states' => array(
663
      'invisible' => array(
664
        'input[name="menu[enabled]"]' => array('checked' => FALSE),
665
      ),
666
    ),
667
  );
668
669
  // Populate the element with the link data.
670
  foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
671
    $form['menu']['link'][$key] = array('#type' => 'value', '#value' => $link[$key]);
672
  }
673
674
  $form['menu']['link']['link_title'] = array(
675
    '#type' => 'textfield',
676
    '#title' => t('Menu link title'),
677
    '#default_value' => $link['link_title'],
678
  );
679
680
  $form['menu']['link']['description'] = array(
681
    '#type' => 'textarea',
682
    '#title' => t('Description'),
683
    '#default_value' => isset($link['options']['attributes']['title']) ? $link['options']['attributes']['title'] : '',
684
    '#rows' => 1,
685
    '#description' => t('Shown when hovering over the menu link.'),
686
  );
687
688
  $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
689
  // If the current parent menu item is not present in options, use the first
690
  // available option as default value.
691
  // @todo User should not be allowed to access menu link settings in such a
692
  // case.
693
  if (!isset($options[$default])) {
694
    $array = array_keys($options);
695
    $default = reset($array);
696
  }
697
  $form['menu']['link']['parent'] = array(
698
    '#type' => 'select',
699
    '#title' => t('Parent item'),
700
    '#default_value' => $default,
701
    '#options' => $options,
702
    '#attributes' => array('class' => array('menu-parent-select')),
703
  );
704
  $form['menu']['link']['weight'] = array(
705
    '#type' => 'weight',
706
    '#title' => t('Weight'),
707
    '#delta' => 50,
708
    '#default_value' => $link['weight'],
709
    '#description' => t('Menu links with smaller weights are displayed before links with larger weights.'),
710
  );
711
}
712
713
/**
714
 * Implements hook_node_submit().
715
 *
716
 * @see menu_form_node_form_alter()
717
 */
718
function menu_node_submit($node, $form, $form_state) {
719
  // Decompose the selected menu parent option into 'menu_name' and 'plid', if
720
  // the form used the default parent selection widget.
721
  if (!empty($form_state['values']['menu']['parent'])) {
722
    list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']);
723
  }
724
}
725
726
/**
727
 * Implements hook_form_FORM_ID_alter().
728
 *
729
 * Adds menu options to the node type form.
730
 */
731
function menu_form_node_type_form_alter(&$form, $form_state) {
732
  $menu_options = menu_get_menus();
733
  $type = $form['#node_type'];
734
  $form['menu'] = array(
735
    '#type' => 'fieldset',
736
    '#title' => t('Menu settings'),
737
    '#collapsible' => TRUE,
738
    '#collapsed' => TRUE,
739
    '#attached' => array(
740
      'js' => array(drupal_get_path('module', 'menu') . '/menu.admin.js'),
741
    ),
742
    '#group' => 'additional_settings',
743
  );
744
  $form['menu']['menu_options'] = array(
745
    '#type' => 'checkboxes',
746
    '#title' => t('Available menus'),
747
    '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu')),
748
    '#options' => $menu_options,
749
    '#description' => t('The menus available to place links in for this content type.'),
750
  );
751
  // To avoid an 'illegal option' error after saving the form we have to load
752
  // all available menu items.
753
  // Otherwise it is not possible to dynamically add options to the list.
754
  // @todo Convert menu_parent_options() into a #process callback.
755
  $options = menu_parent_options(menu_get_menus(), array('mlid' => 0));
756
  $form['menu']['menu_parent'] = array(
757
    '#type' => 'select',
758
    '#title' => t('Default parent item'),
759
    '#default_value' => variable_get('menu_parent_' . $type->type, 'main-menu:0'),
760
    '#options' => $options,
761
    '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
762
    '#attributes' => array('class' => array('menu-title-select')),
763
  );
764
765
  // Call Drupal.menu_update_parent_list() to filter the list of
766
  // available default parent menu items based on the selected menus.
767
  drupal_add_js(
768
    '(function ($) { Drupal.menu_update_parent_list(); })(jQuery);',
769
    array('scope' => 'footer', 'type' => 'inline')
770
  );
771
}
772
773
/**
774
 * Return an associative array of the custom menus names.
775
 *
776
 * @param $all
777
 *   If FALSE return only user-added menus, or if TRUE also include
778
 *   the menus defined by the system.
779
 * @return
780
 *   An array with the machine-readable names as the keys, and human-readable
781
 *   titles as the values.
782
 */
783
function menu_get_menus($all = TRUE) {
784
  if ($custom_menus = menu_load_all()) {
785
    if (!$all) {
786
      $custom_menus = array_diff_key($custom_menus, menu_list_system_menus());
787
    }
788
    foreach ($custom_menus as $menu_name => $menu) {
789
      $custom_menus[$menu_name] = t($menu['title']);
790
    }
791
    asort($custom_menus);
792
  }
793
  return $custom_menus;
794
}