Drupal 8 Rules module - How to configure Rules module to send email notification for every comment posted
Introduction In our previous post, where we saw How to configure comments module…
December 30, 2021
Here, we will see the drupal code to fetch all the active users (not blocked) and who accessed the website within last year.
We will also convert the output to JSON and save the JSON in a file.
This can be useful if you want to fetch users who are inactive. You may want to send them an email to login, or you may want to disable their accounts due to inactivity.
//1 year seconds
$seconds = 3600 * 24 * 365;
$newtime = time() - $seconds;
$baseFolder = 'your_folder/';
$query = \Drupal::entityQuery('user')
->condition('status', '1')
->condition('access', '0', '!=')
->condition('access', $newtime, '<=');
$uids = $query->execute();
foreach ($uids as $uid) {
$filename = $baseFolder.$uid.'.json';
$user = user_load($uid);
$data = \Drupal::service('serializer')->serialize($user, 'json', ['plugin_id' => 'entity']);
print('<br/>Writing to file: '.$filename);
file_put_contents($filename, $data);
}
If your users are a lot, you can use pagination.
//1 year seconds
$seconds = 3600 * 24 * 365;
$newtime = time() - $seconds;
$baseFolder = 'your_folder/';
$query = \Drupal::entityQuery('user')
->condition('status', '1')
->condition('access', '0', '!=')
->condition('access', $newtime, '<=')
->range(0, 100);
$uids = $query->execute();
foreach ($uids as $uid) {
$filename = $baseFolder.$uid.'.json';
$user = user_load($uid);
$data = \Drupal::service('serializer')->serialize($user, 'json', ['plugin_id' => 'entity']);
print('<br/>Writing to file: '.$filename);
file_put_contents($filename, $data);
}
select uid, name, status, from_unixtime(created), from_unixtime(changed),
from_unixtime(access), from_unixtime(login)
from users_field_data
where status=1 and access!=0
and access >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
limit 1000, 10;
Earlier, I wrote about the Drupal Code to Fetch Active Users
Introduction In our previous post, where we saw How to configure comments module…
Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…
See the code below: The output will be 4 columns separated by comma. You can…
Introduction Drupal is an awesome CMS. Drupal content type form, allows you to…
Problem In drupal textarea field, it was always a pain to see the two links…
Note: I have public URLs of these images, which I want to save. return…
Introduction This post has the complete code to send email through smtp server…
Introduction In a normal email sending code from python, I’m getting following…
Introduction In one of my app, I was using to talk to . I have used some event…
Introduction So you have a Django project, and want to run it using docker image…
Introduction It is very important to introduce few process so that your code and…
Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…