You are here

admin_menu.module in Administration menu 6.3

Render an administrative menu as a dropdown menu at the top of the window.

Note: Most theme-functions in Administration Menu are not invoked via theme(), because we try to keep this module as fast as possible and chances are very small that someone wants to override those functions.

File

admin_menu.module
View source
<?php

/**
 * @file
 * Render an administrative menu as a dropdown menu at the top of the window.
 *
 * Note: Most theme-functions in Administration Menu are not invoked via theme(),
 * because we try to keep this module as fast as possible and chances are very
 * small that someone wants to override those functions.
 */

/**
 * Implements hook_help().
 */
function admin_menu_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/admin_menu':
      return t('The administration menu module provides a dropdown menu arranged for one- or two-click access to most administrative tasks and other common destinations (to users with the proper permissions). Use the settings below to customize the appearance of the menu.');
    case 'admin/help#admin_menu':
      $output = '';
      $output .= '<p>' . t('The administration menu module provides a dropdown menu arranged for one- or two-click access to most administrative tasks and other common destinations (to users with the proper permissions). Administration menu also displays the number of anonymous and authenticated users, and allows modules to add their own custom menu items. Integration with the menu varies from module to module; the contributed module <a href="@drupal">Devel</a>, for instance, makes strong use of the administration menu module to provide quick access to development tools.', array(
        '@drupal' => 'http://drupal.org/project/devel',
      )) . '</p>';
      $output .= '<p>' . t('The administration menu <a href="@settings">settings page</a> allows you to modify some elements of the menu\'s behavior and appearance. Since the appearance of the menu is dependent on your site theme, substantial customizations require modifications to your site\'s theme and CSS files. See the advanced module README.txt file for more information on theme and CSS customizations.', array(
        '@settings' => url('admin/settings/admin_menu'),
      )) . '</p>';
      $output .= '<p>' . t('The menu items displayed in the administration menu depend upon the actual permissions of the viewer. First, the administration menu is only displayed to users in roles with the <em>Access administration menu</em> (admin_menu module) permission. Second, a user must be a member of a role with the <em>Access administration pages</em> (system module) permission to view administrative links. And, third, only currently permitted links are displayed; for example, if a user is not a member of a role with the permissions <em>Administer permissions</em> (user module) and <em>Administer users</em> (user module), the <em>User management</em> menu item is not displayed.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_perm().
 */
function admin_menu_perm() {
  return array(
    'access administration menu',
    'display drupal links',
  );
}

/**
 * Implements hook_theme().
 */
function admin_menu_theme() {
  return array(
    'admin_menu_links' => array(
      'arguments' => array(
        'elements' => array(),
      ),
    ),
    'admin_menu_icon' => array(
      'arguments' => array(),
      'file' => 'admin_menu.inc',
    ),
  );
}

/**
 * Implements hook_menu().
 */
function admin_menu_menu() {

  // AJAX callback.
  // @see http://drupal.org/project/js
  $items['js/admin_menu/cache'] = array(
    'page callback' => 'admin_menu_js_cache',
    'access arguments' => array(
      'access administration menu',
    ),
    'type' => MENU_CALLBACK,
  );

  // Module settings.
  $items['admin/settings/admin_menu'] = array(
    'title' => 'Administration menu',
    'description' => 'Adjust administration menu settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'admin_menu_theme_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'admin_menu.inc',
  );

  // Menu link callbacks.
  $items['admin_menu/toggle-modules'] = array(
    'page callback' => 'admin_menu_toggle_modules',
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'admin_menu.inc',
  );
  $items['admin_menu/flush-cache'] = array(
    'page callback' => 'admin_menu_flush_cache',
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'admin_menu.inc',
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function admin_menu_menu_alter(&$items) {
  foreach ($items as $path => $item) {
    if (!strncmp($path, 'admin/', 6)) {
      if (isset($item['type'])) {

        // For any reason, content-type menu items are registered individually
        // in Drupal core, but also by CCK. We a) have to copy and re-assign
        // them the proper router path and b) turn their parent items from
        // MENU_CALLBACK into something visible; otherwise, the menu system
        // does not find the parents and relocates child items to the top-level.
        if (strpos($path, 'admin/content/node-type/') === 0) {

          // Copy item with fixed router path.
          $newpath = str_replace('/node-type/', '/types/', $path);
          $items[$newpath] = $item;

          // Use new item from here, but leave old intact to play nice with
          // others.
          $path = $newpath;

          // Turn MENU_CALLBACK items into visible menu items.
          if ($item['type'] == MENU_CALLBACK) {
            $items[$path]['type'] = MENU_NORMAL_ITEM;
          }
        }

        // Skip invisible MENU_CALLBACKs.
        if ($item['type'] == MENU_CALLBACK) {
          continue;
        }

        // Trick local tasks and actions into the visible menu tree.
        if ($item['type'] & MENU_IS_LOCAL_TASK) {
          $items[$path]['_visible'] = TRUE;
        }
        $items[$path]['menu_name'] = 'admin_menu';
      }
    }
  }

  // Remove 'admin', so children appear on the top-level.
  $items['admin']['menu_name'] = 'admin_menu';

  // Remove local tasks on 'admin'.
  $items['admin/by-task']['_visible'] = FALSE;
  $items['admin/by-module']['_visible'] = FALSE;

  // menu_link_save() looks up the parent's menu_name directly in the database
  // instead of taking data in hook_menu() into account. When installing
  // admin_menu, {menu_links} contains the default value of 'navigation', which
  // is thereby applied to all items, even if this function altered them. There
  // is no other way to override this behavior.
  // Additionally, if links below admin/ were moved or customized in any way and
  // those are moved back into the administration menu, then the menu system
  // will not calculate/assign the proper menu link parents when the parent item
  // (here: 'admin') is marked as customized.
  db_query("UPDATE {menu_links} SET menu_name = 'admin_menu', customized = 0 WHERE router_path = 'admin'");

  // Flush client-side caches whenever the menu is rebuilt.
  admin_menu_flush_caches();
}

/**
 * Implements hook_menu_link_alter().
 */
function admin_menu_menu_link_alter(&$item, $menu) {

  // @todo Temporary fix until #550254 has been released.
  // @see menu_link_save()
  // Only process links during menu router rebuild, belonging to our menu.
  if (!isset($menu) || !($item['menu_name'] == 'admin_menu')) {
    return;
  }

  // If no explicit plid is defined as parent, then the the re-parenting process
  // is always invoked, since there is no guarantee that a plid of 0 is not
  // caused by a re-parenting process that went wrong previously. For example,
  // local tasks may be re-parented to the top-level (0) when tab_root points to
  // an item of type MENU_CALLBACK.
  if (!empty($item['plid'])) {
    $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $item['plid']));
  }
  else {

    // Find the parent - it must be unique.
    $parent_path = $item['link_path'];
    $where = "WHERE link_path = '%s'";

    // Only links derived from router items should have module == 'system', and
    // we want to find the parent even if it's in a different menu.
    if ($item['module'] == 'system') {
      $where .= " AND module = '%s'";
      $arg2 = 'system';
    }
    else {

      // If not derived from a router item, we respect the specified menu name.
      $where .= " AND menu_name = '%s'";
      $arg2 = $item['menu_name'];
    }
    do {
      $parent = FALSE;
      $parent_path = substr($parent_path, 0, strrpos($parent_path, '/'));
      $result = db_query("SELECT COUNT(*) FROM {menu_links} " . $where, $parent_path, $arg2);

      // Only valid if we get a unique result.
      if (db_result($result) == 1) {
        $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} " . $where, $parent_path, $arg2));
      }
    } while ($parent === FALSE && $parent_path);
  }
  if ($parent !== FALSE) {

    // If a parent link was found, derive its menu.
    $item['menu_name'] = $parent['menu_name'];

    // Whenever we retrieve a parent link for a router item from the database,
    // we need to update the parent link's properties according to the new
    // router item. Otherwise, the re-parenting process gets stuck on the menu
    // router data that was stored in {menu_links} in a previous menu rebuild.
    if (isset($menu) && !empty($parent['router_path']) && isset($menu[$parent['router_path']])) {
      $parent = array_merge($parent, _menu_link_build($menu[$parent['router_path']]));
    }
  }
  $menu_name = $item['menu_name'];

  // Menu callbacks need to be in the links table for breadcrumbs, but can't
  // be parents if they are generated directly from a router item.
  if (empty($parent['mlid']) || $parent['hidden'] < 0) {
    $item['plid'] = 0;
  }
  else {
    $item['plid'] = $parent['mlid'];
  }
}

