You are here

function role_expire_user_form_submit in Role Expire 2.x

Same name and namespace in other branches
  1. 8 role_expire.module \role_expire_user_form_submit()

Form submit handler invoked by user_register_form and user_form alter hooks.

TODO: This method needs debugging.

On D7 version, this code was inside hook_user_update. Updates default duration in database.

1 string reference to 'role_expire_user_form_submit'
role_expire_form_user_form_alter in ./role_expire.module
Implements hook_form_FORM_ID_alter().

File

./role_expire.module, line 154
Role Expire module.

Code

function role_expire_user_form_submit($form, FormStateInterface &$form_state) {
  $values = $form_state
    ->getValues();

  // Only rely on Role Delegation data if the user hasn't access to the normal roles field.
  if (!\Drupal::currentUser()
    ->hasPermission('administer permissions')) {

    // If Role Delegation module is used.
    if (isset($values['role_change'])) {
      $values['roles'] = [];
      foreach ($values['role_change'] as $rid) {
        $values['roles'] = $rid;
      }
    }
  }
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $original_roles = $form_state
    ->get('original_roles');
  if (\Drupal::currentUser()
    ->hasPermission('edit users role expire') || \Drupal::currentUser()
    ->hasPermission('administer users')) {

    // Add roles expiry information for the user role.
    foreach ($values as $key => $value) {
      if (strpos($key, 'role_expire_') === 0) {
        $rid = substr($key, strlen('role_expire_'));
        if ($value != '' && in_array($rid, $values['roles'])) {
          $expiry_timestamp = strtotime($value);
          \Drupal::service('role_expire.api')
            ->writeRecord($account
            ->id(), $rid, $expiry_timestamp);
        }
        else {
          $roleExpirationCanBeDeleted = \Drupal::service('role_expire.api')
            ->roleExpirationCanBeDeletedOnUserEditSave($rid);
          if ($roleExpirationCanBeDeleted) {
            \Drupal::service('role_expire.api')
              ->deleteRecord($account
              ->id(), $rid);
          }
        }
      }
    }
    if (isset($values['roles'])) {

      // Add default expiration to any new roles that have been given to the user.
      $new_roles = array_diff($values['roles'], $original_roles);
      if (isset($new_roles)) {

        // We have the new roles, loop over them and see whether we need to assign expiry to them.
        foreach ($new_roles as $role_id) {
          \Drupal::service('role_expire.api')
            ->processDefaultRoleDurationForUser($role_id, $account
            ->id());
        }
      }

      // Remove expiration for roles that have been removed from the user.
      $del_roles = array_diff($original_roles, $values['roles']);
      if (isset($del_roles)) {

        // We have the deleted roles, loop over them and remove their expiry info.
        foreach ($del_roles as $role_id) {
          $roleExpirationCanBeDeleted = \Drupal::service('role_expire.api')
            ->roleExpirationCanBeDeletedOnUserEditSave($role_id);
          if ($roleExpirationCanBeDeleted) {
            \Drupal::service('role_expire.api')
              ->deleteRecord($account
              ->id(), $role_id);
          }
        }
      }
    }

    // if values[roles]
  }

  // if permissions
}