Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Drupal 9 select node data with query conditions using entity type manager

use Drupal
odeEntityNode;

// Various entity type manager query conditions and options
// that you can use to select node data from a drupal database.
$query = Drupal::entityTypeManager()->getStorage('node')->getQuery();
// get only nodes of this content type
$query->condition('type', 'my_content_type_machine_name');
// that are "published" nodes
$query->condition('status', 1);
// and UUID equals..
$query->condition('uuid', $my_input_uuid);
// Sort by a value in a given field
$query->sort('field_my_custom_field', 'some value');
// To return a subset of the result
// This gives you only the first 10
$query->range(1, 10);
// execute query, it returns an array of matching node ids
$nids = $query->execute();

// LOAD THE RESULTING NODE OBJECTS
if (!empty($nids)) {
  $nodes = Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadMultiple($nids);
}
 
PREVIOUS NEXT
Tagged: #Drupal #select #node #data #query #conditions #entity #type #manager
ADD COMMENT
Topic
Name
3+4 =