Problem/Motivation

Existing JSON endpoint usually support filtering through parameters in the query string of the JSON file URL.

Proposed resolution

Provide "Query String" filter and contextual filter handlers. The value of the filters are inserted as parameter in the query string of the JSON request.

Comments

jmymlr’s picture

Category: Feature request » Bug report
pbuyle’s picture

The attached patch implement the suggested solution, but applies after my patch for #2250031: Better pagination support

pbuyle’s picture

Status: Active » Needs review
pbuyle’s picture

Category: Bug report » Feature request

A missing feature is not a bug, reverting issue category.

pbuyle’s picture

Reroll patch to work after latest patch in #2250031: Better pagination support

pbuyle’s picture

tariqinam’s picture

I have applied provided patch, but it doesnt show any diff on "git diff"
I am on version 7.x-1.0

pbuyle’s picture

Did you apply the patch from #2250031? The patch is made for the 7.x-1.x branch.

tariqinam’s picture

I tried applying from #2250031 but it failed for .info file and new handler file. Any way I manually updated that bit. works like charm.
One thing to mention, if api url contain a port number, then it doesn't work. I added that bit just before creating final url in views_json_query_plugin_query_json.inc

// Rebuild the JSON file URL.
    $port = '';
    if(isset($parsed['port'])) {
	$port = ':' . $parsed['port'];
    }

    $view->query->options['json_file'] = "{$parsed['scheme']}://{$parsed['host']}{$port}{$parsed['path']}?" . 	      drupal_http_build_query($query_string);
    $view->build_info['query'] = $this->query();
    $view->build_info['query_args'] = array();
pbuyle’s picture

Thanks @tariqinam, the port does indeed needs handling. But this is actually an issue for the patch in #2250031: Better pagination support (I known, multiple patches are confusing and make it hard to work, but I try to keep feature separated, so pagination support can be added even if the architecture of this solution to this issue is still discussed).

tariqinam’s picture

thanks @mongolito404 , I got one more question. I have an exposed filter (query_string), And results only appear when I pass some value to it. How can I load with a default value.

My second question: Does it work with views page? because for me, it only work with view's block.
might possible if I am doing something wrong.

pbuyle’s picture

I should work with Views page. I don;t think default value are supported in my patch.

acarist’s picture

patch steps not true. error : warning: views_json_query.info has type 100644, expected 100755. i patched manual. please add a patched version.

pbuyle’s picture

Patch apply fine when used with Drush make:

projects[views_json_query][version] = 1.x-dev
projects[views_json_query][download][type] = "git"
projects[views_json_query][patch][2246731] = "http://drupal.org/files/issues/views_json_query-filter-causes-general-error-2246731-1.patch"
projects[views_json_query][patch][2250031] = "http://drupal.org/files/issues/better_pagination_support-2250031-3.patch"
projects[views_json_query][patch][2250191] = "http://drupal.org/files/issues/views_json_query-filter-in-query-string-2250191-5.patch"

Try applying the patch for #2246731: Applying Filter Criteria Generates Fatal Error and #2250191: Filter and contextual filter in JSON file URL query string first.

khalor’s picture

Status: Needs review » Patch (to be ported)

Doesn't apply here either, even after the first 2 patches go through cleanly. It's only the .info file that fails though, everything else goes through.

After manually patching the .info (just those 2 lines to include the new handlers) this works brilliantly, so could skip re-rolling the patch and call this RTBC? Get it committed after #2250031: Better pagination support is resolved?

morganl’s picture

I've applied the patches in the order mentioned, and I believe I have everything in place, but when I try to add a new "Json: Query String" contextual filter, this error comes up:

Configure contextual filter: Broken/missing handler
The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.

Could someone please post a patched working copy of this module? (Or roll this into a new Dev release?) I'd really like to use this functionality.
Thanks!

morganl’s picture

StatusFileSize
new18.78 KB

I was eventually able to get it working using this Drush make file:

api = 2
core = 7.36
projects[views_json_query][type] = core
projects[views_json_query][version] = 1.x-dev
projects[views_json_query][download][type] = "git"
projects[views_json_query][patch][2246731] = "http://drupal.org/files/issues/views_json_query-filter-causes-general-error-2246731-1.patch"
projects[views_json_query][patch][2250031] = "http://drupal.org/files/issues/better_pagination_support-2250031-3.patch"
projects[views_json_query][patch][2250191] = "http://drupal.org/files/issues/views_json_query-filter-in-query-string-2250191-5.patch"

