1.20.x user.admin.inc user_admin_roles($form, $form_state)

Form to re-order roles.

See also

theme_user_admin_roles()

Related topics

File

modules/user/user.admin.inc, line 499
Admin page callbacks 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,
  );
  $roles = user_roles();
  // Do not allow the anonymous or authenticated roles to be set as the
  // administrator.
  unset($roles[BACKDROP_ANONYMOUS_ROLE]);
  unset($roles[BACKDROP_AUTHENTICATED_ROLE]);
  // Add the "disabled" option to the top of the roles list (makes sure that
  // this is the selected option if the role that was previously set as admin is
  // deleted).
  array_unshift($roles, 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') ? $system_config->get('user_admin_role') : 0,
    '#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/configure/' . $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['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
    '#limit_validation_errors' => array(array('roles'), array('user_admin_role'), array('anonymous')),
    '#submit' => array('user_admin_roles_order_submit'),
  );

  return $form;
}