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.
Currently there is no way to query entities based on comparison attributes. Attempts to query such as
http://server/<entityname>?price>2000
do not work in my tests.
The same could be used for dates:
http://server/<entityname>?changed>64000
Comment | File | Size | Author |
---|---|---|---|
#37 | ability_to_filter_with-1910294-37.patch | 22.75 KB | aangel |
#33 | ability_to_filter_with-1910294.patch | 10.15 KB | djdevin |
#12 | query-filter-operators-1910294-9.patch | 9.8 KB | gnucifer |
#10 | query-filter-operators-1910294-8.patch | 7.22 KB | gnucifer |
#8 | query-filter-operators-1910294-7.patch | 7.23 KB | gnucifer |
Issue fork restws-1910294
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
olragon CreditAttribution: olragon commentedMy first attempt to add more query operators, it should work with latest dev version, unstable, maybe buggy, need preview and improve.
Use this patch like this:
http://server/<entityname>?$filter[created]=~ge~1370175882&$filter[changed]=~le~1369657482
Supported operators: "=", ">=", ">", "<=", "<", "<>"
Comment #2
olragon CreditAttribution: olragon commentedComment #3
klausiI'm not sure we should pursue this path since we are basically replicating what Views and Views Datasourse are already doing. Implementing a query builder here just duplicates what is already done in other modules.
I have personally not experimented with those Views modules, so I can't say much where they differ in their JSON output for example. To me it makes much more sense to have a Views display plugin that renders the rows with the exposed form filters applied, like we do in Drupal 8's REST module Views integration.
Comment #4
gnucifer CreditAttribution: gnucifer commented@klausi I partly agree with you, the trouble is none of those modules works very well.
In both https://drupal.org/project/views_datasource and https://drupal.org/project/views_data_export you must meticulously add every field you want included, so more work to begin with, and makes maintenance is cumbersome (when adding/removing fields).
You could build an index-feed using one of the above modules, and than fetch the resources through restws, but if you want to expose a nice, consistent API to a third party you have a problem.
I really need the "greater than"-operator for dates since a third party will be polling a drupal site for content changes, and we don't want to send all the content in every poll (just content changed since !timestamp). One could place a pubsubhubbub-hub in between and solve the problem that way, but more query-operators would be so much more simple and convenient. I will try (and review) the patch, and see if I can find any issues.
I really understand if you want to avoid adding extra complexity that should/could be handled by other modules, but for me this (and more complete file-resources) are the only major things keeping restws from being the perfect REST-interface for Drupal.
Comment #5
gnucifer CreditAttribution: gnucifer commentedI had a look at the patch in #1, and even if it works as advertised I'm not sure it's the ideal way of going about this. It feels a bit kludgy to introduce an new $filter argument.
As a user I would expect
http://server/<entityname>?created=ge:1370175882
or similar. But such a syntax would mean that you have to escape ":" could break existing code. :(Comment #6
gnucifer CreditAttribution: gnucifer commentedHere is my take at it. It's not perfect, but perhaps good enough. I've implemented the syntax in my previous post. To avoid breaking existing code I added the setting "restws_advanced_fitlers" that must be set to TRUE to enable operators in filtering. I don't know if this is a very good idea since I know one of the design-goals of this module is consistent behavior, but since this patch could otherwise break things (":" now have to be escaped) I put it there just to be safe.
Comment #8
gnucifer CreditAttribution: gnucifer commentedAccidentally removed some comments, here is a new patch.
Comment #10
gnucifer CreditAttribution: gnucifer commented(Hopefully) fixed bug causing tests to fail (made a mistake when replacing parameters for fieldConditions with one value).
Comment #11
gnucifer CreditAttribution: gnucifer commentedRight now I am short of time, but will update the patch with operators-specific tests a soon as I can.
Comment #12
gnucifer CreditAttribution: gnucifer commentedAdded some tests, though I can't figure out why the last two of them fails ('CONTAINS' and 'STARTS_WITH'). The exact same queries works fine in a non-test environment. I get 404 because as a result of an empty result in the count-query. Uploading this anyway, will try to sort the failing tests out.
Comment #14
gnucifer CreditAttribution: gnucifer commentedLets try again (think I put my variable_set('restws_filter_operators', TRUE)) in the wrong place, though it worked locally for me (probably because it was in my settings file).
Comment #15
gnucifer CreditAttribution: gnucifer commentedComment #16.0
(not verified) CreditAttribution: commentedModified HTML.
Comment #19
kenianbei CreditAttribution: kenianbei commentedUpdates patch to latest dev.
Comment #20
kenianbei CreditAttribution: kenianbei commentedComment #22
kenianbei CreditAttribution: kenianbei commentedHopefully this fixes simple test.
Comment #23
ciss CreditAttribution: ciss commentedI think it's a really bad idea to expect the operator in the value, as values may actually start with these strings which could break the results in unexpected ways. A far safer approach would be to append the operator to the field, e.g.:
This makes parsing easier as well, as a simple explode() will do the job:
As far as I know field names will never contain ":". Oh, and we should probably consider using "-" or "." instead of ":", as it doesn't require encoding. With that in mind the example would look like:
Thinking this through, it might actually be a lot easier and safer to just provide an additional parameter (e.g. "o", as "op" is already used too often for "operation") similar to what was proposed in #1. With that scenario the example would become:
This could later be expanded to support different operators per value, if multiple values are passed:
The huge benefit of this approach is that it provides a safe fallback in case the operator is not recognized (less values, not more).
Comment #24
rcodina CreditAttribution: rcodina commentedI agree with @ciss. Could anyone please do a new patch? I have tested patch on @22 but doesn't work for me.
All patches are too old and I think this is a basic feature to have and that's why I put this as major priority. In my case, I have events and I want to filter them between two dates.
@kenianbei With your patch, this doesn't work:
<URL>/node.json?type=MY_CONTENT_TYPE&field_data=ge:2017-01-01
It justs returns an empty list!
However, the following examples returns at least one result:
Thanks!
Comment #25
rudiedirkx CreditAttribution: rudiedirkx at ezCompany commentedHow would you query
field > 100 AND field < 200
?Comment #26
ciss CreditAttribution: ciss commented@rudiedirkx Please see the last section of my proposed solution, starting with "This could later be expanded to support different operators per value."
Comment #27
ciss CreditAttribution: ciss commentedI'm actually not quite sure why I still included the o parameter in my last example (the part described as "more readable"). The example should probably have read:
?field_name[gt]=12345&field_name[lt]=54321
Comment #28
djdevinThere may be a field called "field_name" with a column called "gt" - RestWS currently handles this and allows you to filter based on a field's column.
field_something[le]=120
field_something[value]=120
So I think to fully support properties and fields, you have to force specifying the field's column in order to have operators since I don't think we can differentiate between a column call and an operator. Maybe if we check that the field only has 1 column, we can allow it.
property_whatever[gt]=5
property_whatever[lt]=10
field_something[value][gt]=31
field_something[value][lt]=100
field_something[value2][gt]=5
field_some_other[value]=200
field_another=200
I extended the patch so it handles existing use cases but also handles multiple conditions discussed in #23. Probably needs some cleanup and more tests (for fields), but you can query like this now:
http://localhost/course_object.xml?coid[ge]=1&coid[le]=3&field_time_to_complete[value][ge]=100&field_time_to_complete[value][le]=150
Comment #30
djdevinFixed tests.
Comment #31
rcodina CreditAttribution: rcodina commented@djdevin Patch on #30 works for me if I filter integer fields. But If I use date fields, the filtered rows don't make sense. Have you tested it using date fields?
Comment #32
djdevinI've tested with date fields and it appears to work. field_course_date is an ISO format date field.
Comment #33
djdevinRe-rolled.
Comment #34
hestenetComment #36
aangel CreditAttribution: aangel commentedComment #37
aangel CreditAttribution: aangel at HeroDevs commentedI've done some manual testing, added some tests that handle the three types of date fields (I think what I did should work without the Date module being required) and added other filtering tests.
Comment #38
drummThis project has switched to GitLab CI, so merge requests should be used instead of patch files.
Comment #40
froboy@drumm it looks like @aangel did open a branch, but missed creating the MR. I've done that: https://git.drupalcode.org/project/restws/-/merge_requests/2
Comment #41
froboy@drumm I see a bunch of phpcs errors/warnings, but I'd imagine much of that is preexisting tech debt and not from the changes in this issue. How would you recommend moving forward?
Comment #42
aangel CreditAttribution: aangel at HeroDevs commentedYes, those phpcs warnings were there before and I didn't want to expand the MR with non-issue related changes. I might have fixed a very few things but not many.