Attached is a zip of the fully-patch module for anyone else having trouble with this. Be sure to clear your caches after updating.

cprofessionals’s picture

First thanks for a great module and the updates/new features in the dev release. I see the new feature for applying a filter and giving that parameter a name. I am missing the syntax for referencing the parameter in the "The URL or path to the Json file." Thanks in advance.

cprofessionals’s picture

The dev release does not seem to have an updated date on the main project page and still reads "2013-Oct-19" FYI

morganl’s picture

CProfessionals, the format is http://www.example.com/endpoint_or_filename_here

The dev release hasn't been updated in a long time. I'm not sure if/when anyone is working on rolling these patches into the dev release.

morganl’s picture

Status: Patch (to be ported) » Needs review
cprofessionals’s picture

Thanks for the reply. Any simple instructions for my question on #18. How do I reference a parameter in the json query string.

pbuyle’s picture

The patch in #2 allow you to define a query parameter for the JSON file URL as a view filter. When doing so, there is no need to set the query parameter in the "URL or path to the Json file" field. The query parameters added as views filters will be added to URL automatically.

cprofessionals’s picture

I just verified. Brilliant! TOO SIMPLE. thanks for the reply.

I was using a different name in for the filter. Just in case, I want to spell this out for others that may come. If you have a json qwuery that looks like this "http://www.somejsonservice.com/?auth_token=XXXXXXXXXXXXXXXXXXXXXXXXXX&pa... you can replace any of the parameters with with a filter in views. just add a filter and make sure its name matches the parameter you want to make dynamic within views. Then remove the parameter from the query string. This module automatically adds it to the query string. so now it would read: "http://www.somejsonservice.com/?auth_token=XXXXXXXXXXXXXXXXXXXXXXXXXX" and I would have two filters in the view called "heading" and "sort".

Hope this helps someone. and thanks for the great module.

pbuyle’s picture

For authorization token, you may want to not store it in the view definition, but rather in a configuration variable. Tis is easily doable with te following snippet.

/**
 * Implements hook_views_pre_execute().
 */
function MODULE_views_pre_execute(&$view) {
  if ($view->base_table == 'json') {
    if (strpos($view->query->options['json_file'], 'http://www.somejsonservice.com') === 0) {
      $parsed = parse_url($view->query->options['json_file']);
      $query_string = drupal_get_query_array($parsed['query']);
      $query_string['auth_token'] = variable_get('some_json_service_auth_token', NULL);
      $view->query->options['json_file'] = "{$parsed['scheme']}://{$parsed['host']}{$parsed['path']}?" . drupal_http_build_query($query_string);
    }
  }
}
cprofessionals’s picture

Thanks, or you could put it in as a hidden filter, right?

pbuyle’s picture

Yes you could put it as an hidden filter, but then it won't be easy to store it in a configuration variable.

discipolo’s picture

StatusFileSize
new13.39 KB

grouped filters werent working as expected. i added this line to the previous patch to make the filters work for me:

        // set the filter value for grouped filters
        $filter->options['value'] = $filter->value;
discipolo’s picture

StatusFileSize
new6.81 KB

the patch from #28 also includes all the other patches mentioned in #17

this one here is a reroll of #5 with the oneline addition to set the filter value for grouped filters

discipolo’s picture

StatusFileSize
new8.52 KB

adding grouping functionality to query string filters seems to work fine

discipolo’s picture

Title: Filter and contextuel filter in JSON file URL query string » Filter and contextual filter in JSON file URL query string
Issue summary: View changes
StatusFileSize
new9.34 KB

with the other patch applied for convienience

discipolo’s picture

StatusFileSize
new8.18 KB

and here is the real one

discipolo’s picture

StatusFileSize
new9.3 KB

had another look at the related issue which hasnt been updated in a year and decided to stick everything in here.

discipolo’s picture

StatusFileSize
new13.64 KB

removing whitespace

discipolo’s picture

StatusFileSize
new14.68 KB

newer version of patch allows "not empty" condition in grouped exposed filters

skyriter’s picture

I tried patching with filter_and_contextual-2250191-35.patch and it failed for me:

