From 6269b00ecd37c32dcd736193a683046de4f43b0e Mon Sep 17 00:00:00 2001 From: James Wilson Date: Sat, 11 Feb 2012 09:34:39 -0500 Subject: [PATCH] Issue #1157564 by konordo, jwilson3: Create menu position rules based on user role. --- menu_position.module | 3 + plugins/menu_position.user_roles.inc | 121 ++++++++++++++++++++++++++++++++++ plugins/menu_position.user_roles.js | 21 ++++++ 3 files changed, 145 insertions(+), 0 deletions(-) create mode 100644 plugins/menu_position.user_roles.inc create mode 100644 plugins/menu_position.user_roles.js diff --git a/menu_position.module b/menu_position.module index 7d186e4..694f964 100644 --- a/menu_position.module +++ b/menu_position.module @@ -161,6 +161,9 @@ function menu_position_menu_position_rule_plugins() { 'pages' => array( 'file' => 'plugins/menu_position.pages.inc', ), + 'user_role' => array( + 'file' => 'plugins/menu_position.user_roles.inc', + ), ); if (module_exists('locale')) { $plugins['language'] = array( diff --git a/plugins/menu_position.user_roles.inc b/plugins/menu_position.user_roles.inc new file mode 100644 index 0000000..458efc2 --- /dev/null +++ b/plugins/menu_position.user_roles.inc @@ -0,0 +1,121 @@ +roles as $role_id => $role_name) { + if (array_key_exists($role_id, $user_role)) { + $flag = TRUE; + } + } + return $flag; +} + +/** + * Adds form elements for the user_role plugin to the rule configuration form. + * + * If this condition was active in the current rule, the plug-in variables will + * be available in $form_state['#menu-position-rule']['conditions']['user_role']. + * + * It is the resposibility of this hook to add any necessary form validation and + * submission handlers. + * + * @param $form + * A reference to the "add/edit rule" form array. New form elements should be + * added directly to this array. + * @param $form_state + * A reference to the current form state. + */ +function menu_position_menu_position_rule_user_role_form(&$form, &$form_state) { + $roles = user_roles(TRUE); + // If this is an existing rule, load the variables stored in the rule for this plugin. + $variables = !empty($form_state['#menu-position-rule']['conditions']['user_role']) ? $form_state['#menu-position-rule']['conditions']['user_role'] : array(); + + // To ensure that the plugin's form elements are placed inside vertical tabs, + // all elements should be placed inside a collapsed fielset inside the + // $form['conditions'] array. + $form['conditions']['user_role'] = array( + '#type' => 'fieldset', + '#title' => t('User Role'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#attached' => array( + // Ensures a proper summary is added to its vertical tab. + 'js' => array(drupal_get_path('module', 'menu_position') . '/plugins/menu_position.user_roles.js'), + ), + ); + $form['conditions']['user_role']['user_role'] = array( + '#type' => 'checkboxes', + '#title' => t('Roles'), + '#default_value' => !empty($variables['user_role']) ? $variables['user_role'] : array(), + '#options' => $roles, + '#description' => t('Apply rule only to users with this role(s).'), + '#weight' => -20, + ); + + // If we have a validation handler, we can add it this way. Or we could add + // a per-element validation handler with '#element_validate' above. + //$form['#validate'][] = 'menu_position_menu_position_rule_user_role_form_validate'; + + // Add a submit handler. + $form['#submit'][] = 'menu_position_menu_position_rule_user_role_form_submit'; +} + +/** + * Prepares the plugin's variables to be stored in the rule. + * + * If the plugin's form elements indicate that the condition needs to be + * included with the rule, the submit handler must, at the very least, set: + * $form_state['conditions']['user_role'] = array(). Optionally, the plugin can add + * to this array any static variables to be stored in the database with the rule + * configuration. + * + * If, after this submit handler is run, the $form_state['conditions']['user_role'] + * variables array is not set, this plugin will not be added as a condition for + * this rule. + * + * @param $form + * A reference to the "add/edit rule" form array. + * @param $form_state + * A reference to the current form state, including submitted values. + */ +function menu_position_menu_position_rule_user_role_form_submit(&$form, &$form_state) { + // The user has added our plugin's form elements as a condition for the rule. + if (!empty($form_state['values']['user_role'])) { + // Remove any 0 valued options. + foreach ($form_state['values']['user_role'] as $key => $value) { + if ($value === 0) { + unset($form_state['values']['user_role'][$key]); + } + } + // Determine if any checkboxes were on. + if (!empty($form_state['values']['user_role'])) { + // Add this plugin's variables to the rule. + $variables = array( + 'user_role' => $form_state['values']['user_role'], + ); + $form_state['values']['conditions']['user_role'] = $variables; + } + } +} diff --git a/plugins/menu_position.user_roles.js b/plugins/menu_position.user_roles.js new file mode 100644 index 0000000..e2710b9 --- /dev/null +++ b/plugins/menu_position.user_roles.js @@ -0,0 +1,21 @@ +(function ($) { + +/** + * Provide the summary information for the user_role plugin's vertical tab. + */ +Drupal.behaviors.menuPositionUserRoleSettingsSummary = { + attach: function (context) { + $('fieldset#menu-position-rule', context).drupalSetSummary(function (context) { + var vals = []; + $('input[type="checkbox"]:checked', context).each(function () { + vals.push($.trim($(this).next('label').text())); + }); + if (!vals.length) { + vals.push(Drupal.t('Any Role')); + } + return vals.join(', '); + }); + } +}; + +})(jQuery); -- 1.7.5.4