Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Find Entity using Drupal EntityQuery

By editor , 29 May, 2016
Question

How to Find Entity (nodes, taxonomy terms, users ...) using Drupal EntityQuery ?

You can find any entity using Drupal 8 EntityQuery System.

Example 1. Taxonomy

Get list of all taxonomy terms
    $query = \Drupal::entityQuery('taxonomy_term');
    $tids = $query->execute();

Get term id of the vocabulary 'tags'
    $query = \Drupal::entityQuery('taxonomy_term');
    $query->condition('vid', 'tags');
    $tids = $query->execute();

Example 2. Node

Find all nodes where the title contains 'drupal'
    $query = \Drupal::entityQuery('node');
    $query->condition('title', '%drupal%','LIKE');
    $nids = $query->execute();

Example 3. User

Fiend all users who's name contains 'admin'
    $query = \Drupal::entityQuery('user');
    $query->condition('name', '%admin%','LIKE');
    $ids = $query->execute();

Example 4. Node with OR condition

    $query = \Drupal::entityQuery('node');
    $group = $query->orConditionGroup()
        ->condition('type', 'page')
        ->condition('type', 'article');
    $entity_ids = $query
        ->condition('title', '%drupal%','LIKE')
        ->condition($group)
        ->execute();

Example 5. With multiple AND + OR conditions

$query->condition('type', $type);
$query_or = $query->orConditionGroup();

$query_and = $query->andConditionGroup()
  ->condition('end1', $end1)
  ->condition('end2', $end2);
$query_or->condition($query_and);

$query_and = $query->andConditionGroup()
  ->condition('end1', $end2)
  ->condition('end2', $end1);
$query_or->condition($query_and);

$query->condition($query_or);


Example 6. Check A field value is NULL or is Not Null

$query = \Drupal::entityQuery('the_entity');
$query->notExists('field_to_check');//Check A field value is NULL
//OR
$query->exists('field_to_check');//Check A field value is NOT NULL
ids = $query->execute();

Tags

  • Drupal 8
  • Code
  • Module
  • Add new comment
Database Basic Examples (Select, Merge, Insert ...)
Database LIKE condition
Load entity dynamicly by entity type on Drupal 8

Comments6

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Adrien Lasserre (not verified)

10 years 5 months ago

How to query user by role

For a role named 'reviewer' :

 $uids = \Drupal::entityQuery('user')->condition('roles.target_id','reviewer')->execute();

Voilàa

 

  • Reply

editor

10 years 5 months ago

In reply to How to query user by role by Adrien Lasserre (not verified)

Merci

Thank you

  • Reply

Pamith (not verified)

9 years 4 months ago

Term Reference

How Can I use term id in condition

  • Reply

Anonymous (not verified)

9 years 3 months ago

In reply to Term Reference by Pamith (not verified)

field.tags.target_id should…

field.tags.target_id should do the trick

  • Reply

Anonymous (not verified)

9 years 3 months ago

In reply to field.tags.target_id should… by Anonymous (not verified)

Edit previous comment :…

Edit previous comment : field_tags.target_id 

  • Reply

Andres (not verified)

8 years 10 months ago

Thank you!

This save my day!

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer