1

The title seems a bit odd, I know, but I couldn't find any issues similar to this and that's the best I could come up with. We are running into an odd instance of entityQuery returning nodes that shouldn't be returned.

We have a site with 3 languages installed, EN, PT-BR and ES. I have an entityQuery where I'm trying to select nodes with conditions for two entity reference fields (genre and platform, both custom vocabularies, being queried on the custom field 'field_shortname'), in one of the 3 languages. There are currently 3 nodes. The first 2 nodes reference 'arcade' and 'ps4' in all languages. The third node references 'arcade' and 'switch' in EN and PT-BR, but 'arcade' and 'ps4' in ES. I'm trying to query for nodes in EN with reference 'arcade' and 'ps4' - Here's the entityQuery code:

$query = \Drupal::entityQuery('node');    
$query->condition('type', 'game');
$query->condition('status', 1);
$query->condition('langcode', 'en', '=');
$query->condition(
  $query->andConditionGroup()
  ->condition('field_genre.entity.field_shortname', 'arcade')
);
$query->condition(
  $query->andConditionGroup()
  ->condition('field_platform.entity.field_shortname', 'ps4')
);
$query->execute();

I expect the query to return the 2 nodes that reference 'arcade' and 'ps4' in EN. Instead, all 3 are being returned - I'm assuming since the 3rd node references 'ps4' in one of the translations. I've tried adding the langcode into the conditions, which fixes the EN query but breaks the queries when I try the other languages - they return nothing instead:

$query->condition(
  $query->andConditionGroup()
  ->condition('field_genre.entity.field_shortname', 'arcade', '=', 'es')
);
$query->condition(
  $query->andConditionGroup()
  ->condition('field_platform.entity.field_shortname', 'ps4', '=', 'es')
);

Anybody have any ideas? Seems like it should be a pretty straightforward query, but I just can't seem to get anything to return the correct results. This is on Drupal 8.3.7. Thanks in advance!

Your Answer

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

Browse other questions tagged or ask your own question.