admin_menu_source.module 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. $source_menu = _admin_menu_source_get_role_menu($rid);
  52. if (!empty($source_menu)) {
  53. $content['menu'] = admin_menu_links_menu(admin_menu_tree($source_menu));
  54. $content['menu']['#theme'] = 'admin_menu_links';
  55. $content['menu']['#weight'] = 0;
  56. $content['menu']['#sorted'] = TRUE;
  57. }
  58. }
  59. /**
  60. * Returns HTML for a settings form.
  61. *
  62. * @param $variables
  63. * An associative array containing:
  64. * - form: A render element representing the form.
  65. *
  66. * @ingroup themeable
  67. */
  68. function theme_admin_menu_source_settings_form($variables) {
  69. $output = '';
  70. $form = $variables['form'];
  71. $rows = array();
  72. if (!empty($form['admin_menu_source_settings'])) {
  73. foreach (element_children($form['admin_menu_source_settings']) as $key) {
  74. $rows[] = array(
  75. drupal_render($form['admin_menu_source_settings'][$key]['role']),
  76. drupal_render($form['admin_menu_source_settings'][$key]['source']),
  77. );
  78. }
  79. }
  80. $output .= theme('table', array('header' => array(t('Role'), t('Menu')), 'rows' => $rows));
  81. $output .= drupal_render($form['submit']);
  82. $output .= drupal_render_children($form);
  83. return $output;
  84. }
  85. /**
  86. * Helper function to get settings for admin_menu_source
  87. */
  88. function _admin_menu_source_get_settings() {
  89. return variable_get('admin_menu_source_settings', array());
  90. }
  91. /**
  92. * Helper function to get source menu per role
  93. *
  94. * @param $rid
  95. * the user role id
  96. */
  97. function _admin_menu_source_get_role_menu($rid) {
  98. //load the settings
  99. $settings = _admin_menu_source_get_settings();
  100. return isset($settings[$rid]['source']) ? $settings[$rid]['source'] : '';
  101. }