2

I'm trying to add an sort condition to an EFQ on an reference entity table, I think a code example explains this better.

$query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
$ids = $query->condition('type', 'some_type')
  ->condition('some_more', 'conditions')
  ->sort('a_term_field', 'ASC')
  ->execute();

Now what happens is that the nodes will be sorted by the term id (since that is what is stored in the field table) but I would like to sort on the term label instead. Is something like that possible with EFQs?

3

Have you tried ->sort('a_term_field.entity.name', 'ASC'), that should work.

Some limitations exists with multilingual fields, as it is not defined on which language it should be sorted.

  • Works like a charm, did I miss documentation for this feature or is it just a hidden gem? – googletorp Aug 1 '16 at 8:25
  • You mean relationship support in general? (this works for conditions as well btw). I thought I've seen some documentation about this but all I cound find for now is the change record: drupal.org/node/1882418 – Berdir Aug 1 '16 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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