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

Form to re-order roles.

See also

theme_user_admin_roles()

Related topics

File

core/modules/user/user.admin.inc, line 545
Admin page callbacks for the User module.

Code

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

  $form['roles_help'] = array(
    '#type' => 'help',
    '#markup' => t('Roles provide a way to organize people into groups that have similar duties. Each user account can be given one or more roles, and <a href="@permissions">permissions</a> can be granted to those roles. To make the <a href="@permissions">permissions</a> page easier to navigate, it is recommended that roles be ordered from the least permissive (Anonymous) to the most permissive (usually Administrator).', array('@permissions' => url('admin/config/people/permissions'))),
  );

  // Default administrator role and content editor role options.
  $form['default_roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default roles'),
    '#description' => t('Changing these settings will not affect existing permissions.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => -20,
  );
  $roles = user_roles();
  // Do not allow the anonymous or authenticated roles to be set as the default
  // administrator or content editor roles.
  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 or
  // content editor is deleted).
  $roles = array(0 => t('- None -')) + $roles;
  $form['default_roles']['user_admin_role'] = array(
    '#type' => 'select',
    '#title' => t('Default administrator role'),
    '#default_value' => $system_config->get('user_admin_role') ? $system_config->get('user_admin_role') : 0,
    '#options' => $roles,
    '#description' => t('This role will be automatically given any permission introduced by newly enabled modules.'),
  );
  $form['default_roles']['user_editor_role'] = array(
    '#type' => 'select',
    '#title' => t('Default content editor role'),
    '#default_value' => $system_config->get('user_editor_role') ? $system_config->get('user_editor_role') : 0,
    '#options' => $roles,
    '#description' => t('This role will be automatically given permission to create, edit, and delete content for any newly created content types.'),
  );

  // 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 (module_exists('config') && 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('user_editor_role'),
      array('anonymous'),
    ),
    '#submit' => array('user_admin_roles_order_submit'),
  );

  return $form;
}