/**
 * Implements hook_init().
 *
 * We can't move this into admin_menu_footer(), because PHP-only based themes
 * like chameleon load and output scripts and stylesheets in front of
 * theme_closure(), so we ensure Admin menu's styles and scripts are loaded on
 * all pages via hook_init().
 */
function admin_menu_init() {

  // Re-enable breadcrumbs on administrative pages by setting 'admin_menu' as
  // the active menu name. Also applies to users without access to the
  // Administration menu as they should also get properly working breadcrumbs on
  // administrative pages.
  // @see admin_menu_menu_alter()
  if (arg(0) == 'admin') {
    menu_set_active_menu_name('admin_menu');
  }
  if (!user_access('access administration menu') || admin_menu_suppress(FALSE)) {
    return;
  }

  // Performance: Skip this entirely for AJAX requests.
  if (strpos($_GET['q'], 'js/') === 0) {
    return;
  }
  global $user, $language;
  $path = drupal_get_path('module', 'admin_menu');
  drupal_add_css($path . '/admin_menu.css', 'module', 'all', FALSE);
  if ($user->uid == 1) {
    drupal_add_css($path . '/admin_menu.uid1.css', 'module', 'all', FALSE);
  }

  // Previous versions used the 'defer' attribute to increase browser rendering
  // performance. At least starting with Firefox 3.6, deferred .js files are
  // loaded, but Drupal.behaviors are not contained in the DOM when drupal.js
  // executes Drupal.attachBehaviors().
  drupal_add_js($path . '/admin_menu.js');

  // Destination query strings are applied via JS.
  $settings['destination'] = drupal_get_destination();

  // Hash for client-side HTTP/AJAX caching.
  $cid = 'admin_menu:' . $user->uid . ':' . session_id() . ':' . $language->language;
  if (!empty($_COOKIE['has_js']) && ($hash = admin_menu_cache_get($cid))) {
    $settings['hash'] = $hash;

    // The base path to use for cache requests depends on whether clean URLs
    // are enabled, whether Drupal runs in a sub-directory, and on the language
    // system configuration. url() already provides us the proper path and also
    // invokes potentially existing custom_url_rewrite() functions, which may
    // add further required components to the URL to provide context. Due to
    // those components, and since url('') returns only base_path() when clean
    // URLs are disabled, we need to use a replacement token as path.  Yuck.
    $settings['basePath'] = url('admin_menu');
  }
  $replacements = module_invoke_all('admin_menu_replacements');
  if (!empty($replacements)) {
    $settings['replacements'] = $replacements;
  }
  if ($setting = variable_get('admin_menu_margin_top', 1)) {
    $settings['margin_top'] = $setting;
  }
  if ($setting = variable_get('admin_menu_position_fixed', 0)) {
    $settings['position_fixed'] = $setting;
  }
  if ($setting = variable_get('admin_menu_tweak_tabs', 0)) {
    $settings['tweak_tabs'] = $setting;
  }
  if ($_GET['q'] == 'admin/build/modules' || strpos($_GET['q'], 'admin/build/modules/list') === 0) {
    $settings['tweak_modules'] = variable_get('admin_menu_tweak_modules', 0);
  }
  if ($_GET['q'] == 'admin/user/permissions') {
    $settings['tweak_permissions'] = variable_get('admin_menu_tweak_permissions', 0);
  }
  drupal_add_js(array(
    'admin_menu' => $settings,
  ), 'setting');
}

