- Create a file named disable_users.drush.inc in the ~/.drush folder for your user:
<?php
/**
* @file
* Loads all users who have not logged in within 10 days and disables them.
*/
- Add the Drush command hook that will allow Drush to discover our commands provided by the file:
/**
* Implements hook_drush_command().
*/
function disable_users_drush_command() {
$items = [];
$items['disable-users'] = [
'description' => 'Disables users after 10 days of inactivity',
];
return $items;
}
- Next, we will create the command callback function, which will end up holding all of our logic. Since our filename is disable_users.drush.inc and our command is disable-users, the hook turns out to be drush_disable_users_disable_users:
/**
* Implements drush_hook_COMMAND().
*/
function drush_disable_users_disable_users() {
}
- Update the function to create a DateTime object, representing 10 days ago. We will use this to generate a timestamp for our query:
/**
* Implements drush_hook_COMMAND().
*/
function drush_disable_users_disable_users() {
// Get the default timezone and make a DateTime object for 10 days ago.
$system_date = \Drupal::config('system.date');
$default_timezone = $system_date->get('timezone.default') ?: date_default_timezone_get();
$now = new DateTime('now', new DateTimeZone($default_timezone));
$now->modify('-10 days');
}
- Now, we will add our query, which will query all the user entities who have a login timestamp greater than 10 days:
/**
* Implements drush_hook_COMMAND().
*/
function drush_disable_users_disable_users() {
// Get the default timezone and make a DateTime object for 10 days ago.
$system_date = \Drupal::config('system.date');
$default_timezone = $system_date->get('timezone.default') ?: date_default_timezone_get();
$now = new DateTime('now', new DateTimeZone($default_timezone));
$now->modify('-10 days');
$query = \Drupal::entityQuery('user')->condition('login', $now->getTimestamp(), '>');
$results = $query->execute();
if (empty($results)) {
drush_print('No users to disable!');
}
}
- Next, we will iterate over the results and mark the user as disabled:
/**
* Implements drush_hook_COMMAND().
*/
function drush_disable_users_disable_users() {
// Get the default timezone and make a DateTime object for 10 days ago.
$system_date = \Drupal::config('system.date');
$default_timezone = $system_date->get('timezone.default') ?: date_default_timezone_get();
$now = new DateTime('now', new DateTimeZone($default_timezone));
$now->modify('-10 days');
$query = \Drupal::entityQuery('user')->condition('login', $now->getTimestamp(), '>');
$results = $query->execute();
if (empty($results)) {
drush_print('No users to disable!');
}
foreach ($results as $uid) {
/** @var \Drupal\user\Entity\User $user */
$user = \Drupal\user\Entity\User::load($uid);
$user->block();
$user->save();
}
drush_print(dt('Disabled !count users', ['!count' => count($results)]));
}
- Drush's cache will need to be cleared in order to discover your new command:
$ drush cache-clear drush
- Check whether the command exists:
$ drush disable-users --help
Disables users after 10 days of inactivity