You are here

function subuser_form_alter in Subuser 8

Same name and namespace in other branches
  1. 7.2 subuser.module \subuser_form_alter()

Implements hook_form_alter().

File

./subuser.module, line 193
Provides primary Drupal hook implementations.

Code

function subuser_form_alter(&$form, &$form_state, $form_id) {
  $account = \Drupal::currentUser();
  subuser_load_all($account);
  switch ($form_id) {
    case 'user_account_form':
    case 'user_profile_form':
      if (!isset($form['account'])) {
        return;
      }
      $account = $form['#user'];

      // If the user does not have access to the roles field then filter the roles
      // field based on subuser permissions and display if more then one left. If
      // the user has 'administer permissions' then the #access will be set to TRUE
      // and they will have access to all roles, otherwise if the user has access
      // to this page through subuser then only provide them with the roles they
      // are allowed based on subuser. All users will have at least one role, but
      // that role may be 'authenticated user' which is not included in #options.
      if (!$form['account']['roles']['#access']) {
        $parents = subuser_load_all($account, FALSE);
        if (user_access('edit subusers', $user) && in_array($user->uid, $parents)) {
          foreach ($form['account']['roles']['#options'] as $rid => $role) {
            if (!user_access('create subuser ' . $rid)) {
              unset($form['account']['roles']['#options'][$rid]);
            }
          }
          $form['account']['roles']['#access'] = count($form['account']['roles']['#options']) > 0;
        }
      }

      // Remove the limit user setting from users that doesnt have the administer subusers permissions
      $form['field_subuser_limit']['#access'] = user_access('administer subusers', $user);
      break;
    case 'user_register_form':

      // Remove the limit user setting from users that doesnt have the administer subusers permissions
      $form['field_subuser_limit']['#access'] = user_access('administer subusers', $user);
      break;
  }
}