/**
 * Suppress display of administration menu.
 *
 * This function should be called from within another module's page callback
 * (preferably using module_invoke()) when the menu should not be displayed.
 * This is useful for modules that implement popup pages or other special
 * pages where the menu would be distracting or break the layout.
 *
 * @param $set
 *   Defaults to TRUE. If called before hook_footer(), the menu will not be
 *   displayed. If FALSE is passed, the suppression state is returned.
 */
function admin_menu_suppress($set = TRUE) {
  static $suppress = FALSE;

  // drupal_add_js() must only be invoked once.
  if (!empty($set) && $suppress === FALSE) {
    $suppress = TRUE;
    drupal_add_js(array(
      'admin_menu' => array(
        'suppress' => 1,
      ),
    ), 'setting');
  }
  return $suppress;
}

/**
 * Implements hook_footer().
 */
function admin_menu_footer($main = 0) {
  return admin_menu_output();
}

/**
 * Implements hook_js().
 */
function admin_menu_js() {
  return array(
    'cache' => array(
      'callback' => 'admin_menu_js_cache',
      'includes' => array(
        'common',
        'theme',
        'unicode',
      ),
      'dependencies' => array(
        'devel',
        'filter',
        'user',
      ),
    ),
  );
}

/**
 * Retrieve a client-side cache hash from cache.
 *
 * The hash cache is consulted more than once per request; we therefore cache
 * the results statically to avoid multiple database requests.
 *
 * This should only be used for client-side cache hashes. Use cache_menu for
 * administration menu content.
 *
 * @param $cid
 *   The cache ID of the data to retrieve.
 */
function admin_menu_cache_get($cid) {
  static $cache = array();
  if (!variable_get('admin_menu_cache_client', TRUE)) {
    return FALSE;
  }
  if (!array_key_exists($cid, $cache)) {
    $cache[$cid] = cache_get($cid, 'cache_admin_menu');
    if ($cache[$cid] && isset($cache[$cid]->data)) {
      $cache[$cid] = $cache[$cid]->data;
    }
  }
  return $cache[$cid];
}

/**
 * Store a client-side cache hash in persistent cache.
 *
 * This should only be used for client-side cache hashes. Use cache_menu for
 * administration menu content.
 *
 * @param $cid
 *   The cache ID of the data to retrieve.
 */
function admin_menu_cache_set($cid, $data) {
  if (variable_get('admin_menu_cache_client', TRUE)) {
    cache_set($cid, $data, 'cache_admin_menu');
  }
}

/**
 * Menu callback; Output administration menu for HTTP caching via AJAX request.
 */
function admin_menu_js_cache($hash = NULL) {

  // Get the rendered menu.
  $content = admin_menu_output();

  // @todo According to http://www.mnot.net/blog/2006/05/11/browser_caching,
  //   IE will only cache the content when it is compressed.
  // Determine if the client accepts gzipped data.
  if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE) {
      $encoding = 'x-gzip';
    }
    elseif (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
      $encoding = 'gzip';
    }

    // Perform gzip compression when:
    // 1) the user agent supports gzip compression.
    // 2) Drupal page compression is enabled. Sites may wish to perform the
    //    compression at the web server level (e.g. using mod_deflate).
    // 3) PHP's zlib extension is loaded, but zlib compression is disabled.
    if (isset($encoding) && variable_get('page_compression', TRUE) && extension_loaded('zlib') && zlib_get_coding_type() === FALSE) {

      // Use Vary header to tell caches to keep separate versions of the menu
      // based on user agent capabilities.
      header('Vary: Accept-Encoding');

      // Since we manually perform compression, we are also responsible to
      // send a proper encoding header.
      header('Content-Encoding: ' . $encoding);
      $content = gzencode($content, 9, FORCE_GZIP);
    }
  }
  $max_age = 3600 * 24 * 365;
  header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT');
  header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
  header('Cache-Control: max-age=' . $max_age . ', private');
  header('Content-Type: text/html; charset=utf-8');

  // Suppress Devel module.
  $GLOBALS['devel_shutdown'] = FALSE;
  echo $content;
  exit;
}

/**
 * Implements hook_admin_menu_replacements().
 */
function admin_menu_admin_menu_replacements() {
  $items = array();
  if ($user_count = admin_menu_get_user_count()) {
    $items['.admin-menu-users a'] = $user_count;
  }
  return $items;
}

/**
 * Return count of online anonymous/authenticated users.
 *
 * @see user_block(), user.module
 */
function admin_menu_get_user_count() {
  $interval = time() - variable_get('user_block_seconds_online', 900);
  $count_anon = sess_count($interval);
  $count_auth = db_result(db_query("SELECT COUNT(DISTINCT uid) FROM {sessions} WHERE uid > 0 AND timestamp >= %d", $interval));
  return t('@count-anon / @count-auth', array(
    '@count-anon' => $count_anon,
    '@count-auth' => $count_auth,
  ));
}

/**
 * Build the administration menu output.
 */
