- Create a new module that will hold your Drupal Console command, such as
console_commands:
drupal generate:module
// Welcome to the Drupal module generator
Enter the new module name:
> Console Commands
Enter the module machine name [console_commands]:
>
Enter the module Path [/modules/custom]:
>
Enter module description [My Awesome Module]:
>
Enter package name [Custom]:
>
Enter Drupal Core version [8.x]:
>
Do you want to generate a .module file (yes/no) [yes]:
>
Define module as feature (yes/no) [no]:
>
Do you want to add a composer.json file to your module (yes/no) [yes]:
>
Would you like to add module dependencies (yes/no) [no]:
>
Do you want to generate a unit test class (yes/no) [yes]:
>
Do you want to generate a themeable template (yes/no) [yes]:
>
Do you confirm generation? (yes/no) [yes]:
>
Generated or updated files
1 - modules/custom/console_commands/console_commands.info.yml
2 - modules/custom/console_commands/console_commands.module
3 - modules/custom/console_commands/composer.json
4 modules/custom/console_commands/tests/src/Functional/LoadTest.php
5 - modules/custom/console_commands/console_commands.module
6 - modules/custom/console_commands/templates/console-commands.html.twig
- Next, we will generate the command's base files using the generate:command command. Call it the disable_users command:
drupal generate:command
// Welcome to the Drupal Command generator
Enter the extension name [devel]:
> console_commands
Enter the Command name. [console_commands:default]:
> console_commands:disable_users
Enter the Command Class. (Must end with the word 'Command'). [DefaultCommand]:
> DisableUsersCommand
Is the command aware of the drupal site installation when executed?. (yes/no) [no]:
> yes
Do you confirm generation? (yes/no) [yes]:
>
Generated or updated files
1 - modules/custom/console_commands/src/Command/DisableUsersCommand.php
2 - modules/custom/console_commands/console.services.yml
3 - modules/custom/console_commands/console/translations/en/console_commands.disable_users.yml
- Edit the created DisableUsersCommand.php file and remove the boilerplate example code from the execute method:
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
}
- Update the function to create a DateTime object, representing 10 days ago. We will use this to generate a timestamp for our query:
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
// 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:
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
// 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)) {
$output->writeln('<info>No users to disable!</info>');
}
}
- Next, we will iterate over the results and mark the user as disabled:
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
// 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)) {
$output->writeln('<info>No users to disable!</info>');
}
foreach ($results as $uid) {
/** @var \Drupal\user\Entity\User $user */
$user = \Drupal\user\Entity\User::load($uid);
$user->block();
$user->save();
}
$total = count($results);
$output->writeln("Disabled $total users");
}
- Enable the module in order to access the following command:
$ drupal module:install console_commands