patching file handlers/views_json_query_handler_argument_query_string.inc
patching file handlers/views_json_query_handler_filter.inc
patching file handlers/views_json_query_handler_filter_query_string.inc
patching file views_json_query.info
Hunk #1 FAILED at 8.
1 out of 1 hunk FAILED -- saving rejects to file views_json_query.info.rej
patching file views_json_query.views.inc
patching file views_json_query_plugin_query_json.inc
Hunk #4 succeeded at 226 (offset -2 lines).
Hunk #5 succeeded at 266 (offset -2 lines).
Hunk #6 succeeded at 290 (offset -2 lines).
Hunk #7 succeeded at 325 (offset -2 lines).
Hunk #8 succeeded at 351 (offset -2 lines).
Hunk #9 succeeded at 365 (offset -2 lines).
Hunk #10 succeeded at 392 (offset -2 lines).

Here's what was in views_json_query.info.rej

--- views_json_query.info
+++ views_json_query.info
@@ -8,4 +8,7 @@
 ; Handlers
 files[] = handlers/views_json_query_handler_field.inc
 files[] = handlers/views_json_query_handler_filter.inc
+files[] = handlers/views_json_query_handler_filter_query_string.inc
 files[] = handlers/views_json_query_handler_sort.inc
+files[] = handlers/views_json_query_handler_argument_query_string.inc
+

Has anyone been able to patch with the most recent one version?

skyriter’s picture

This make file worked for me:

api = 2
core = 7.x
projects[views_json_query][version] = 1.x-dev
projects[views_json_query][download][type] = "git"
projects[views_json_query][type] = module
projects[views_json_query][subdir] = contrib
projects[views_json_query][patch][2250191] = https://www.drupal.org/files/issues/filter_and_contextual-2250191-35.patch

Keep in mind, we keep our contributed modules in a contrib/ folder to easily distinguish them for custom modules, which we keep in a custom/ folder.

skyriter’s picture

I'm wondering how I can use a query string with this patch, or can I?

I can use mysite.com/view-url/value1 to only bring back a JSON query string which has value1 as a value of the key:value pair, but I can't figure out how to grab mysite.com/view-url/value2 without having /value1/value2 in a specific order. This also means I can't just use any query key and value or mix up their order. Any thoughts?

pbuyle’s picture

@omahane: The patch allow usage of the view filters in the query string. Given the base URL https://example.com/api/data, you can use it to build a URL that looks like https://example.com/api/data?foo=value1 where 'foo' is a filter with the value is 'value1'. You can't use to push the value of a filter in the path part of the URL and build an URL like https://example.com/api/data/value1.

The patch also add support for contextual filter (aka. argument). If I remember correctly, these can be used in the path of the queried URL, the same they can be used in the path of a transitional view: using %n in the URL. So given the https://example.com/api/data/%1 base URL, you can use it to build https://example.com/api/data/value1 as queried URL.

agerard’s picture

I've just installed and started using Views Json Query in a test cloud install of D7 - not sure if the configuration is part of the problem but I wasn't able to use 'filter criteria' or 'relationships' settings in my test view using json (I got ajax errors, after which the specific block or page becomes unusable). I found this issue and was hoping patches herein might help me do what seems necessary, which is to supply 'filtering' parameters to the url string - I was able to apply patches to the dev module (one snippet manually) and now 'filter criteria' and 'relationships' work and I'm able to add parameters.

Very cool - thanks for this!

sunnygambino’s picture

Yes, confirmed it is working cool!
You have to patch the info file manually and add two extra files. Check patch error file.

marcofernandes’s picture

Status: Needs review » Fixed

Merged manually to dev branch because I already applied #2250031 patch. Looks ok.

jbfelix’s picture

Hello,

I try to build the view for this:

https://remoteapi.com/webservice/api/apikey/XXXXXXXXX/contextual_filter/%/format/json

I would like to embed the view in a node template where the "contextual_filter" would be provided as a field's token.

My problem is that the requested API accept this:
https://remoteapi.com/webservice/api/apikey/XXXXXXXXX/format/json/contextual_filter/xx

But not this format:
https://remoteapi.com/webservice/api/apikey/XXXXXXXXX/format/json?contextual_filter=xx

How to achieve this or is it possible to rewrite ?contextual_filter=xx to /contextual_filter/xx

Thanks

jukka792’s picture

Hi,

I have a JSON API URL: https://remoteapi.com/webservice/?filter_name_a=xx
What is the exact value which should be in the "Json file field"?
I have tried: "https://remoteapi.com/webservice/" etc. but nothing seems to work so that the contextual filter would be included.

Created a contextual filter with a administrative name "filter_name_a".

Can't get this working, the "Json file field" does not get the contextual filter from the views.

What am I doing wrong?

Status: Fixed » Closed (fixed)

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