function admin_menu_output() {
  if (!user_access('access administration menu') || admin_menu_suppress(FALSE)) {
    return;
  }
  global $user, $language;
  $cache_server_enabled = variable_get('admin_menu_cache_server', TRUE);
  $cid = 'admin_menu:' . $user->uid . ':' . session_id() . ':' . $language->language;

  // Do nothing at all here if the client supports client-side caching, the user
  // has a hash, and is NOT requesting the cache update path. Consult the hash
  // cache last, since it requires a DB request.
  // @todo Implement a sanity-check to prevent permanent double requests; i.e.
  //   what if the client-side cache fails for any reason and performs a second
  //   request on every page?
  if (!empty($_COOKIE['has_js']) && strpos($_GET['q'], 'js/admin_menu/cache') !== 0) {
    if (admin_menu_cache_get($cid)) {
      return;
    }
  }

  // Try to load and output administration menu from server-side cache.
  if ($cache_server_enabled) {
    $cache = cache_get($cid, 'cache_menu');
    if ($cache && isset($cache->data)) {
      $content = $cache->data;
    }
  }

  // Rebuild the output.
  if (!isset($content)) {

    // Add site name as CSS class for development/staging theming purposes. We
    // leverage the cookie domain instead of HTTP_HOST to account for many (but
    // not all) multi-domain setups (e.g. language-based sub-domains).
    $class_site = 'admin-menu-site' . drupal_strtolower(preg_replace('/[^a-zA-Z0-9-]/', '-', $GLOBALS['cookie_domain']));

    // @todo Always output container to harden JS-less support.
    $content['#prefix'] = '<div id="admin-menu" class="' . $class_site . '"><div id="admin-menu-wrapper"><ul>';
    $content['#suffix'] = '</ul></div></div>';

    // Load menu builder functions.
    module_load_include('inc', 'admin_menu');

    // Add administration menu.
    $content['menu'] = admin_menu_links_menu(menu_tree_all_data('admin_menu'));
    $content['menu']['#theme'] = 'admin_menu_links';

    // Ensure the menu tree is rendered between the icon and user links.
    $content['menu']['#weight'] = 0;

    // Add menu additions.
    $content['icon'] = admin_menu_links_icon();
    $content['user'] = admin_menu_links_user();

    // Allow modules to enhance the menu.
    // Uses '_output' suffix for consistency with the alter hook (see below).
    foreach (module_implements('admin_menu_output_build') as $module) {
      $function = $module . '_admin_menu_output_build';
      $function($content);
    }

    // Allow modules to alter the output.
    // The '_output' suffix is required to prevent hook implementation function
    // name clashes with the contributed Admin module.
    drupal_alter('admin_menu_output', $content);
    $content = drupal_render($content);

    // Cache the menu for this user.
    if ($cache_server_enabled) {
      cache_set($cid, $content, 'cache_menu');
    }
  }

  // Store the new hash for this user.
  if (!empty($_COOKIE['has_js'])) {
    admin_menu_cache_set($cid, md5($content));
  }
  return $content;
}

/**
 * Implements hook_admin_menu_output_build().
 */
function admin_menu_admin_menu_output_build(&$content) {

  // Retrieve the "Add content" link tree.
  $link = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE router_path = 'node/add' AND module = 'system'"));
  $conditions = array();
  for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
    if (!empty($link["p{$i}"])) {
      $conditions["p{$i}"] = $link["p{$i}"];
    }
  }

  // Forked from menu.inc.
  // @see menu_tree_all_data()
  $where = '';
  $args = array();
  foreach ($conditions as $column => $value) {
    $where .= " AND {$column} = %d";
    $args[] = $value;
  }
  $parents = $args;
  array_unshift($args, $link['menu_name']);
  $data['tree'] = menu_tree_data(db_query("\n    SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*\n    FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path\n    WHERE ml.menu_name = '%s'" . $where . "\n    ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
  $data['node_links'] = array();
  menu_tree_collect_node_links($data['tree'], $data['node_links']);
  menu_tree_check_access($data['tree'], $data['node_links']);
  $tree = $data['tree'];
  $links = admin_menu_links_menu($tree);
  if (!empty($links)) {

    // If the user has access to the top-level "Content" category, insert the
    // "Add content" link tree there.
    if (isset($content['menu']['admin/content'])) {
      $content['menu']['admin/content'] += $links;
    }
    else {
      $key = key($links);
      $links[$key]['#weight'] = -100;
      $content['menu'] += $links;
    }
  }
}

/**
 * Render a themed list of links.
 *
 * @param $elements
 *   A structured drupal_render()-array of links using the following keys:
 *   - '#attributes': Optional array of attributes for the list item, processed
 *     via drupal_attributes().
 *     Note that we use an array for 'class'.
 *   - '#title': Title of the link, passed to l().
 *   - '#href': Optional path of the link, passed to l(). When omitted, the
 *     element's '#title' is rendered without link.
 *   - '#description': Optional alternative text for the link, passed to l().
 *   - '#options': Optional alternative text for the link, passed to l().
 *
 *   The array key of each child element itself is passed as path for l().
 * @param $depth
 *   Current recursion level; internal use only.
 */
function theme_admin_menu_links($elements, $depth = 0) {
  static $destination, $destination_array;
  if (!isset($destination)) {
    $destination = drupal_get_destination();
    $destination_array = explode('=', $destination);
    $destination_array = array(
      $destination_array[0] => urldecode($destination_array[1]),
    );
  }

  // The majority of items in the menu are sorted already, but since modules
  // may add or change arbitrary items anywhere, there is no way around sorting
  // everything again. element_sort() is not sufficient here, as it
  // intentionally retains the order of elements having the same #weight,
  // whereas menu links are supposed to be ordered by #weight and #title.
  uasort($elements, 'admin_menu_element_sort');
  $elements['#sorted'] = TRUE;
  $output = '';
  $depth_child = $depth + 1;
  foreach (element_children($elements) as $path) {

    // Early-return nothing if user does not have access.
    if (isset($elements[$path]['#access']) && !$elements[$path]['#access']) {
      continue;
    }
    $elements[$path] += array(
      '#attributes' => array(),
      '#options' => array(),
    );

    // Render children to determine whether this link is expandable.
    if (isset($elements[$path]['#type']) || isset($elements[$path]['#theme'])) {
      $elements[$path]['#admin_menu_depth'] = $depth_child;
      $elements[$path]['#children'] = drupal_render($elements[$path]);
    }
    else {
      $elements[$path]['#children'] = theme('admin_menu_links', $elements[$path], $depth_child);
      if (!empty($elements[$path]['#children'])) {
        $elements[$path]['#attributes']['class'][] = 'expandable';
      }
      if (isset($elements[$path]['#attributes']['class'])) {
        $elements[$path]['#attributes']['class'] = implode(' ', $elements[$path]['#attributes']['class']);
      }
    }
    $link = '';
    if (isset($elements[$path]['#href'])) {

      // Strip destination query string from href attribute and apply a CSS class
      // for our JavaScript behavior instead.
      $class = FALSE;
      if (!empty($elements[$path]['#options']['query'])) {
        if (is_array($elements[$path]['#options']['query']) && isset($elements[$path]['#options']['query']['destination']) && $elements[$path]['#options']['query']['destination'] == $destination_array['destination']) {
          unset($elements[$path]['#options']['query']['destination']);
          $class = TRUE;
        }
        else {
          $query = preg_replace('/&?' . preg_quote($destination, '/') . '/', '', $elements[$path]['#options']['query']);
          if ($query != $elements[$path]['#options']['query']) {
            $elements[$path]['#options']['query'] = $query;
            $class = TRUE;
          }
        }
      }
      if ($class) {
        if (!isset($elements[$path]['#options']['attributes']['class'])) {
          $elements[$path]['#options']['attributes']['class'] = '';
        }
        $elements[$path]['#options']['attributes']['class'] .= ' admin-menu-destination';
      }
      $link .= l($elements[$path]['#title'], $elements[$path]['#href'], $elements[$path]['#options']);
    }
    elseif (isset($elements[$path]['#title'])) {
      if (!empty($elements[$path]['#options']['html'])) {
        $title = $elements[$path]['#title'];
      }
      else {
        $title = check_plain($elements[$path]['#title']);
      }
      if (!empty($elements[$path]['#options']['attributes'])) {
        $link .= '<span' . drupal_attributes($elements[$path]['#options']['attributes']) . '>' . $title . '</span>';
      }
      else {
        $link .= $title;
      }
    }
    $output .= '<li' . drupal_attributes($elements[$path]['#attributes']) . '>';
    $output .= $link . $elements[$path]['#children'];
    $output .= '</li>';
  }

  // @todo #attributes probably required for UL, but already used for LI.
  // @todo Use $element['#children'] here instead.
  if ($output && $depth > 0) {
    $output = "\n<ul>" . $output . '</ul>';
  }
  return $output;
}

/**
 * Function used by uasort to sort structured arrays by #weight AND #title.
 */
function admin_menu_element_sort($a, $b) {

  // @see element_sort()
  $a_weight = is_array($a) && isset($a['#weight']) ? $a['#weight'] : 0;
  $b_weight = is_array($b) && isset($b['#weight']) ? $b['#weight'] : 0;
  if ($a_weight == $b_weight) {

    // @see element_sort_by_title()
    $a_title = is_array($a) && isset($a['#title']) ? $a['#title'] : '';
    $b_title = is_array($b) && isset($b['#title']) ? $b['#title'] : '';
    return strnatcasecmp($a_title, $b_title);
  }
  return $a_weight < $b_weight ? -1 : 1;
}

/**
 * Implements hook_translated_menu_link_alter().
 *
 * Here is where we make changes to links that need dynamic information such
 * as the current page path or the number of users.
 */
function admin_menu_translated_menu_link_alter(&$item, $map) {
  global $user, $base_url;
  static $access_all;
  if ($item['menu_name'] != 'admin_menu') {
    return;
  }

  // Check whether additional development output is enabled.
  if (!isset($access_all)) {
    $access_all = variable_get('admin_menu_show_all', 0) && module_exists('devel');
  }

  // Prepare links that would not be displayed normally.
  if ($access_all && !$item['access']) {
    $item['access'] = TRUE;

    // Prepare for http://drupal.org/node/266596
    if (!isset($item['localized_options'])) {
      _menu_item_localize($item, $map, TRUE);
    }
  }

  // Don't waste cycles altering items that are not visible
  if (!$item['access']) {
    return;
  }

  // Add developer information to all links, if enabled.
  if ($extra = variable_get('admin_menu_display', 0)) {
    $item['title'] .= ' ' . $extra[0] . ': ' . $item[$extra];
  }
}

/**
 * Implements hook_flush_caches().
 *
 * Flush client-side caches.
 */
function admin_menu_flush_caches() {

  // Flush cached output of admin_menu.
  cache_clear_all('admin_menu:', 'cache_menu', TRUE);

  // Flush client-side cache hashes.
  // db_table_exists() required for SimpleTest.
  if (db_table_exists('cache_admin_menu')) {
    cache_clear_all('*', 'cache_admin_menu', TRUE);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Form submit handler to flush client-side cache hashes when clean URLs are toggled.
 */
function admin_menu_form_system_clean_url_settings_alter(&$form, $form_state) {
  $form['#submit'][] = 'admin_menu_flush_caches';
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Extends Devel module with Administration menu developer settings.
 */
function admin_menu_form_devel_admin_settings_alter(&$form, $form_state) {
  module_load_include('inc', 'admin_menu');
  _admin_menu_form_devel_admin_settings_alter($form, $form_state);
}

Functions

Namesort descending Description
admin_menu_admin_menu_output_build Implements hook_admin_menu_output_build().
admin_menu_admin_menu_replacements Implements hook_admin_menu_replacements().
admin_menu_cache_get Retrieve a client-side cache hash from cache.
admin_menu_cache_set Store a client-side cache hash in persistent cache.
admin_menu_element_sort Function used by uasort to sort structured arrays by #weight AND #title.
admin_menu_flush_caches Implements hook_flush_caches().
admin_menu_footer Implements hook_footer().
admin_menu_form_devel_admin_settings_alter Implements hook_form_FORM_ID_alter().
admin_menu_form_system_clean_url_settings_alter Implements hook_form_FORM_ID_alter().
admin_menu_get_user_count Return count of online anonymous/authenticated users.
admin_menu_help Implements hook_help().
admin_menu_init Implements hook_init().
admin_menu_js Implements hook_js().
admin_menu_js_cache Menu callback; Output administration menu for HTTP caching via AJAX request.
admin_menu_menu Implements hook_menu().
admin_menu_menu_alter Implements hook_menu_alter().
admin_menu_menu_link_alter Implements hook_menu_link_alter().
admin_menu_output Build the administration menu output.
admin_menu_perm Implements hook_perm().
admin_menu_suppress Suppress display of administration menu.
admin_menu_theme Implements hook_theme().
admin_menu_translated_menu_link_alter Implements hook_translated_menu_link_alter().
theme_admin_menu_links Render a themed list of links.