Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have a table view with exposed filters and a sort on a date field.
I added a data export with the same structure of the page, with parent sort selected, but while the exposed filters work, the sort doesn't, both with or without the exposed option selected.
I tried to debug the code, but I found no clue.
Any ideas?
Comments
Comment #2
sjancich CreditAttribution: sjancich commentedI have the same problem. The sort is correct when I do not have batched exports enabled. However, with batching, the export contains rows without my sort -- even though the parent sort option is selected.
I turned on the "show SQL queries" debug feature in views, and for my data export display, there is no ORDER BY clause in the query.
Comment #3
ankur.addweb CreditAttribution: ankur.addweb at AddWeb Solution Pvt. Ltd. commentedCan you try this with Views Data Export 7.x-3.1 ? Its working with me with exposed filters and batch process on.
Comment #4
sjancich CreditAttribution: sjancich commentedI still have the issue with 7.x-3.1.
Comment #5
kostask CreditAttribution: kostask at Point Blank commentedI experienced a similar issue: The sort was working on the view and not on the views export.
I had this behaviour only on the production server (MariaDb) and not on my dev server (MySQL).
A temporary solution that applies the sort to the export was to add DISTINCT (Query settings: check Distinct) to the query.
Comment #6
antiorario CreditAttribution: antiorario commentedThe sort bug on MariaDB is still present in 7.x-3.2. It's particularly nasty because Pantheon runs MariaDB, and I can only imagine how many large sites are potentially affected. The
distinct
trick works, but I'm afraid it won't be viable in all cases, so finding an actual solution would be best. I'm wondering whether this is a bug with MariaDB itself, and I'll try to poke around as soon as I have some time.Comment #7
nitheesh CreditAttribution: nitheesh commentedCan confirm the sort is working on Pantheon site when we apply
distinct
option.Comment #8
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedJust ran into this myself on Pantheon.
Comment #9
TunprogApplying
distinct
fixes the issue for me. Thank you.Comment #10
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedSo, as @antiorario feared, I've just run into a situation where distinct is not an option -- Search API results.We're trying to give our users the ability to export search results as CSV reports. Unfortunately, in this scenario, "distinct" isn't an option and the data does not come out in the same order as the search result page.As it turns out, sorting does appear to work when exporting in Search API results, though still not 100%. The default sort for the search view doesn't seem to apply (i.e. if "Relevancy" is the default sort, among several other exposed sorts, then the results are not exported according to relevancy if the user does not change the sort). But, if I toggle to a different sorting option, or toggle away from the default and then back to it, sort apply. I tried using the option for "parent sort" but all that did was make the "Back to previous page" link appear on the batched export success page (which is desirable... but doesn't actually fix the sorting issue).
The reason I didn't think sorting worked was because I was using Better Exposed Filters with the "combined sorts" option (so that there is only one QS parameter for sorting instead of field and direction being separate QS parameters), while the export display of the view was using the standard Views exposed form. The two parse QS parameters differently, so it was ignoring the search parameters from the parent display.
Comment #11
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedOk, I take it back. Sorting works correctly when exporting from a display that is attached to a Search API view. The reason why it didn't seem like the default sort of the parent display was having an effect was because the sort orders on the export display of the view weren't the same as the orders of the same sorts in the parent display.
Specifically, I had 'Relevance" set to sort DESC in the parent view and it was set to sort ASC in the export view by accident. That is why the export only matched the search if I explicitly set the sort order in the QS of the parent display because it would override the default incorrect sort in the export view.
Comment #12
JimCraner CreditAttribution: JimCraner commentedI can confirm that the
DISTINCT
trick does NOT work for 7.x-3.2 when sorting by a Date field.Comment #13
alesr CreditAttribution: alesr commentedAfter losing some nerves with this I was finally able to find a solution with the combination of DISTINCT and hook_views_query_alter().
Just with hook_views_query_alter it didn't work because the query in batches doesn't care for ORDER BY.
Without the batching it worked fine. So the only thing that worked for me is to enable DISTINCT in this specific view display and adding hook_views_query_alter()
Hope it helps anyone else ;)