You are here

function single_user_role_form_alter in Single User Role 7

Same name and namespace in other branches
  1. 8 single_user_role.module \single_user_role_form_alter()

Implements hook_form_alter().

File

./single_user_role.module, line 14
Single User Role module file.

Code

function single_user_role_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_register_form' || $form_id == 'user_profile_form') {

    // Set user role field name.
    $role_field_name = 'roles';
    if (module_exists('role_delegation')) {

      // Based on role delegation module condition only change role field for
      // user without administer permissions.
      if (!user_access('administer permissions')) {
        $role_field_name = 'roles_change';
      }
    }

    // Change role field type from checkbox to select or radio field.
    $form['account'][$role_field_name]['#type'] = variable_get('single_user_role_field_type', 'select');
    $form['account'][$role_field_name]['#multiple'] = FALSE;

    // Set role field helptext.
    $form['account'][$role_field_name]['#description'] = variable_get('single_user_role_field_desc', '');

    // Set role field title singular.
    $form['account'][$role_field_name]['#title'] = t('Role');

    // Remove authenticated user checkbox.
    $form['account'][$role_field_name][2]['#access'] = FALSE;

    // Set default role.
    if (isset($form['account'][$role_field_name]['#default_value']) && is_array($form['account'][$role_field_name]['#default_value'])) {
      foreach ($form['account'][$role_field_name]['#default_value'] as $key => $value) {
        if ($value != 2) {

          // Set first assigned role as default.
          $form['account'][$role_field_name]['#default_value'] = $value;
          break;
        }
      }
    }
    $form['account'][$role_field_name][2]['#access'] = FALSE;

    // Make our validation first one to change structure of roles field back.
    array_unshift($form['#validate'], 'single_user_role_form_validate');
  }
}