Skip to content

Instantly share code, notes, and snippets.

@gargsuchi
Created April 10, 2017 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gargsuchi/7c38515d6826532ec9efbde1865d8061 to your computer and use it in GitHub Desktop.
Save gargsuchi/7c38515d6826532ec9efbde1865d8061 to your computer and use it in GitHub Desktop.
function lotus_cron() {
$result = \Drupal::entityQuery("user")
->condition('login', strtotime('-6 months'), '<=')
->execute();
$storage_handler = \Drupal::entityTypeManager()->getStorage("user");
foreach ($result AS $user) {
$entity = $storage_handler->load($user);
$entity->block();
$storage_handler->save($entity);
}
}
@jkamizato
Copy link

We can use \Drupal\user\Entity\User::load as well, its gonna be:


function lotus_cron() {

  $result = \Drupal::entityQuery("user")
    ->condition('login', strtotime('-6 months'), '<=')
    ->execute();

  foreach ($result AS $user) {
    $entity = \Drupal\user\Entity\User::load($user);
    $entity->block();
    $entity->save();
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment