You are here

select_registration_roles.module in Select registration roles 8

Same filename and directory in other branches
  1. 7 select_registration_roles.module

File

select_registration_roles.module
View source
<?php

/**
 * Get all Roles.
 * @return array $all_roles
 */
function select_registration_roles_all_roles($membersonly = TRUE) {
  $roles = user_roles($membersonly);
  $all_roles = array();
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
  foreach ($roles as $role_id => $role) {
    $all_roles[$role_id] = $role
      ->label();
  }
  return $all_roles;
}

/**
 *  Implements hook_form_alter().
 */
function select_registration_roles_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == "user_register_form") {
    $roles = array();
    $config = \Drupal::configFactory()
      ->getEditable('select_registration_roles.settings');
    $display_roles = $config
      ->get('select_registration_roles_setby_admin');
    if (empty($display_roles)) {
      $form['select_roles'] = array(
        '#type' => 'checkboxes',
        '#title' => t('You must configure <a href = "!configure">role field on the registration form.</a>', array(
          '!configure' => \Drupal::url('select_registration_roles.roles_set_by_admin'),
        )),
        '#options' => $roles,
      );
    }
    else {
      $approval_roles = $config
        ->get('select_registration_roles_admin_approval');
      $display_roles = $config
        ->get('select_registration_roles_setby_admin');
      foreach ($display_roles as $role_id => $role_name) {
        if ($role_name !== 0) {
          if ($approval_roles[$role_id] === $display_roles[$role_id]) {
            $role_name .= t("<i>*needs administration approval</i>");
          }
          $roles[$role_id] = $role_name;
          $form['select_roles'] = array(
            '#type' => 'radios',
            '#title' => t('Choose a role'),
            '#options' => $roles,
            '#cache' => array(
              'max-age' => -1,
            ),
          );
        }
      }
    }
    $form['actions']['submit']['#submit'][] = 'select_registration_roles_form_submit';
  }
}

/**
 * Implements hook_form_submit().
 */
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();
    }
  }
}

/**
 * Implements hook_mail().
 */
function select_registration_roles_mail($key, &$message, $params) {
  switch ($key) {
    case 'registration':

      // note: data can be passed to this function in the $params array.
      $output = t("Name: @name", array(
        '@name' => $params['username'],
      ));
      $output .= t("has requested a role that need your confirmation.");
      $output .= t("Role(s) that need your approvals:");
      $message['subject'] = t('New Roles Request');
      $message['body'][] = $output . '.';
  }
}

/**
 * @param $name
 * @return mixed
 * Get uid of the usr.
 */
function getUid($name) {
  $select = db_select('users_field_data', 'u')
    ->fields('u', array())
    ->condition('u.name', $name)
    ->execute()
    ->fetchAll();
  return $select[0]->uid;
}