Skip to main content
Category:

Simple snippet on how to Block/activate users programmatically in drupal 8.

<?php


/**
 * @param $variables
 */
function mymodule_preprocess_page(&$variables)
{
  $result = \Drupal::entityQuery("user")
    ->execute();

  $storage_handler = \Drupal::entityTypeManager()->getStorage("user");
  foreach ($result AS $user) {
    $entity = $storage_handler->load($user);
    // activate users
     $entity->activate();
    // block users
    // $entity->block();
    $storage_handler->save($entity);
  }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook