1 user.admin.inc user_admin_roles($form, $form_state)

Form to re-order roles or add a new one.

See also

theme_user_admin_roles()

Related topics

File

core/modules/user/user.admin.inc, line 479
Admin page callback file for the user module.

Code

function user_admin_roles($form, $form_state) {
  $system_config = config('system.core');

  // Administrative role option.
  $form['admin_role'] = array(
    '#type' => 'fieldset',
    '#title' => t('Administrator role'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => -20,
  );
  // Do not allow the anonymous or authenticated user roles as administrator.
  $roles = user_roles();
  unset($roles[BACKDROP_ANONYMOUS_ROLE]);
  unset($roles[BACKDROP_AUTHENTICATED_ROLE]);
  $roles[0] = t('disabled');
  $form['admin_role']['user_admin_role'] = array(
    '#type' => 'select',
    '#title' => t('The role that will be automatically assigned new permissions whenever a module is enabled:'),
    '#default_value' => $system_config->get('user_admin_role'),
    '#options' => $roles,
    '#description' => t('Changing this setting will not affect existing permissions.'),
  );

  // Settings for anonymous users.
  $form['anonymous_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Anonymous visitors'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => -10,
  );
  $form['anonymous_settings']['anonymous'] = array(
    '#type' => 'textfield',
    '#title' => t('The name you would like used for anonymous visitors to your site:'),
    '#default_value' => $system_config->get('anonymous'),
    '#required' => TRUE,
  );

  $form['roles'] = array(
    '#tree' => TRUE,
  );
  $order = 0;
  $roles = user_roles(FALSE, NULL, TRUE);
  foreach ($roles as $role_name => $role) {
    $form['roles'][$role_name]['#role'] = $role;
    $form['roles'][$role_name]['#weight'] = $order;
    $form['roles'][$role_name]['weight'] = array(
      '#type' => 'textfield',
      '#title' => t('Weight for @title', array('@title' => check_plain($role->label))),
      '#title_display' => 'invisible',
      '#size' => 4,
      '#default_value' => $order,
      '#attributes' => array('class' => array('role-weight')),
    );
    $links = array();
    $links['configure'] = array(
      'title' => t('Configure role'),
      'href' => 'admin/config/people/roles/edit/' . $role_name,
      'weight' => 0,
    );
    $links['permissions'] = array(
      'title' => t('Set permissions'),
      'href' => 'admin/config/people/permissions/' . $role_name,
      'weight' => 5,
    );
    if (user_access('synchronize configuration')) {
      $links['export'] = array(
        'title' => t('Export role'),
        'href' => 'admin/config/development/configuration/single/export',
        'query' => array(
          'group' => 'User roles',
          'name' => 'user.role.' . $role_name,
        ),
      );
    }
    if ($role_name !== BACKDROP_ANONYMOUS_ROLE && $role_name !== BACKDROP_AUTHENTICATED_ROLE) {
      $links['delete'] = array(
        'title' => t('Delete role'),
        'href' => 'admin/config/people/roles/delete/' . $role_name,
        'weight' => 0,
      );
    }
    $form['roles'][$role_name]['operations'] = array(
      '#type' => 'operations',
      '#links' => $links,
    );
    $order++;
  }

  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Add role'),
    '#title_display' => 'invisible',
    '#required' => TRUE,
    '#size' => 32,
    '#maxlength' => 64,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#size' => 32,
    '#maxlength' => 64,
    '#machine_name' => array(
      'source' => array('label'),
      'exists' => 'user_role_load',
      'standalone' => TRUE,
    ),
    '#description' => t('A unique machine-readable name for this role. It must only contain lowercase letters, numbers, and underscores.'),
  );
  $form['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add role'),
    '#submit' => array('user_admin_roles_add_submit'),
  );
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save roles'),
    '#limit_validation_errors' => array(array('roles'), array('user_admin_role'), array('anonymous')),
    '#submit' => array('user_admin_roles_order_submit'),
  );

  return $form;
}