Problem/Motivation
Currently it does not seem to be possible to sort a config entity query through the third party settings.
return $this->getQuery($this->keys, $this->category, $this->state)
->sort('third_party_settings.custom_module.last_updated', 'DESC')
->execute();
This seems to be because the execute() method of \Drupal\Core\Config\Entity\Query\Query does not handle nested properties in the way the conditions on the query are handled.
Proposed resolution
Parse fields in the same way the conditions for the config entity queries are handles (so split the field by dot).
Remaining tasks
Discuss the solution, add some tests.
User interface changes
None
API changes
None
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | 2942569-16.patch | 11.63 KB | idebr |
Comments
Comment #2
seanbPatch is attached, still need to add tests if this approach is ok, please review!
Comment #3
seanbComment #4
amateescu commentedThe patch looks very nice and clean, let's add a quick test and we should be good to go.
Comment #5
idebr commentedUpdating to 'Needs work' per #4
Comment #6
seanbAdded a test.
Comment #7
seanbSo idebr just pointed out it might be good to also test for the reverse order. Totally agree, new patch attached.
Comment #8
amateescu commentedThe assertions looks a bit weird. Is that because entities with the IDs
2, 4and1, 3have the same data?Comment #9
seanbYes, the entities with IDs 1 and 3 both have a level value of 1, and the entities with IDs 2 and 4 both have a level value of 2. In this case, the sorting doesn't do anything and the order shouldn't change between those records.
Comment #10
berdirWe had a lot of trouble with php7 test fails due to sort changes, are you sure this works the same on all php versions we support?
Comment #11
amateescu commentedLet's find out. I queued test runs for all php versions.
Comment #12
amateescu commentedAll the test runs are green, I think we're good to go here.
Comment #13
seanbJust found out this doesn't work as expected when 1 of the items does not have a value for a property. Will fix this and add a test as well!
Comment #14
seanbThis should do it, since I needed to update the tests anyway, also changed the tests in a way to make sure the not nested properties are sorted in the right way with a missing value (which already worked btw).
Comment #16
idebr commentedReroll against 8.7.x to include changes from #2968519: The entity.query service is essentially deprecated because it relies on \Drupal\Core\Entity\Query\QueryFactory which is
Comment #17
amateescu commentedLooked at this again and I think it's ready to go.
Comment #19
alexpottThe change looks good and is still passing tests but we need a change record to tell people this is possible.
Comment #23
kristiaanvandeneyndeSimple change record here: https://www.drupal.org/node/3155124
Did not fill out the version numbers as I'm hoping this will be backported to Drupal 8.9 as well, along with #3154858: Drupal\Core\Config\Entity\Query\Condition::notExists() does not work when parent property is also missing. which deals with the exact same issue, except it's notExists() being broken there, rather than sort().
Comment #24
alexpottIn a follow-up I think this can be rewritten to be
($a <=> $b) * $directionwhich is more elegant and returns the correct value when we consider them equal. Which will be better for PHP 8 and stable sorts.For this to be backported to 8.9.x this would have to be considered a bug. I think there are arguments that this is a bug because sorting in these situations sorting does not work where you would expect it to. And therefore my request for a change record is a little bogus and had the affect of derailing the issue for 2 years :( Totally my fault. I should have given more consideration to whether this really was a feature request.
@kristiaanvandeneynde thanks for writing the CR - I'm going to credit you here but delete it as I no longer think this is worthy of a CR - it's fixing things to work as you would expect.
Comment #25
alexpottCurrently discussing the backport with @catch. Will do that if we agree.
Committed a07a4ba and pushed to 9.1.x. Thanks!
Comment #27
alexpottDiscussed with @catch and we agreed to backport this.
Comment #33
alexpottHad to hotfix the test because this patch removed the $all variable but the test added by #3154858: Drupal\Core\Config\Entity\Query\Condition::notExists() does not work when parent property is also missing. relied on it.