Drupal 7: How to save a node programmatically and add an image field from a public URL
Note: I have public URLs of these images, which I want to save. return…
December 30, 2021
This post shows code to run database query to fetch active user details, its count, with pagination and write its result json to a file.
We will use \Drupal::entityQuery
to run database query
$query = \Drupal::entityQuery('user')
->condition('status', '1');
$uids = $query->execute();
foreach ($uids as $uid) {
$user = user_load($uid);
print_r($user);
}
Above code will just print the result on console or browser.
If your users count is huge, We should use pagination. Else, we may get out of memory kind of errors. Or, database may become unresponsive.
$query = \Drupal::entityQuery('user')
->condition('status', '1')
->range(0, 1000);
$uids = $query->execute();
foreach ($uids as $uid) {
$user = user_load($uid);
print_r($user);
}
Notice the range(offset, count)
function. Assume you have 5000 users, you will run above query 5 times, with following range
parameters.
range(0, 1000)
range(1000, 1000);
range(2000, 1000);
range(3000, 1000);
range(4000, 1000);
//get total users
$query = \Drupal::entityQuery('user')
->condition('status', '1');
$total_users = $query->count()->execute();
print_r($total_users);
You can also run above query to get the total count of users, based on query condition.
We may want to convert the output to JSON and write it to file.
$query = \Drupal::entityQuery('user')
->condition('status', '1')
->range(0, 1000);
$uids = $query->execute();
$baseFolder = '/your_folder/';
foreach ($uids as $uid) {
$user = user_load($uid);
$data = \Drupal::service('serializer')->serialize($user, 'json', ['plugin_id' => 'entity']);
$filename = $baseFolder.$uid.'.json';
print('<br/>Writing to file: '.$filename);
file_put_contents($filename, $data);
}
We first need to convert the object to json, it is done by:
\Drupal::service('serializer')->serialize($user, 'json', ['plugin_id' => 'entity'])
Then, we are creating a separate file based on uid
of user.
Also see: Drupal Code to Fetch User Accessed Website within Last One Year
Note: I have public URLs of these images, which I want to save. return…
Introduction I have created a view, with some filters and content fields. I will…
Bootstrap has a simple solution to have sticky blocks in your html. I’ve given a…
Introduction Twig is a powerful template engine for php. Drupal uses it heavily…
Introduction Drupal provides a powerful comment module, which comes as a part of…
Introduction Drupal is an awesome CMS. Drupal content type form, allows you to…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…