You are here

function admin_menu_page_build in Administration menu 7.3

Implements hook_page_build().

File

./admin_menu.module, line 241
Render an administrative menu as a dropdown menu at the top of the window.

Code

function admin_menu_page_build(&$page) {

  // Performance. Skip entirely if this is an Ajax request or if user does
  // not have permission to view administration menus.
  if (arg(0) === 'js' || !user_access('access administration menu') || admin_menu_suppress(FALSE)) {
    return;
  }
  global $user;
  $path = drupal_get_path('module', 'admin_menu');
  $page['page_bottom']['admin_menu'] = array(
    '#attached' => array(),
  );
  $attached =& $page['page_bottom']['admin_menu']['#attached'];
  $options = array(
    'every_page' => TRUE,
  );
  $attached['css'][$path . '/admin_menu.css'] = $options;
  if ($user->uid == 1) {
    $attached['css'][$path . '/admin_menu.uid1.css'] = $options;
  }

  // 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().
  $attached['js'][$path . '/admin_menu.js'] = $options;

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

  // Determine whether we need to show all components and disable all caches.
  $complete = FALSE;
  if (current_path() == 'admin/config/administration/admin-menu' && $_SERVER['REQUEST_METHOD'] == 'GET') {
    $complete = TRUE;
  }

  // Retrieve the current hash.
  $hash = admin_menu_get_hash();

  // Determine various states. Should not use cached data when
  // $has_switch_user_access is true. This is to avoid failure of switch_user
  // functionality as drupal_valid_token causes issue with cached data.
  $has_switch_user_access = module_exists('devel') && user_access('switch users');
  $cache_client_enabled = !$complete && !$has_switch_user_access && variable_get('admin_menu_cache_client', TRUE);
  $cache_server_enabled = !$complete && !$has_switch_user_access && variable_get('admin_menu_cache_server', TRUE);

  // If the client supports JavaScript and client side caching is enabled,
  // just generate the unique callback URL and a possible CSRF token.
  if ($cache_client_enabled && !empty($_COOKIE['has_js'])) {

    // Provide a URL for the client side JS to render admin_menu.
    $settings['url'] = url("js/admin_menu/cache/{$hash}");

    // If the user is authenticated, generate a CSRF token.
    // Because a user may not have the JS module installed, admin_menu must
    // generate it's own.
    if ($user->uid) {
      $settings['token'] = drupal_get_token("admin_menu:{$hash}");
    }
  }
  else {
    if ($cache_server_enabled && ($cache = cache_get("admin_menu:{$hash}", 'cache_menu')) && isset($cache->data)) {
      $content = $cache->data;
    }
    else {
      $content = admin_menu_output($complete);
      if ($cache_server_enabled) {
        cache_set("admin_menu:{$hash}", $content, 'cache_menu');
      }
    }
    $page['page_bottom']['admin_menu']['#markup'] = $content;
  }
  $replacements = module_invoke_all('admin_menu_replacements', $complete);
  if (!empty($replacements)) {
    $settings['replacements'] = $replacements;
  }
  if ($setting = variable_get('admin_menu_margin_top', 1)) {
    $settings['margin_top'] = $setting;

    // @todo Drupal.behaviors.adminMenuMarginTop is obsolete, but
    //   hook_page_build() does not allow to set a CSS class on the body yet.
    // @see http://drupal.org/node/1473548, http://drupal.org/node/1194528
    // $page['#attributes']['class'][] = 'admin-menu';
  }
  if ($setting = variable_get('admin_menu_position_fixed', 1)) {
    $settings['position_fixed'] = $setting;

    // In fixed positioning, supply a callback function for tableheader.js to
    // allow it to determine the top viewport offset.
    // @see admin_menu.js, toolbar.js
    $attached['js'][] = array(
      'data' => array(
        'tableHeaderOffset' => 'Drupal.admin.height',
      ),
      'type' => 'setting',
    );
  }
  if ($setting = variable_get('admin_menu_tweak_tabs', 0)) {
    if (variable_get('admin_menu_tweak_tabs_exclude_admin', 0)) {
      if (!path_is_admin(current_path())) {
        $settings['tweak_tabs'] = $setting;
      }
    }
    else {
      $settings['tweak_tabs'] = $setting;
    }
  }
  if ($_GET['q'] == 'admin/modules' || strpos($_GET['q'], 'admin/modules/list') === 0) {
    $settings['tweak_modules'] = variable_get('admin_menu_tweak_modules', 0);
  }
  if (strpos($_GET['q'], 'admin/people/permissions') === 0) {
    $settings['tweak_permissions'] = variable_get('admin_menu_tweak_permissions', 0);
  }
  if ($setting = variable_get('admin_menu_font_size', 0)) {
    $settings['font_size'] = $setting;
  }
  $attached['js'][] = array(
    'data' => array(
      'admin_menu' => $settings,
    ),
    'type' => 'setting',
  );
}