| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file 5 * Allows administrators to customize the site navigation menu. 6 */ 7 8 /** 9 * Maximum length of menu name as entered by the user. Database length is 32 10 * and we add a menu- prefix. 11 */ 12 define('MENU_MAX_MENU_NAME_LENGTH_UI', 27); 13 14 /** 15 * Implementation of hook_help(). 16 */ 17 function menu_help($path, $arg) { 18 switch ($path) { 19 case 'admin/help#menu': 20 $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, or menu items, used to navigate a website, and are positioned and displayed using Drupal's flexible block system. By default, three menus are created during installation: <em>Navigation</em>, <em>Primary links</em>, and <em>Secondary links</em>. The <em>Navigation</em> menu contains most links necessary for working with and navigating your site, and is often displayed in either the left or right sidebar. Most Drupal themes also provide support for <em>Primary links</em> and <em>Secondary links</em>, by displaying them in either the header or footer of each page. By default, <em>Primary links</em> and <em>Secondary links</em> contain no menu items but may be configured to contain custom menu items specific to your site.") .'</p>'; 21 $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 item, or to rearrange items 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/build/menu'), '@add-menu' => url('admin/build/menu/add'), '@blocks' => url('admin/build/block'))) .'</p>'; 22 $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>'; 23 return $output; 24 case 'admin/build/menu': 25 return '<p>'. t('Menus are a collection of links (menu items) used to navigate a website. The menus currently available on your site are displayed below. Select a menu from this list to manage its menu items.') .'</p>'; 26 case 'admin/build/menu/add': 27 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/build/block'))) .'</p>'; 28 case 'admin/build/menu-customize/%': 29 return '<p>'. t('To rearrange menu items, grab a drag-and-drop handle under the <em>Menu item</em> column and drag the items (or group of items) to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save configuration</em> button at the bottom of the page.') .'</p>'; 30 case 'admin/build/menu/item/add': 31 return '<p>'. t('Enter the title and path for your new menu item.') .'</p>'; 32 } 33 } 34 35 /** 36 * Implementation of hook_perm(). 37 */ 38 function menu_perm() { 39 return array('administer menu'); 40 } 41 42 /** 43 * Implementation of hook_menu(). 44 */ 45 function menu_menu() { 46 $items['admin/build/menu'] = array( 47 'title' => 'Menus', 48 'description' => "Control your site's navigation menu, primary links and secondary links, as well as rename and reorganize menu items.", 49 'page callback' => 'menu_overview_page', 50 'access callback' => 'user_access', 51 'access arguments' => array('administer menu'), 52 'file' => 'menu.admin.inc', 53 ); 54 55 $items['admin/build/menu/list'] = array( 56 'title' => 'List menus', 57 'type' => MENU_DEFAULT_LOCAL_TASK, 58 'weight' => -10, 59 'file' => 'menu.admin.inc', 60 ); 61 $items['admin/build/menu/add'] = array( 62 'title' => 'Add menu', 63 'page callback' => 'drupal_get_form', 64 'page arguments' => array('menu_edit_menu', 'add'), 65 'access arguments' => array('administer menu'), 66 'type' => MENU_LOCAL_TASK, 67 'file' => 'menu.admin.inc', 68 ); 69 $items['admin/build/menu/settings'] = array( 70 'title' => 'Settings', 71 'page callback' => 'drupal_get_form', 72 'page arguments' => array('menu_configure'), 73 'access arguments' => array('administer menu'), 74 'type' => MENU_LOCAL_TASK, 75 'weight' => 5, 76 'file' => 'menu.admin.inc', 77 ); 78 $items['admin/build/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 'file' => 'menu.admin.inc', 87 ); 88 $items['admin/build/menu-customize/%menu/list'] = array( 89 'title' => 'List items', 90 'weight' => -10, 91 'type' => MENU_DEFAULT_LOCAL_TASK, 92 'file' => 'menu.admin.inc', 93 ); 94 $items['admin/build/menu-customize/%menu/add'] = array( 95 'title' => 'Add item', 96 'page callback' => 'drupal_get_form', 97 'page arguments' => array('menu_edit_item', 'add', NULL, 3), 98 'access arguments' => array('administer menu'), 99 'type' => MENU_LOCAL_TASK, 100 'file' => 'menu.admin.inc', 101 ); 102 $items['admin/build/menu-customize/%menu/edit'] = array( 103 'title' => 'Edit menu', 104 'page callback' => 'drupal_get_form', 105 'page arguments' => array('menu_edit_menu', 'edit', 3), 106 'access arguments' => array('administer menu'), 107 'type' => MENU_LOCAL_TASK, 108 'file' => 'menu.admin.inc', 109 ); 110 $items['admin/build/menu-customize/%menu/delete'] = array( 111 'title' => 'Delete menu', 112 'page callback' => 'menu_delete_menu_page', 113 'page arguments' => array(3), 114 'access arguments' => array('administer menu'), 115 'type' => MENU_CALLBACK, 116 'file' => 'menu.admin.inc', 117 ); 118 $items['admin/build/menu/item/%menu_link/edit'] = array( 119 'title' => 'Edit menu item', 120 'page callback' => 'drupal_get_form', 121 'page arguments' => array('menu_edit_item', 'edit', 4, NULL), 122 'access arguments' => array('administer menu'), 123 'type' => MENU_CALLBACK, 124 'file' => 'menu.admin.inc', 125 ); 126 $items['admin/build/menu/item/%menu_link/reset'] = array( 127 'title' => 'Reset menu item', 128 'page callback' => 'drupal_get_form', 129 'page arguments' => array('menu_reset_item_confirm', 4), 130 'access arguments' => array('administer menu'), 131 'type' => MENU_CALLBACK, 132 'file' => 'menu.admin.inc', 133 ); 134 $items['admin/build/menu/item/%menu_link/delete'] = array( 135 'title' => 'Delete menu item', 136 'page callback' => 'menu_item_delete_page', 137 'page arguments' => array(4), 138 'access arguments' => array('administer menu'), 139 'type' => MENU_CALLBACK, 140 'file' => 'menu.admin.inc', 141 ); 142 143 return $items; 144 } 145 146 /** 147 * Implemenation of hook_theme(). 148 */ 149 function menu_theme() { 150 return array( 151 'menu_overview_form' => array( 152 'file' => 'menu.admin.inc', 153 'arguments' => array('form' => NULL), 154 ), 155 ); 156 } 157 158 /** 159 * Implementation of hook_enable() 160 * 161 * Add a link for each custom menu. 162 */ 163 function menu_enable() { 164 menu_rebuild(); 165 $base_link = db_fetch_array(db_query("SELECT mlid AS plid, menu_name from {menu_links} WHERE link_path = 'admin/build/menu' AND module = 'system'")); 166 $base_link['router_path'] = 'admin/build/menu-customize/%'; 167 $base_link['module'] = 'menu'; 168 $result = db_query("SELECT * FROM {menu_custom}"); 169 while ($menu = db_fetch_array($result)) { 170 // $link is passed by reference to menu_link_save(), so we make a copy of $base_link. 171 $link = $base_link; 172 $link['mlid'] = 0; 173 $link['link_title'] = $menu['title']; 174 $link['link_path'] = 'admin/build/menu-customize/'. $menu['menu_name']; 175 if (!db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND plid = %d", $link['link_path'], $link['plid']))) { 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_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name)); 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 * Implementation of hook_block(). 268 */ 269 function menu_block($op = 'list', $delta = 0) { 270 $menus = menu_get_menus(); 271 // The Navigation menu is handled by the user module. 272 unset($menus['navigation']); 273 if ($op == 'list') { 274 $blocks = array(); 275 foreach ($menus as $name => $title) { 276 // Default "Navigation" block is handled by user.module. 277 $blocks[$name]['info'] = check_plain($title); 278 // Menu blocks can't be cached because each menu item can have 279 // a custom access callback. menu.inc manages its own caching. 280 $blocks[$name]['cache'] = BLOCK_NO_CACHE; 281 } 282 return $blocks; 283 } 284 else if ($op == 'view') { 285 $data['subject'] = check_plain($menus[$delta]); 286 $data['content'] = menu_tree($delta); 287 return $data; 288 } 289 } 290 291 /** 292 * Implementation of hook_nodeapi(). 293 */ 294 function menu_nodeapi(&$node, $op) { 295 switch ($op) { 296 case 'insert': 297 case 'update': 298 if (isset($node->menu)) { 299 $item = &$node->menu; 300 if (!empty($item['delete'])) { 301 menu_link_delete($item['mlid']); 302 } 303 elseif (trim($item['link_title'])) { 304 $item['link_title'] = trim($item['link_title']); 305 $item['link_path'] = "node/$node->nid"; 306 if (!$item['customized']) { 307 $item['options']['attributes']['title'] = trim($node->title); 308 } 309 if (!menu_link_save($item)) { 310 drupal_set_message(t('There was an error saving the menu link.'), 'error'); 311 } 312 } 313 } 314 break; 315 case 'delete': 316 // Delete all menu module links that point to this node. 317 $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu'", $node->nid); 318 while ($m = db_fetch_array($result)) { 319 menu_link_delete($m['mlid']); 320 } 321 break; 322 case 'prepare': 323 if (empty($node->menu)) { 324 // Prepare the node for the edit form so that $node->menu always exists. 325 $menu_name = variable_get('menu_default_node_menu', 'primary-links'); 326 $item = array(); 327 if (isset($node->nid)) { 328 // Give priority to the default menu 329 $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND menu_name = '%s' AND module = 'menu' ORDER BY mlid ASC", $node->nid, $menu_name, 0, 1)); 330 // Check all menus if a link does not exist in the default menu. 331 if (!$mlid) { 332 $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu' ORDER BY mlid ASC", $node->nid, 0, 1)); 333 } 334 if ($mlid) { 335 $item = menu_link_load($mlid); 336 } 337 } 338 // Set default values. 339 $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); 340 } 341 // Find the depth limit for the parent select. 342 if (!isset($node->menu['parent_depth_limit'])) { 343 $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu); 344 } 345 break; 346 } 347 } 348 349 /** 350 * Find the depth limit for items in the parent select. 351 */ 352 function _menu_parent_depth_limit($item) { 353 return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0); 354 } 355 356 /** 357 * Implementation of hook_form_alter(). Adds menu item fields to the node form. 358 */ 359 function menu_form_alter(&$form, $form_state, $form_id) { 360 if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) { 361 // Note - doing this to make sure the delete checkbox stays in the form. 362 $form['#cache'] = TRUE; 363 364 $form['menu'] = array( 365 '#type' => 'fieldset', 366 '#title' => t('Menu settings'), 367 '#access' => user_access('administer menu'), 368 '#collapsible' => TRUE, 369 '#collapsed' => FALSE, 370 '#tree' => TRUE, 371 '#weight' => -2, 372 '#attributes' => array('class' => 'menu-item-form'), 373 ); 374 $item = $form['#node']->menu; 375 376 if ($item['mlid']) { 377 // There is an existing link. 378 $form['menu']['delete'] = array( 379 '#type' => 'checkbox', 380 '#title' => t('Delete this menu item.'), 381 ); 382 } 383 if (!$item['link_title']) { 384 $form['menu']['#collapsed'] = TRUE; 385 } 386 387 foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) { 388 $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]); 389 } 390 $form['menu']['#item'] = $item; 391 392 $form['menu']['link_title'] = array('#type' => 'textfield', 393 '#title' => t('Menu link title'), 394 '#default_value' => $item['link_title'], 395 '#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.'), 396 '#required' => FALSE, 397 ); 398 // Generate a list of possible parents (not including this item or descendants). 399 $options = menu_parent_options(menu_get_menus(), $item); 400 $default = $item['menu_name'] .':'. $item['plid']; 401 if (!isset($options[$default])) { 402 $default = 'primary-links:0'; 403 } 404 $form['menu']['parent'] = array( 405 '#type' => 'select', 406 '#title' => t('Parent item'), 407 '#default_value' => $default, 408 '#options' => $options, 409 '#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)), 410 '#attributes' => array('class' => 'menu-title-select'), 411 ); 412 $form['#submit'][] = 'menu_node_form_submit'; 413 414 $form['menu']['weight'] = array( 415 '#type' => 'weight', 416 '#title' => t('Weight'), 417 '#delta' => 50, 418 '#default_value' => $item['weight'], 419 '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'), 420 ); 421 } 422 } 423 424 /** 425 * Decompose the selected menu parent option into the menu_name and plid. 426 */ 427 function menu_node_form_submit($form, &$form_state) { 428 list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']); 429 } 430 431 /** 432 * Return an associative array of the custom menus names. 433 * 434 * @param $all 435 * If FALSE return only user-added menus, or if TRUE also include 436 * the menus defined by the system. 437 * @return 438 * An array with the machine-readable names as the keys, and human-readable 439 * titles as the values. 440 */ 441 function menu_get_menus($all = TRUE) { 442 $system_menus = menu_list_system_menus(); 443 $sql = 'SELECT * FROM {menu_custom}'; 444 if (!$all) { 445 $sql .= ' WHERE menu_name NOT IN ('. implode(',', array_fill(0, count($system_menus), "'%s'")) .')'; 446 } 447 $sql .= ' ORDER BY title'; 448 $result = db_query($sql, $system_menus); 449 $rows = array(); 450 while ($r = db_fetch_array($result)) { 451 $rows[$r['menu_name']] = $r['title']; 452 } 453 return $rows; 454 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |