diff --git a/admin_menu_source.inc b/admin_menu_source.inc index dabd055..1a5ed09 100644 --- a/admin_menu_source.inc +++ b/admin_menu_source.inc @@ -5,9 +5,10 @@ */ function admin_menu_source_settings() { - $roles = user_roles(); + $roles = user_roles(TRUE, 'access administration menu'); $menus = menu_get_menus(); $default_values = _admin_menu_source_get_settings(); + $form['#theme'] = 'admin_menu_source_settings_form'; $form['admin_menu_source_description'] = array( @@ -20,9 +21,12 @@ function admin_menu_source_settings() { '#sorted' => FALSE, ); - foreach ($roles as $rid => $role) { + foreach ($default_values as $rid => $setting) { + if (empty($roles[$rid])) { + continue; + } $form['admin_menu_source_settings'][$rid]['role'] = array( - '#markup' => check_plain($role), + '#markup' => check_plain($roles[$rid]), ); $form['admin_menu_source_settings'][$rid]['rid'] = array( '#type' => 'hidden', @@ -31,25 +35,52 @@ function admin_menu_source_settings() { $form['admin_menu_source_settings'][$rid]['source'] = array( '#type' => 'select', '#options' => array('' => t('Default')) + $menus, - '#default_value' => isset($default_values[$rid]['source']) ? $default_values[$rid]['source'] : '', + '#default_value' => isset($setting['source']) ? $setting['source'] : '', ); $form['admin_menu_source_settings'][$rid]['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), - '#default_value' => isset($default_values[$rid]['weight']) ? $default_values[$rid]['weight'] : 0, - '#delta' => 10, + '#default_value' => isset($setting['weight']) ? $setting['weight'] : 0, '#title_display' => 'invisible', ); - $form['admin_menu_source_settings'][$rid]['#weight'] = $default_values[$rid]['weight']; + $form['admin_menu_source_settings'][$rid]['#weight'] = isset($setting['weight']) ? $setting['weight'] : 0; } + $form = system_settings_form($form); + // Add a custom submit handler. $form['#submit'][] = 'admin_menu_source_settings_submit'; - return system_settings_form($form); + return $form; } function admin_menu_source_settings_submit() { + $settings = _admin_menu_source_get_settings(); + + // Sort the settings based on their weight. + uasort($settings, '_admin_menu_source_sort_by_roles'); + + // normalize weights + $weight = 0; + foreach ($settings as $key => $setting) { + $settings[$key]['weight'] = $weight; + $weight++; + } + + // re-save + variable_set('admin_menu_source_settings', $settings); + // Flush admin_menu's cache. admin_menu_flush_caches(); } + +/** + * Implementation of a uasort compatible sorter function. + * Helper function to sort roles based on their weight + */ +function _admin_menu_source_sort_by_roles($a, $b) { + if ($a['weight'] == $b['weight']) { + return 0; + } + return ($a['weight'] < $b['weight']) ? -1 : 1; +} diff --git a/admin_menu_source.module b/admin_menu_source.module index 64cb3b8..16167ab 100644 --- a/admin_menu_source.module +++ b/admin_menu_source.module @@ -60,17 +60,14 @@ function admin_menu_source_admin_menu_output_alter(&$content) { // 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_reverse(array_intersect($roles_ids, $user_roles_ids)); + $user_roles = array_intersect($roles_ids, $user_roles_ids); if (count($user_roles)) { $settings = _admin_menu_source_get_settings(); - // Sort the settings based on their weight. - uasort($settings, "_admin_menu_source_sort_by_roles"); $source_menu = ''; - // Use the foreach ($settings as $source) { - if (array_search($source['rid'], $user_roles) !== FALSE) { + if (in_array($source['rid'], $user_roles, TRUE)) { $source_menu = $source['source']; } } @@ -85,17 +82,6 @@ function admin_menu_source_admin_menu_output_alter(&$content) { } /** - * Implementation of a uasort compatible sorter function. - * Helper function to sort roles based on their weight - */ -function _admin_menu_source_sort_by_roles($a, $b) { - if ($a['weight'] == $b['weight']) { - return 0; - } - return ($a['weight'] < $b['weight']) ? -1 : 1; -} - -/** * Returns HTML for a settings form. * * @param $variables @@ -144,7 +130,23 @@ function theme_admin_menu_source_settings_form($variables) { /** * 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() { - return variable_get('admin_menu_source_settings', array()); + $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, // new roles go to the bottom + ); + } + } + + return $settings; }