You are here

function select_registration_roles_form_submit in Select registration roles 8

Same name and namespace in other branches
  1. 7 select_registration_roles.module \select_registration_roles_form_submit()

Implements hook_form_submit().

1 string reference to 'select_registration_roles_form_submit'
select_registration_roles_form_alter in ./select_registration_roles.module
Implements hook_form_alter().

File

./select_registration_roles.module, line 62

Code

function select_registration_roles_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $name = $form_state
    ->getValues()['name'];
  $uid = getUid($name);
  $select_roles = $form_state
    ->getValue('select_roles');
  $config = \Drupal::config('select_registration_roles.settings');
  $display_roles = $config
    ->get('select_registration_roles_setby_admin');
  $approval_roles = $config
    ->get('select_registration_roles_admin_approval');
  $user = \Drupal\user\Entity\User::load($uid);
  foreach ($display_roles as $role_id => $role_name) {
    if ($role_name !== 0) {
      if ($role_id === $select_roles) {

        //if approval
        if ($approval_roles[$role_id] === $display_roles[$role_id]) {
          $user
            ->block();

          //If not.
        }
        else {
          $user
            ->addRole($role_id);
          $user
            ->activate();
        }
      }
      $user
        ->save();
    }
  }
}