admin_menu_source.module 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * Use a different menu as the Administration Menu.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function admin_menu_source_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/config/administration/admin_menu/source':
  12. return '<p>' . t('Set the source menu for the Administration menu per role.') . '</p>';
  13. }
  14. }
  15. /**
  16. * Implements hook_menu().
  17. */
  18. function admin_menu_source_menu() {
  19. $items = array();
  20. $items['admin/config/administration/admin_menu/settings'] = array(
  21. 'title' => 'Settings',
  22. 'type' => MENU_DEFAULT_LOCAL_TASK,
  23. );
  24. $items['admin/config/administration/admin_menu/source'] = array(
  25. 'title' => 'Source',
  26. 'description' => 'Configure the source for the administration menu.',
  27. 'page callback' => 'drupal_get_form',
  28. 'page arguments' => array('admin_menu_source_settings'),
  29. 'access arguments' => array('administer site configuration'),
  30. 'type' => MENU_LOCAL_TASK,
  31. 'file' => 'admin_menu_source.inc',
  32. );
  33. return $items;
  34. }
  35. /**
  36. * Implements hook_theme().
  37. */
  38. function admin_menu_source_theme() {
  39. return array(
  40. 'admin_menu_source_settings_form' => array(
  41. 'render element' => 'form',
  42. ),
  43. );
  44. }
  45. /**
  46. * Implements hook_admin_menu_output_alter().
  47. */
  48. function admin_menu_source_admin_menu_output_alter(&$content) {
  49. global $user;
  50. // $rid = key(array_reverse($user->roles, TRUE));
  51. // Find the user role rid.
  52. $roles_ids = array_keys(user_roles(TRUE, 'access administration menu'));
  53. $user_roles_ids = array_keys($user->roles);
  54. $user_roles = array_reverse(array_intersect($roles_ids, $user_roles_ids));
  55. if (count($user_roles)) {
  56. $rid = $user_roles[0];
  57. $source_menu = _admin_menu_source_get_role_menu($rid);
  58. if (!empty($source_menu)) {
  59. $content['menu'] = admin_menu_links_menu(admin_menu_tree($source_menu));
  60. $content['menu']['#theme'] = 'admin_menu_links';
  61. $content['menu']['#weight'] = 0;
  62. $content['menu']['#sorted'] = TRUE;
  63. }
  64. }
  65. }
  66. /**
  67. * Returns HTML for a settings form.
  68. *
  69. * @param $variables
  70. * An associative array containing:
  71. * - form: A render element representing the form.
  72. *
  73. * @ingroup themeable
  74. */
  75. function theme_admin_menu_source_settings_form($variables) {
  76. $output = '';
  77. $form = $variables['form'];
  78. $rows = array();
  79. if (!empty($form['admin_menu_source_settings'])) {
  80. foreach (element_children($form['admin_menu_source_settings']) as $key) {
  81. $rows[] = array(
  82. drupal_render($form['admin_menu_source_settings'][$key]['role']),
  83. drupal_render($form['admin_menu_source_settings'][$key]['source']),
  84. );
  85. }
  86. }
  87. $output .= theme('table', array('header' => array(t('Role'), t('Menu')), 'rows' => $rows));
  88. $output .= drupal_render($form['submit']);
  89. $output .= drupal_render_children($form);
  90. return $output;
  91. }
  92. /**
  93. * Helper function to get settings for admin_menu_source.
  94. */
  95. function _admin_menu_source_get_settings() {
  96. return variable_get('admin_menu_source_settings', array());
  97. }
  98. /**
  99. * Helper function to get source menu per role.
  100. *
  101. * @param $rid
  102. * the user role id
  103. */
  104. function _admin_menu_source_get_role_menu($rid) {
  105. // Load the settings.
  106. $settings = _admin_menu_source_get_settings();
  107. return isset($settings[$rid]['source']) ? $settings[$rid]['source'] : '';
  108. }