Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
EntityFieldQuery currently returns an incorrect entity count when you query a field that has multiple translations.
If you create a node with a translatable body field, and add 3 translations, querying for the entity like this:
$query = new EntityFieldQuery();
$entities = $query->fieldCondition('body')->execute();
will return one entity, but if you do the same thing with a count
$query = new EntityFieldQuery();
$count = $query->fieldCondition('body')->count()->execute();
the result is three.
This also means that $query->ordered_results will have duplicates.
Comments
Comment #1
loganfsmyth CreditAttribution: loganfsmyth commentedThis was enough to get it working for me.
I don't know if using $field['translatable'] directly is acceptable, or if we need to use something like this, http://api.drupal.org/api/drupal/modules--field--field.multilingual.inc/..., but then you need to have entity_type nailed down.
Comment #2
plachFrom a quick review the fix looks correct, I think we need some tests covering the bug.
Comment #3
loganfsmyth CreditAttribution: loganfsmyth commentedAdded a basic test that creates a single entity with a translatable field in two languages, with cardinality one.
This patch also includes adding entity_query.test to entity.info's file list, because it seems to have been missed when entities moved the a module. I've seen that particular fix one of the other entity related patches too, but it hasn't been committed yet.
Comment #4
plachStupid coding standard things to be fixed below. Attaching a test-only patch which is supposed to fail to prove that the tests capture the bug. Also, this will need to be backported.
Missing trailing dot.
Missing trailing dot.
Comment does not wrap at column 80.
-1 days to next Drupal core point release.
Comment #6
loganfsmyth CreditAttribution: loganfsmyth commentedHere's an update for coding standards, and a test-only patch to match.
Comment #7
plachGrrreat, but...
...comments should wrap as near as possible to column 80. In this case:
See http://drupal.org/coding-standards for details.
Comment #8
loganfsmyth CreditAttribution: loganfsmyth commentedAh, didn't realize that, thanks.
Comment #9
chx CreditAttribution: chx commentedIs DISTINCT what you want? Shouldn't this be a documentation patch to urge you to specify a language?
Comment #11
loganfsmyth CreditAttribution: loganfsmyth commentedI think in this case DISTINCT makes the most sense, as we may not know or want to query by the language. Also, the distinct is already there in any case whenever the cardinality isn't '1', so I felt this change wouldn't be a drastic addition. It doesn't really make sense that you could get one result entity when you run the query itself, but then get a count of more than one when you do a count query. Even if it were documented, it would be a fairly strange behavior to have.
As an example of not knowing the language to query by, I noticed this issue while working on Entity Translation because we want to look up all entities with values for a certain field, and then only later look up field values based on that particular entity's language.
Comment #12
plachI also think that distinct is the way to go here: since an entity has only one language there is no point in explictly specifying one to obtain the correct entity count. Unless @chx disagrees with this reasoning, I'd say #8 is RTBC.
Comment #13
chx CreditAttribution: chx commentedIt is.
Comment #14
plachComment #15
webchickThat testX() function needs some PHPDoc. I added:
And committed/pushed to 8.x. Thanks!
Needs a backport for D7.
Comment #16
loganfsmyth CreditAttribution: loganfsmyth commentedBackported to D7 and added webchick's PHPDoc.
Comment #17
plachnice!
Comment #18
webchickThank you!
Committed and pushed to 7.x.
Comment #19.0
(not verified) CreditAttribution: commentedUpdated issue summary.