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.
Problem/Motivation
A view that uses an exposed filter on a page can be accessed from a link such as http://d.8/new?since=2014-09-10
and the value of since
will be used, filtering the view. A REST Export view with the same configuration will not be filtered by the exposed value. If there is some other mechanism that is supposed to be used to populate the filter, I can't find any documentation of it and it seems that the pattern from a page view should work with a REST view.
Repro steps:
- Install head under Apache/2.4.9 (Unix) PHP/5.5.15.
- Create two content items with different content dates.
- Create REST Export view with exposed filter greater than "Authored on" and set filter identifier.
- Make REST request to view endpoint, using filter identifier and a value in URL that should only display one node. Response will incorrectly include both nodes.
- Clone view to be a Page and use the same URL. Response will be correct.
Proposed resolution
Umm....
Remaining tasks
Identify cause & patch?
User interface changes
Existing feature will work as expected.
API changes
None?
Comment | File | Size | Author |
---|---|---|---|
#9 | interdiff-2340623-7-9.txt | 797 bytes | johnennew |
#9 | 2340623-9.patch | 8.09 KB | johnennew |
#7 | allow_rest_exposed_filter-2340623-7.patch | 7.84 KB | johnennew |
#1 | allow-rest-exposed-2340623-1.patch | 453 bytes | R.Muilwijk |
Comments
Comment #1
R.Muilwijk CreditAttribution: R.Muilwijk commentedUsing the exposed filters is fairly easy and could be a valuable addition to a GET REST endpoint. Patch attached.
Comment #2
sebas5384 CreditAttribution: sebas5384 commentedHey @R.Muilwijk thanks!! you saved my day ;)
This worked exactly like I was expecting to work, I don't know if I'm the right person to review this because I don't know what kind of problems could bring this feature.
But it worked!, and for curious I would love to know why this wasn't released at first.
Please!! somebody from the core, this is a really important feature, not just for filters, for example exposed pagination.
Cheers!
Comment #3
klausiMakes sense, can we get a test case for this?
Comment #4
kalabroI'm working on this at DrupalCamp Baltics 2015 Code Sprint!
Comment #5
kalabro@klausi Could you please explain what kind of test would you like to see here? As I can see, REST module isn't responsible for Views logic tests. Views exposed filters are tested in Views module itself.
Comment #6
klausiWe should write a web test that does something similar as described in the issue summary. Create a REST view, add some nodes, sent a request with an exposed filter, make sure you only get filtered results back.
Comment #7
johnennew CreditAttribution: johnennew at Deeson commentedPlease find a new patch with a test attached.
1. Adds a new test view with an exposed title filter set to filter by titles which start with the provided value
2. Creates 3 nodes specifying the title
3. Queries without the title query parameter to check all 3 nodes are returned - this shows the patch is not changing the no query parameter behaviour
4. Queries again with a title query parameter which should reduce the number of nodes returned to be 2 - this shows providing a parameter applies the query and filters the results.
Comment #8
dawehnerThank you for adding a test!
I think we should better test the cacheabilty metadata introduced by those exposed filters as well, just to be sure. So use \Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait and check assertCacheContexts() that it includes the proper query parameters
Comment #9
johnennew CreditAttribution: johnennew at Deeson commentedHi @dawehner - I've added a test which makes use of assertCacheContexts(). I couldn't see if it was possible to check if the specific query parameters were being included in the cache - is this possible? I'm at DrupalCon Barcelona today (Friday 25th) if you want to ping me any instructions Drupal Contribute as ceng_
Comment #10
Anonymous (not verified) CreditAttribution: Anonymous commentedAny chance to get this into final Drupal 8.0?
Comment #11
dawehnerWow nice, I'd not have expected that its sad easy. Nice work
Comment #12
xjmRetitling to clarify the bug. @effulgentsia and I agreed that this can be fixed during RC because this is a common usecase, the fix is non-disruptive, and we don't actually need to change anything except enabling the functionality.
I do find myself wondering why this got set to false in the first place, though. I might ask @damiankloip or check the git history. Leaving at RTBC but I'd want to double-check that before committing this patch myself--someone else can still commit it though if they want.
Comment #13
dawehnerWell, I just think we didn't believed that it actually works that easy.
Comment #14
xjm@phenaproxima checked and it's been set to FALSE since comment #9 of #1819760-9: Add a REST export display plugin and serializer integration., which was basically the first patch in that issue based on the display plugin refactor. It's not discussed on the issue at all there, so indeed it was not disabled for any specific reason other than what @dawehner suggests in #13 or scope management or whatnot.
Committed and pushed to 8.0.x. Thanks!
Comment #16
damiankloip CreditAttribution: damiankloip commentedYes. I think we just had it disabled....because. I wouldn't be surprised if there are a few issues, and expectations based on a regular exposed form. It works so easily because views exposed filters/form uses GET anyway.
Over all +1. Just don't blame me if some things don't work :)
Comment #19
khaled.zaidan CreditAttribution: khaled.zaidan at Reading Room commentedThis problem seems to be broken again (despite the display type still being set as "usesExposed").
After doing a bit of debugging, I've found that in the case of the REST export, the exposed filter is completely missing from the views filters (in the $view object), whereas it's present in other display types (e.g. page).
Looking further into it right now.
Comment #20
khaled.zaidan CreditAttribution: khaled.zaidan at Reading Room commentedI'm actually just completely wrong... I was editing one view while looking at another!
Please ignore my previous comment :)