You are here

function autoassignrole_form_user_register_form_alter in Auto Assign Role 7.2

Same name and namespace in other branches
  1. 7 autoassignrole.module \autoassignrole_form_user_register_form_alter()

Implements hook_form_FORM_ID_alter() for user_register_form().

File

./autoassignrole.module, line 276
The main autoassignrole.module file

Code

function autoassignrole_form_user_register_form_alter(&$form, &$form_state) {
  if (variable_get("autoassignrole_user_active", 0) && !user_access('administer users') && !autoassignrole_get_active_path_roles()) {

    // Get a list of valid roles that can be selected.
    $roles = array_intersect_key(user_roles(TRUE), _autoassignrole_convert_roles(variable_get('autoassignrole_user_roles', array())));
    if ($roles) {
      $form['autoassignrole_user'] = array(
        '#type' => 'fieldset',
        '#title' => filter_xss(variable_get('autoassignrole_user_fieldset_title', t('User Roles'))),
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );

      // Sort the roles.
      if (variable_get('autoassignrole_user_sort', 'SORT_ASC') == 'SORT_ASC') {
        asort($roles);
      }
      elseif (variable_get('autoassignrole_user_sort', 'SORT_DESC') == 'SORT_DESC') {
        arsort($roles);
      }
      else {
        drupal_sort_weight($roles, $roles);
      }
      $roles = drupal_map_assoc($roles);
      $multiple = variable_get('autoassignrole_user_multiple', 0);
      $user_selection = variable_get('autoassignrole_user_selection', AUTOASSIGNROLE_ELEMENT_RADIO_CHECKBOX);
      $type = 'select';
      if ($user_selection == AUTOASSIGNROLE_ELEMENT_RADIO_CHECKBOX) {
        $type = $user_selection == AUTOASSIGNROLE_ELEMENT_RADIO_CHECKBOX && !$multiple ? 'radios' : 'checkboxes';
      }

      // If not multiple + not required + select box, need a none selection.
      if (!$multiple && !variable_get('autoassignrole_user_required', 0) && $type == 'select') {
        $roles = array(
          0 => t('None'),
        ) + $roles;
      }

      // Set the user description filter format.
      $autoassignrole_user_description = _autoassignrole_get_user_description();

      // Add in the element.
      $form['autoassignrole_user']['user_roles'] = array(
        '#type' => $type,
        '#title' => filter_xss(variable_get('autoassignrole_user_title', t('Role'))),
        '#options' => $roles,
        '#description' => filter_xss_admin($autoassignrole_user_description['value']),
        '#required' => variable_get('autoassignrole_user_required', 0),
        '#multiple' => $multiple,
      );
    }
  }
}