You are here

function uc_role_revoke in Ubercart 8.4

Revokes a role on a given user.

This function deletes a given role from a user's list of roles, as well as removing any expiration data associated with the user/role. The function notifies the user of revocation.

Parameters

\Drupal\user\UserInterface $account: A Drupal user object.

string $rid: A Drupal role ID.

bool $silent: When set to TRUE will suppress any Drupal messages from this function.

1 call to uc_role_revoke()
uc_role_cron in uc_role/uc_role.module
Implements hook_cron().

File

uc_role/uc_role.module, line 523
Grants roles upon accepted payment of products.

Code

function uc_role_revoke(UserInterface &$account, $rid, $silent = FALSE) {

  // Remove this role from the user's list.
  $account
    ->removeRole($rid);
  $account
    ->save();

  // Remove our record of the expiration.
  uc_role_delete($account, $rid, $silent);
  $connection = \Drupal::database();
  $role_name = $connection
    ->query('SELECT name FROM {role} WHERE rid = :rid', [
    ':rid' => $rid,
  ])
    ->fetchField();
  if (!$silent) {
    if (\Drupal::currentUser()
      ->id() == $account
      ->id()) {
      \Drupal::messenger()
        ->addMessage(t('Your %role role has been revoked.', [
        '%role' => $role_name,
      ]));
    }
    else {
      $username = [
        '#theme' => 'username',
        '#account' => $account,
      ];
      \Drupal::messenger()
        ->addMessage(t('@user has had the %role role revoked.', [
        '@user' => drupal_render($username),
        '%role' => $role_name,
      ]));
    }
  }
}