You are here

function _roleassign_form_alter in RoleAssign 7

Same name and namespace in other branches
  1. 7.2 roleassign.admin.inc \_roleassign_form_alter()

Really implements hook_form_alter().

Adds checkboxes for assignable roles to the user edit form.

_state

Parameters

array $form:

string $form_id:

1 call to _roleassign_form_alter()
roleassign_form_alter in ./roleassign.module
Implements hook_form_alter().

File

./roleassign.admin.inc, line 58
Allows site administrators to further delegate the task of managing user's roles.

Code

function _roleassign_form_alter(array &$form, array &$form_state, $form_id) {

  // Get all roles that are available.
  $roles = user_roles(TRUE);

  // Get roles that are available for assignment.
  $assignable_roles = _roleassign_assignable_roles($roles);

  // Get roles already assigned to the account.
  $account = NULL;
  if ($form_id == 'user_profile_form' && !empty($form_state['user'])) {
    $account = $form_state['user'];
  }
  $assigned_roles = isset($account->roles) ? $account->roles : array();

  // An account might already have a role that isn't available for assignment
  // through this module. Such a role is called "sticky".
  // Get sticky roles.
  $sticky_roles = array_diff($assigned_roles, $assignable_roles);
  $sticky_roles = array_intersect_key($roles, $sticky_roles);

  // Store sticky roles for later use in roleassign_user_presave().
  _roleassign_sticky_roles($sticky_roles);

  // Make a string of all sticky roles.
  $sticky_roles[DRUPAL_AUTHENTICATED_RID] = $roles[DRUPAL_AUTHENTICATED_RID];
  $sticky_roles_str = implode(', ', $sticky_roles);

  // Build the assign roles checkboxes.
  $roles_field = array(
    '#type' => 'checkboxes',
    '#title' => t('Assignable roles'),
    '#options' => $assignable_roles,
    '#default_value' => array_keys($assigned_roles),
    '#description' => t('The user receives the combined permissions of all roles selected here and the following roles: %roles.', array(
      '%roles' => $sticky_roles_str,
    )),
  );

  // The user form is sometimes within an 'account' fieldset.
  if (isset($form['account'])) {
    $user_form =& $form['account'];
  }
  else {
    $user_form =& $form;
  }

  // Add the assign roles checkboxes to the user form, and make sure
  // that the notify user checkbox comes last.
  if (isset($user_form['notify'])) {
    $notify_field = $user_form['notify'];
    unset($user_form['notify']);
    $user_form['roleassign_roles'] = $roles_field;
    $user_form['notify'] = $notify_field;
  }
  else {
    $user_form['roleassign_roles'] = $roles_field;
  }
  if (user_access('administer permissions', $account) && !user_access('administer permissions')) {
    drupal_set_message(t('Some of the fields on this form are locked for this user.'), 'warning', FALSE);
    $form['account']['name']['#disabled'] = TRUE;
    $form['account']['mail']['#disabled'] = TRUE;
    $form['account']['pass']['#disabled'] = TRUE;
  }
}