Drupal 9 Select Node Data With Query Conditions Using Entity

[Solved] Drupal 9 Select Node Data With Query Conditions Using Entity | Php Frameworks Drupal - Code Explorer | yomemimo.com
Question : Drupal 9 select node data with query conditions using entity type manager

Answered by : cheryl-mcnamee

use Drupal\node\Entity\Node;
// 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);
}

Source : | Last Update : Tue, 11 Jan 22

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

Code Explorer Popular Question For Php Frameworks Drupal