You are here

admin_menu_source.module in Administration Menu Source 7

Use a different menu as the Administration Menu.

File

admin_menu_source.module
View source
<?php

/**
 * @file
 * Use a different menu as the Administration Menu.
 */

/**
 * Implements hook_help().
 */
function admin_menu_source_help($path, $arg) {
  switch ($path) {
    case 'admin/config/administration/admin_menu/source':
      return '<p>' . t('Set the source menu for the Administration menu per role.') . '</p>';
  }
}

/**
 * Implements hook_menu().
 */
function admin_menu_source_menu() {
  $items = array();
  $items['admin/config/administration/admin_menu/settings'] = array(
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/administration/admin_menu/source'] = array(
    'title' => 'Source',
    'description' => 'Configure the source for the administration menu.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'admin_menu_source_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'admin_menu_source.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function admin_menu_source_theme() {
  return array(
    'admin_menu_source_settings_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_admin_menu_output_alter().
 */
function admin_menu_source_admin_menu_output_alter(&$content) {
  global $user;

  // $rid = key(array_reverse($user->roles, TRUE));
  // Find the user role rid.
  $roles_ids = array_keys(user_roles(TRUE, 'access administration menu'));
  $user_roles_ids = array_keys($user->roles);
  $user_roles = array_intersect($roles_ids, $user_roles_ids);
  if (count($user_roles)) {
    $settings = _admin_menu_source_get_settings();
    $source_menu = '';
    foreach ($settings as $source) {
      if (in_array($source['rid'], $user_roles, TRUE)) {
        $source_menu = $source['source'];
      }
    }
    if (!empty($source_menu)) {

      // Get the menu tree.
      $tree = admin_menu_tree($source_menu);

      // Fix for special_menu_items so multiple <nolink> are supported.
      // Replace <nolink> with <nolink-*>.
      $count = 0;
      foreach ($tree as &$data) {
        if ($data['link']['href'] == '<nolink>') {
          $data['link']['href'] = '<nolink-' . $count . '>';
          $count++;
        }
      }

      // Build the menu as renderable menu links.
      $menu = admin_menu_links_menu($tree);

      // Replace <nolink-*> back with <nolink>.
      foreach ($menu as &$data) {
        preg_match("/^<nolink.*>/", $data['#href'], $matches);
        if (count($matches)) {
          $data['#href'] = '<nolink>';
        }
      }

      // Replace with new $menu.
      $content['menu'] = $menu;
      $content['menu']['#theme'] = 'admin_menu_links';
      $content['menu']['#weight'] = 0;
      $content['menu']['#wrapper_attributes']['id'] = 'admin-menu-menu';
    }
  }
}

/**
 * Returns HTML for a settings form.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 *
 * @ingroup themeable
 */
function theme_admin_menu_source_settings_form($variables) {
  $form = $variables['form'];
  $output = drupal_render($form['admin_menu_source_description']);
  $rows = array();
  if (!empty($form['admin_menu_source_settings'])) {
    foreach (element_children($form['admin_menu_source_settings']) as $key) {

      // Identifier for drupal_add_tabledrag call
      $form['admin_menu_source_settings'][$key]['weight']['#attributes']['class'] = array(
        'role-weight',
      );
      $rows[] = array(
        'data' => array(
          drupal_render($form['admin_menu_source_settings'][$key]['role']),
          drupal_render($form['admin_menu_source_settings'][$key]['source']),
          drupal_render($form['admin_menu_source_settings'][$key]['weight']),
        ),
        'class' => array(
          'draggable',
        ),
      );
    }
  }
  $headers = array(
    t('Role'),
    t('Menu'),
    t('Weight'),
  );
  $table_id = drupal_html_id('admin-menu-source-settings');
  $output .= theme('table', array(
    'header' => $headers,
    'rows' => $rows,
    'attributes' => array(
      'id' => $table_id,
    ),
  ));
  $output .= drupal_render($form['submit']);
  $output .= drupal_render_children($form);

  // The javascript magic that makes dragging work!
  drupal_add_tabledrag($table_id, 'order', 'sibling', 'role-weight');
  return $output;
}

/**
 * Helper function to get settings for admin_menu_source.
 * It assumes the settings are already sorted
 * It appends new roles to the bottom (lowest priority)
 */
function _admin_menu_source_get_settings() {
  $settings = variable_get('admin_menu_source_settings', array());

  // make sure all roles are in
  $roles_ids = array_keys(user_roles(TRUE, 'access administration menu'));
  foreach ($roles_ids as $rid) {
    if (empty($settings[$rid])) {
      $settings[$rid] = array(
        'rid' => $rid,
        'source' => '',
        'weight' => 10000,
      );
    }
  }
  return $settings;
}

Functions

Namesort descending Description
admin_menu_source_admin_menu_output_alter Implements hook_admin_menu_output_alter().
admin_menu_source_help Implements hook_help().
admin_menu_source_menu Implements hook_menu().
admin_menu_source_theme Implements hook_theme().
theme_admin_menu_source_settings_form Returns HTML for a settings form.
_admin_menu_source_get_settings Helper function to get settings for admin_menu_source. It assumes the settings are already sorted It appends new roles to the bottom (lowest priority)