Hi guys,

Filtering and sorting combined seem to be not working.

My request parameters are the following:

{{host}}/api/node/article?_format=api_json&filter[0][value]=250&filter[0][operator]=CONTAINS&filter[0][field]=title&page[size]=3&page[offset]=0&fields[node--article]=title&sort=title

Basically the query should get me the two articles that contain "250" within its title sorted by title but it is returning all the "article nodes" sorted but not filtered:

{
  "data": [
    {
      "type": "node--article",
      "id": "30",
      "attributes": {
      "title": "250 items"
      }
    },
    {
      "type": "node--article",
      "id": "12",
      "attributes": {
      "title": "300 items"
      }
    },
    {
      "type": "node--article",
      "id": "25",
      "attributes": {
      "title": "400 items"
      }
    }
  ]
}

Steps to reproduce it:
1. build a query with sort and filter indexes.
2. Try to use a filter value that you know only will return certain results.
3. You will get the results sorted but not filtered.

Expected result:
1. The results should be filtered and then sorted:

{
  "data": [
    {
      "type": "node--article",
      "id": "30",
      "attributes": {
      "title": "250 items"
      }
    },
    {
      "type": "node--article",
      "id": "12",
      "attributes": {
      "title": "250 other example"
      }
    }
  ]
}
CommentFileSizeAuthor
#5 filtering_and_sorting-2792399-5.patch576 bytesgabesullice

Comments

mpastas created an issue. See original summary.

mpastas’s picture

Issue summary: View changes
mpastas’s picture

Issue summary: View changes
gabesullice’s picture

@mpastas, I'm looking into this now. Thanks for the bug report!

gabesullice’s picture

Status: Active » Needs review
StatusFileSize
new576 bytes

This should fix your issue @mpastas.

I also noticed another bug while working on this, you may need to also apply #2793059: [BUGFIX] Shorthand filter parameters always use the '=' operator.

gabesullice’s picture

Title: Filtering and sorting combined in query seem to be not working » [BUGFIX] Sort parameters can override filter parameters with numeric indices.

Updating title.

dagmar’s picture

Thanks @gabesullice this fixes our issue. I think this may need some tests to get in, right?

gabesullice’s picture

@dagmar yes :) interested?

gabesullice’s picture

Title: [BUGFIX] Sort parameters can override filter parameters with numeric indices. » [BUGFIX] Prevent sort parameters from overriding filter parameters with numeric indices

Updating issue title to be in line with the standard mentioned here: https://www.drupal.org/node/2793195#comment-11573427

e0ipso’s picture

Status: Needs review » Needs work

Good!

Only a nit pick.

+++ b/src/Query/QueryBuilder.php
@@ -185,7 +185,7 @@ class QueryBuilder implements QueryBuilderInterface {
+      $extracted[] = $this->newSortOption(sprintf("sort_%s", $sort_index), $sort);

Please favor ' over ".

As suggested by @dagmar we can probably benefit from a test around this.

gabesullice’s picture

e0ipso’s picture

@gabesullice would you be working on that follow up? I wouldn't like to defer this to a follow up to have it forgotten in the issue queue.

If you are not taking #2730245: [TASK] Test classes in the Query namespace, can you edit the description so someone new to the project can have enough context and direction (maybe suggest some implementation details) to tackle this.

I'll be merging this.

  • e0ipso committed 2d592f7 on 8.x-1.x authored by gabesullice
    Issue #2792399 by gabesullice, e0ipso: [BUGFIX] Prevent sort parameters...
e0ipso’s picture

Status: Needs work » Fixed
gabesullice’s picture

@e0ipso I've updated #2730245: [TASK] Test classes in the Query namespace , although I'll probably be the one who takes it eventually ;)

Status: Fixed » Closed (fixed)

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