function uc_role_renew in Ubercart 8.4
Renews a given role on a user.
This function updates expiration time on a role already granted to a user. First the function checks the new expiration. If it never expires, the function deletes the past expiration record and returns, leaving management up to Drupal. Otherwise, the record is updated with the new expiration time, and the user is notified of the change.
Parameters
\Drupal\user\UserInterface $account: A Drupal user object.
string $rid: A Drupal role ID.
int $timestamp: When this role will expire.
bool $silent: When set to TRUE will suppress any Drupal messages from this function.
2 calls to uc_role_renew()
- RenewRole::doExecute in uc_role/
src/ Plugin/ RulesAction/ RenewRole.php - Renews an order's product roles.
- uc_role_user_presave in uc_role/
uc_role.module - Implements hook_user_presave().
File
- uc_role/
uc_role.module, line 639 - Grants roles upon accepted payment of products.
Code
function uc_role_renew(UserInterface $account, $rid, $timestamp, $silent = FALSE) {
// If it doesn't expire, we'll remove our data associated with it.
// After that, Drupal will take care of it.
if (is_null($timestamp)) {
uc_role_delete($account, $rid);
return;
}
// Update the expiration date and reset the notified flag.
$connection = \Drupal::database();
$connection
->update('uc_roles_expirations')
->fields([
'expiration' => $timestamp,
'notified' => NULL,
])
->condition('uid', $account
->id())
->condition('rid', $rid)
->execute();
if (!$silent) {
$role_name = $connection
->query('SELECT name FROM {role} WHERE rid = :rid', [
':rid' => $rid,
])
->fetchField();
if (\Drupal::currentUser()
->id() == $account
->id()) {
$message = t('Your %role role has been renewed. It will expire on %date.', [
'%role' => $role_name,
'%date' => \Drupal::service('date.formatter')
->format($timestamp, 'short'),
]);
}
else {
$username = [
'#theme' => 'username',
'#account' => $account,
];
$message = t("@user's %role role has been renewed. It will expire on %date.", [
'@user' => drupal_render($username),
'%role' => $role_name,
'%date' => \Drupal::service('date.formatter')
->format($timestamp, 'short'),
]);
}
\Drupal::messenger()
->addMessage($message);
}
}