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

Comments

seanB created an issue. See original summary.

seanb’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new862 bytes

Patch is attached, still need to add tests if this approach is ok, please review!

seanb’s picture

Title: Allow sorting third party settings of config entity queries » Allow sorting nested properties of config entity queries
amateescu’s picture

The patch looks very nice and clean, let's add a quick test and we should be good to go.

idebr’s picture

Status: Needs review » Needs work

Updating to 'Needs work' per #4

seanb’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.72 KB
new741 bytes

Added a test.

seanb’s picture

StatusFileSize
new1.9 KB
new746 bytes

So idebr just pointed out it might be good to also test for the reverse order. Totally agree, new patch attached.

amateescu’s picture

+++ b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php
@@ -565,6 +565,15 @@ public function testDotted() {
+    $this->assertResults(['1', '3', '2', '4', '5']);
...
+    $this->assertResults(['5', '2', '4', '1', '3']);

The assertions looks a bit weird. Is that because entities with the IDs 2, 4 and 1, 3 have the same data?

seanb’s picture

The assertions looks a bit weird. Is that because entities with the IDs 2, 4 and 1, 3 have the same data?

Yes, 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.

berdir’s picture

We 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?

amateescu’s picture

Let's find out. I queued test runs for all php versions.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

All the test runs are green, I think we're good to go here.

seanb’s picture

Status: Reviewed & tested by the community » Needs work

Just 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!

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new11.89 KB
new11.82 KB

This 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).

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

idebr’s picture

StatusFileSize
new11.63 KB
amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Looked at this again and I think it's ready to go.

The last submitted patch, 14: 2942569-14.patch, failed testing. View results

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

The change looks good and is still passing tests but we need a change record to tell people this is possible.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kristiaanvandeneynde’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record

Simple 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().

alexpott’s picture

Title: Allow sorting nested properties of config entity queries » [backport] Sorting nested properties of config entity queries does not work
Version: 9.1.x-dev » 8.9.x-dev
Category: Feature request » Bug report
Issue summary: View changes
+++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php
@@ -89,7 +89,14 @@ public function execute() {
+        return ($a <= $b) ? $direction : -$direction;

In a follow-up I think this can be rewritten to be ($a <=> $b) * $direction which 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.

alexpott’s picture

Currently discussing the backport with @catch. Will do that if we agree.

Committed a07a4ba and pushed to 9.1.x. Thanks!

  • alexpott committed a07a4ba on 9.1.x
    Issue #2942569 by seanB, idebr, amateescu, kristiaanvandeneynde: Sorting...
alexpott’s picture

Title: [backport] Sorting nested properties of config entity queries does not work » Sorting nested properties of config entity queries does not work
Status: Reviewed & tested by the community » Fixed

Discussed with @catch and we agreed to backport this.

  • alexpott committed 6434476 on 9.0.x
    Issue #2942569 by seanB, idebr, amateescu, kristiaanvandeneynde: Sorting...

  • alexpott committed d24a452 on 8.9.x
    Issue #2942569 by seanB, idebr, amateescu, kristiaanvandeneynde: Sorting...

  • alexpott committed 02d4434 on 9.1.x
    Issue #2942569 hotfix: Sorting nested properties of config entity...

  • alexpott committed 9357d0b on 9.0.x
    Issue #2942569 hotfix: Sorting nested properties of config entity...

  • alexpott committed c4e1f91 on 8.9.x
    Issue #2942569 hotfix: Sorting nested properties of config entity...
alexpott’s picture

Had 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.