You are here

function roleassign_form_alter in RoleAssign 8

Same name and namespace in other branches
  1. 5 roleassign.module \roleassign_form_alter()
  2. 6 roleassign.module \roleassign_form_alter()
  3. 7.2 roleassign.module \roleassign_form_alter()
  4. 7 roleassign.module \roleassign_form_alter()

Implements hook_form_alter().

File

./roleassign.module, line 81
Allows site administrators to delegate the task of managing user's roles.

Code

function roleassign_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (roleassign_restrict_access()) {

    // Add the checkboxes to the user register and user edit form.
    if ($form_id == 'user_register_form' || $form_id == 'user_form') {

      // Get all roles that are available.
      $roles = user_role_names(TRUE);

      // Get roles that are available for assignment.
      $assignable_roles = array_intersect_key($roles, array_filter(\Drupal::config('roleassign.settings')
        ->get('roleassign_roles')));

      // Get roles already assigned to the account.
      $account = $form_state
        ->getFormObject()
        ->getEntity();
      if (!empty($account
        ->id())) {
        $assigned_roles = $account
          ->getRoles();

        // Replace numeric indexes with rolenames.
        $assigned_roles = array_combine($assigned_roles, $assigned_roles);

        // An account might already have a role that isn't available
        // for assignment through this module, such a role is called "sticky".
        // Get sticky roles.
        $sticky_roles = array_diff_key($assigned_roles, $assignable_roles);
        $sticky_roles = array_intersect_key($roles, $sticky_roles);
      }

      // Add Authenticated user role sticky roles.
      $sticky_roles[RoleInterface::AUTHENTICATED_ID] = $roles[RoleInterface::AUTHENTICATED_ID];

      // Store sticky roles in form values.
      $form['sticky_roles'] = [
        '#type' => 'value',
        '#value' => $sticky_roles,
      ];

      // Build the assign roles checkboxes.
      $roles_field = [
        '#type' => 'checkboxes',
        '#title' => t('Assignable roles'),
        '#options' => $assignable_roles,
        '#default_value' => empty($assigned_roles) ? [] : array_keys($assigned_roles),
        '#description' => t('The user receives the combined permissions of all roles selected here and the following roles: %roles.', [
          '%roles' => implode(', ', $sticky_roles),
        ]),
      ];

      // The user form is sometimes within an 'account' fieldset.
      if (isset($form['account'])) {
        $user_form =& $form['account'];
      }
      else {
        $user_form =& $form;
      }

      // Add the assign roles checkboxes to the user form, and make sure
      // that the notify user checkbox comes last.
      if (isset($user_form['notify'])) {
        $notify_field = $user_form['notify'];
        unset($user_form['notify']);
        $user_form['roles'] = $roles_field;
        $user_form['notify'] = $notify_field;
      }
      else {
        $user_form['roles'] = $roles_field;
      }
    }
    elseif (\Drupal::moduleHandler()
      ->moduleExists('cas') && $form_id == 'bulk_add_cas_users') {

      // Get all roles that are available.
      $roles = user_role_names(TRUE);

      // Get roles that are available for assignment.
      $assignable_roles = array_intersect_key($roles, array_filter(\Drupal::config('roleassign.settings')
        ->get('roleassign_roles')));
      $form['roles']['#options'] = $assignable_roles;
    }
  }
}