Simpletest Coverage - modules/menu/menu.module

1 <?php
2 // $Id: menu.module,v 1.195 2009/07/20 18:51:33 dries 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 );
58 $items['admin/structure/menu/list'] = array(
59 'title' => 'List menus',
60 'type' => MENU_DEFAULT_LOCAL_TASK,
61 'weight' => -10,
62 );
63 $items['admin/structure/menu/add'] = array(
64 'title' => 'Add menu',
65 'page callback' => 'drupal_get_form',
66 'page arguments' => array('menu_edit_menu', 'add'),
67 'access arguments' => array('administer menu'),
68 'type' => MENU_LOCAL_TASK,
69 );
70 $items['admin/structure/menu/settings'] = array(
71 'title' => 'Settings',
72 'page callback' => 'drupal_get_form',
73 'page arguments' => array('menu_configure'),
74 'access arguments' => array('administer menu'),
75 'type' => MENU_LOCAL_TASK,
76 'weight' => 5,
77 );
78 $items['admin/structure/menu-customize/%menu'] = array(
79 'title' => 'Customize menu',
80 'page callback' => 'drupal_get_form',
81 'page arguments' => array('menu_overview_form', 3),
82 'title callback' => 'menu_overview_title',
83 'title arguments' => array(3),
84 'access arguments' => array('administer menu'),
85 'type' => MENU_CALLBACK,
86 );
87 $items['admin/structure/menu-customize/%menu/list'] = array(
88 'title' => 'List links',
89 'weight' => -10,
90 'type' => MENU_DEFAULT_LOCAL_TASK,
91 );
92 $items['admin/structure/menu-customize/%menu/add'] = array(
93 'title' => 'Add link',
94 'page callback' => 'drupal_get_form',
95 'page arguments' => array('menu_edit_item', 'add', NULL, 3),
96 'access arguments' => array('administer menu'),
97 'type' => MENU_LOCAL_TASK,
98 );
99 $items['admin/structure/menu-customize/%menu/edit'] = array(
100 'title' => 'Edit menu',
101 'page callback' => 'drupal_get_form',
102 'page arguments' => array('menu_edit_menu', 'edit', 3),
103 'access arguments' => array('administer menu'),
104 'type' => MENU_LOCAL_TASK,
105 );
106 $items['admin/structure/menu-customize/%menu/delete'] = array(
107 'title' => 'Delete menu',
108 'page callback' => 'menu_delete_menu_page',
109 'page arguments' => array(3),
110 'access arguments' => array('administer menu'),
111 'type' => MENU_CALLBACK,
112 );
113 $items['admin/structure/menu/item/%menu_link/edit'] = array(
114 'title' => 'Edit menu link',
115 'page callback' => 'drupal_get_form',
116 'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
117 'access arguments' => array('administer menu'),
118 'type' => MENU_CALLBACK,
119 );
120 $items['admin/structure/menu/item/%menu_link/reset'] = array(
121 'title' => 'Reset menu link',
122 'page callback' => 'drupal_get_form',
123 'page arguments' => array('menu_reset_item_confirm', 4),
124 'access arguments' => array('administer menu'),
125 'type' => MENU_CALLBACK,
126 );
127 $items['admin/structure/menu/item/%menu_link/delete'] = array(
128 'title' => 'Delete menu link',
129 'page callback' => 'menu_item_delete_page',
130 'page arguments' => array(4),
131 'access arguments' => array('administer menu'),
132 'type' => MENU_CALLBACK,
133 );
134 return $items;
135 }
136
137 /**
138 * Implement hook_theme().
139 */
140 function menu_theme() {
141 return array(
142 'menu_overview_form' => array(
143 'file' => 'menu.admin.inc',
144 'arguments' => array('form' => NULL),
145 ),
146 'menu_admin_overview' => array(
147 'file' => 'menu.admin.inc',
148 'arguments' => array('title' => NULL, 'name' => NULL, 'description' => NULL),
149 ),
150 );
151 }
152
153 /**
154 * Implement hook_enable().
155 *
156 * Add a link for each custom menu.
157 */
158 function menu_enable() {
159 menu_rebuild();
160 $base_link = db_query("SELECT mlid AS plid, menu_name from {menu_links} WHERE link_path = 'admin/structure/menu' AND module = 'system'")->fetchAssoc();
161 $base_link['router_path'] = 'admin/structure/menu-customize/%';
162 $base_link['module'] = 'menu';
163 $result = db_query("SELECT * FROM {menu_custom}", array(), array('fetch' => PDO::FETCH_ASSOC));
164 foreach ($result as $menu) {
165 // $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
166 $link = $base_link;
167 $link['mlid'] = 0;
168 $link['link_title'] = $menu['title'];
169 $link['link_path'] = 'admin/structure/menu-customize/' . $menu['menu_name'];
170 $menu_link = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND plid = :plid", array(
171 ':path' => $link['link_path'],
172 ':plid' => $link['plid']
173 ))
174 ->fetchField();
175 if (!$menu_link) {
176 menu_link_save($link);
177 }
178 }
179 menu_cache_clear_all();
180 }
181
182 /**
183 * Title callback for the menu overview page and links.
184 */
185 function menu_overview_title($menu) {
186 return $menu['title'];
187 }
188
189 /**
190 * Load the data for a single custom menu.
191 */
192 function menu_load($menu_name) {
193 return db_query("SELECT * FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu_name))->fetchAssoc();
194 }
195
196 /**
197 * Return a list of menu items that are valid possible parents for the given menu item.
198 *
199 * @param $menus
200 * An array of menu names and titles, such as from menu_get_menus().
201 * @param $item
202 * The menu item for which to generate a list of parents.
203 * If $item['mlid'] == 0 then the complete tree is returned.
204 * @return
205 * An array of menu link titles keyed on the a string containing the menu name
206 * and mlid. The list excludes the given item and its children.
207 */
208 function menu_parent_options($menus, $item) {
209 // The menu_links table can be practically any size and we need a way to
210 // allow contrib modules to provide more scalable pattern choosers.
211 // hook_form_alter is too late in itself because all the possible parents are
212 // retrieved here, unless menu_override_parent_selector is set to TRUE.
213 if (variable_get('menu_override_parent_selector', FALSE)) {
214 return array();
215 }
216 // If the item has children, there is an added limit to the depth of valid parents.
217 if (isset($item['parent_depth_limit'])) {
218 $limit = $item['parent_depth_limit'];
219 }
220 else {
221 $limit = _menu_parent_depth_limit($item);
222 }
223
224 foreach ($menus as $menu_name => $title) {
225 $tree = menu_tree_all_data($menu_name, NULL);
226 $options[$menu_name . ':0'] = '<' . $title . '>';
227 _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
228 }
229 return $options;
230 }
231
232 /**
233 * Recursive helper function for menu_parent_options().
234 */
235 function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
236 foreach ($tree as $data) {
237 if ($data['link']['depth'] > $depth_limit) {
238 // Don't iterate through any links on this level.
239 break;
240 }
241 if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
242 $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
243 if ($data['link']['hidden']) {
244 $title .= ' (' . t('disabled') . ')';
245 }
246 $options[$menu_name . ':' . $data['link']['mlid']] = $title;
247 if ($data['below']) {
248 _menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
249 }
250 }
251 }
252 }
253
254 /**
255 * Reset a system-defined menu item.
256 */
257 function menu_reset_item($item) {
258 $new_item = _menu_link_build(menu_get_item($item['router_path']));
259 foreach (array('mlid', 'has_children') as $key) {
260 $new_item[$key] = $item[$key];
261 }
262 menu_link_save($new_item);
263 return $new_item;
264 }
265
266 /**
267 * Implement hook_block_list().
268 */
269 function menu_block_list() {
270 $menus = menu_get_menus(FALSE);
271
272 $blocks = array();
273 foreach ($menus as $name => $title) {
274 // Default "Navigation" block is handled by user.module.
275 $blocks[$name]['info'] = check_plain($title);
276 // Menu blocks can't be cached because each menu item can have
277 // a custom access callback. menu.inc manages its own caching.
278 $blocks[$name]['cache'] = BLOCK_NO_CACHE;
279 }
280 return $blocks;
281 }
282
283 /**
284 * Implement hook_block_view().
285 */
286 function menu_block_view($delta = '') {
287 $menus = menu_get_menus(FALSE);
288 $data['subject'] = check_plain($menus[$delta]);
289 $data['content'] = menu_tree($delta);
290 return $data;
291 }
292
293 /**
294 * Implement hook_node_insert().
295 */
296 function menu_node_insert($node) {
297 if (isset($node->menu)) {
298 $item = &$node->menu;
299 if (!empty($item['delete'])) {
300 menu_link_delete($item['mlid']);
301 }
302 elseif (trim($item['link_title'])) {
303 $item['link_title'] = trim($item['link_title']);
304 $item['link_path'] = "node/$node->nid";
305 if (!$item['customized']) {
306 $item['options']['attributes']['title'] = trim($node->title);
307 }
308 if (!menu_link_save($item)) {
309 drupal_set_message(t('There was an error saving the menu link.'), 'error');
310 }
311 }
312 }
313 }
314
315 /**
316 * Implement hook_node_update().
317 */
318 function menu_node_update($node) {
319 if (isset($node->menu)) {
320 $item = &$node->menu;
321 if (!empty($item['delete'])) {
322 menu_link_delete($item['mlid']);
323 }
324 elseif (trim($item['link_title'])) {
325 $item['link_title'] = trim($item['link_title']);
326 $item['link_path'] = "node/$node->nid";
327 if (!$item['customized']) {
328 $item['options']['attributes']['title'] = trim($node->title);
329 }
330 if (!menu_link_save($item)) {
331 drupal_set_message(t('There was an error saving the menu link.'), 'error');
332 }
333 }
334 }
335 }
336
337 /**
338 * Implement hook_node_delete().
339 */
340 function menu_node_delete($node) {
341 // Delete all menu module links that point to this node.
342 $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));
343 foreach ($result as $m) {
344 menu_link_delete($m['mlid']);
345 }
346 }
347
348 /**
349 * Implement hook_node_prepare().
350 */
351 function menu_node_prepare($node) {
352 if (empty($node->menu)) {
353 // Prepare the node for the edit form so that $node->menu always exists.
354 $menu_name = variable_get('menu_default_node_menu', 'main-menu');
355 $item = array();
356 if (isset($node->nid)) {
357 // Give priority to the default menu
358 $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", array(
359 ':path' => 'node/' . $node->nid,
360 ':menu_name' => $menu_name,
361 ), 0, 1)
362 ->fetchField();
363 // Check all menus if a link does not exist in the default menu.
364 if (!$mlid) {
365 $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' ORDER BY mlid ASC", array(
366 ':path' => 'node/' . $node->nid,
367 ), 0, 1)
368 ->fetchField();
369 }
370 if ($mlid) {
371 $item = menu_link_load($mlid);
372 }
373 }
374 // Set default values.
375 $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);
376 }
377 // Find the depth limit for the parent select.
378 if (!isset($node->menu['parent_depth_limit'])) {
379 $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
380 }
381 }
382
383 /**
384 * Find the depth limit for items in the parent select.
385 */
386 function _menu_parent_depth_limit($item) {
387 return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
388 }
389
390 /**
391 * Implement hook_form_alter(). Adds menu item fields to the node form.
392 */
393 function menu_form_alter(&$form, $form_state, $form_id) {
394 if (!empty($form['#node_edit_form'])) {
395 // Note - doing this to make sure the delete checkbox stays in the form.
396 $form['#cache'] = TRUE;
397
398 $form['menu'] = array(
399 '#type' => 'fieldset',
400 '#title' => t('Menu settings'),
401 '#access' => user_access('administer menu'),
402 '#collapsible' => TRUE,
403 '#collapsed' => FALSE,
404 '#group' => 'additional_settings',
405 '#attached_js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
406 '#tree' => TRUE,
407 '#weight' => -2,
408 '#attributes' => array('class' => 'menu-item-form'),
409 );
410 $item = $form['#node']->menu;
411
412 if ($item['mlid']) {
413 // There is an existing link.
414 $form['menu']['delete'] = array(
415 '#type' => 'checkbox',
416 '#title' => t('Delete this menu item.'),
417 );
418 }
419 if (!$item['link_title']) {
420 $form['menu']['#collapsed'] = TRUE;
421 }
422
423 foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
424 $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
425 }
426 $form['menu']['#item'] = $item;
427
428 $form['menu']['link_title'] = array('#type' => 'textfield',
429 '#title' => t('Menu link title'),
430 '#default_value' => $item['link_title'],
431 '#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.'),
432 '#required' => FALSE,
433 );
434 // Generate a list of possible parents (not including this item or descendants).
435 $options = menu_parent_options(menu_get_menus(), $item);
436 $default = $item['menu_name'] . ':' . $item['plid'];
437 if (!isset($options[$default])) {
438 $default = 'main-menu:0';
439 }
440 $form['menu']['parent'] = array(
441 '#type' => 'select',
442 '#title' => t('Parent item'),
443 '#default_value' => $default,
444 '#options' => $options,
445 '#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)),
446 '#attributes' => array('class' => 'menu-title-select'),
447 );
448 $form['#submit'][] = 'menu_node_form_submit';
449
450 $form['menu']['weight'] = array(
451 '#type' => 'weight',
452 '#title' => t('Weight'),
453 '#delta' => 50,
454 '#default_value' => $item['weight'],
455 '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
456 );
457 }
458 }
459
460 /**
461 * Decompose the selected menu parent option into the menu_name and plid.
462 */
463 function menu_node_form_submit($form, &$form_state) {
464 list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']);
465 }
466
467 /**
468 * Return an associative array of the custom menus names.
469 *
470 * @param $all
471 * If FALSE return only user-added menus, or if TRUE also include
472 * the menus defined by the system.
473 * @return
474 * An array with the machine-readable names as the keys, and human-readable
475 * titles as the values.
476 */
477 function menu_get_menus($all = TRUE) {
478 $system_menus = array_keys(menu_list_system_menus());
479 $query = db_select('menu_custom');
480 $query->addField('menu_custom', 'menu_name', 'menu_name');
481 $query->addField('menu_custom', 'title', 'title');
482 if (!$all) {
483 $query->condition('menu_name', $system_menus, 'NOT IN');
484 }
485 $query->orderBy('title');
486
487 return $query->execute()->fetchAllKeyed();
488 }
489

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.