logo

DRUPAL 8 field Query

alam
28 May 2021

Select those users which name (Custom field) is empty

In the following I have created a custom field (name) in user profile, field machine name as field_name

I want get user list where name field is blank

 

$uids = \Drupal::entityQuery('user')
    ->condition('field_name', NULL, 'IS NULL')
    ->execute();
echo count($uids);

If you want to get more user through user id

foreach($uids as $uid){
	$users = User::load($uid);
	$users->get('name')->value;
}
admin
22 Apr 2020

Code to query for getting node according to field condition in DRUPAL 8

$query = \Drupal::entityQuery('node')
  ->condition('status', NODE_PUBLISHED)
  ->condition('type', 'custom_type');
$and = $query->andConditionGroup();
$and->condition('custom_taxonomy', 2);
$query->condition($and);
$and = $query->andConditionGroup();
$and->condition('custom_taxonomy', 8);
$query->condition($and);
$result = $query->execute();