Skip to content

Instantly share code, notes, and snippets.

@ultimike
Created July 10, 2020 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ultimike/cd2fe8d2158a98d585009c82d21cd9bb to your computer and use it in GitHub Desktop.
Save ultimike/cd2fe8d2158a98d585009c82d21cd9bb to your computer and use it in GitHub Desktop.
<?php
public function go() {
// Documentation: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Query%21QueryInterface.php/interface/QueryInterface/8.9.x
// Example 1
$query = \Drupal::entityQuery('node')
->condition('type', 'blog')
->condition('field_blog_type', '0') // 0=Tutorial, 1=News item
->range(0, 10)
//->sort('created', 'DESC')
->sort('vid', 'DESC');
// Cast the query as a string.
$this->logger()->notice($query->__toString());
$results = $query->execute();
// Example 2
// $results = \Drupal::entityQuery('node')
// ->condition('type', 'audio')
// //->condition('main_topic.entity:taxonomy_term.name', 'DrupalEasy Podcast')
// ->condition('uid.entity:user.name', 'ryanprice')
// ->execute();
// Example 3
// $query = \Drupal::entityQuery('node');
// $group = $query
// ->orConditionGroup()
// ->condition('created', '1262304000', '<') // Jan 1, 2010
// ->condition('created', '1577836800', '>'); // Jan 1, 2020
// $results = $query->condition($group)
// ->condition('type', 'audio')
// ->execute();
return $this->outputTable($results);
}
public function outputTable(array $results, $options = ['format' => 'table']) {
foreach ($results as $key => $nid) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$rows[] = [
'Node ID' => $nid,
'Title' => $node->getTitle(),
];
}
return new RowsOfFields($rows);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment