5

I'm running an app where I am searching for every nodes of a given content type.

$nids = \Drupal::entityTypeManager()
  ->getListBuilder('node')
  ->getStorage()
  ->loadByProperties([
    'type' => 'courses',
    'status' => 1,
]);

I'm searching for a way to sort the results among a custom field of my nodes.

EDIT: I do not understand what's going on. Thanks to @4k4, I wrote this query:

$query = \Drupal::entityTypeManager()
    ->getListBuilder('node')
    ->getStorage()
    ->getQuery();
$query->condition('type', 'post');
$query->condition('status', 1);
$query->condition('field_category', $k);
$query->sort('field_ordering', 'ASC');

$nids = $query->execute();
$nids = Node::loadMultiple($nids);

A var_dump on $r[1] prints nothing. Same for $r[2]. But $results contains my results. What's going on ? The sorting doesn't work by the way.

5
  • use entityQuery, see drupal.stackexchange.com/questions/205075/…
    – 4uk4
    Oct 25, 2017 at 16:20
  • 1
    I am working on 8.4 and entityQuery is deprecated since 8.3. Oct 26, 2017 at 7:03
  • No, entityQuery is not deprecated, or do you mean how to get to it? You know if you inject entityTypeManager you can get entityQuery from it as well?
    – 4uk4
    Oct 26, 2017 at 8:28
  • Did I miss-read this post ? drupal.org/node/2389335 Could you provide me a snippet using entityQuery that joins with nodes custom fields tables ? I tried entityQueryAggregate but it won't work. I am totally lost here. Oct 26, 2017 at 8:56
  • Yes, you are, loadByProperties was never meant to be a replacement for entityQuery, it is a shortcut to it for simple searches without sorting or other advanced things. See the answer how to sort with entityQuery in D8.4
    – 4uk4
    Oct 26, 2017 at 9:31

1 Answer 1

8

The change record says if you have entityTypeManager injected, use

$query = $this->entityTypeManager->getStorage('node')->getQuery();

For non-OOP code, $query = \Drupal::entityQuery(); is not deprecated and can still be used.

The linked example for sorting an entity query by using $query:

$query->condition('type', 'blog_post')
  ->sort('field', 'DESC');
$nids = $query->execute();
5
  • Thank you this helps. However, I need to add a condition on a custom field (field_test) and it seems the joins aren't made in this request. Oct 26, 2017 at 9:55
  • Is sorting working now? - For a condition on a field, this would be a separate issue. Normally you can use all fields. If not, ask another question and tell what is special about the custom field so that is doesn't work.
    – 4uk4
    Oct 26, 2017 at 10:12
  • I edited my post. Oct 26, 2017 at 10:21
  • Please don't post this amount of code not directly connected to the question.
    – 4uk4
    Oct 26, 2017 at 11:36
  • I found my error. I was doing a foreach on my table and reindexing my output array with the node key (so the ID). I turned the keys to strings and it works ! Thanks. Oct 26, 2017 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.