11

I have a view that accepts a contextual filter in order to return the correct results. I'm using Services 3 and Drupal 7. I'm able to create an endpoint, and retrieve the view via services. But it seems like there is no way to pass the contextual filter, because services_views only has a retrieve method (which uses GET). Does anyone know if this is possible, and if so how it can be done?

14

pass your filter as follows: yourendpoint?args[0]=value&args[1]=value

Good luck

3

As long as the filters are exposed in the view, the fieldnames are available as contextual filters. For instance, expose NID in the filters list and you can do

GET /endpoint?nid=5
  • 1
    This solution works for exposed filter criteria. To the best of my knowledge contextual filters do not work, but this is an outstanding workaround. – Brent Feb 6 '13 at 2:28
3

This is an already answered question, but for others browsing here in the future, I would recommend using and taking a look at the documentation at Services Views. It spells out how to pass contextual filters along with a bunch of other possible arguments, and it has a demo video and shows examples.

  • 1
    After doing further research, it appears the this is not possible without modification of services_views. – user379468 Sep 28 '12 at 16:13
2

If you have to use multiple value in your contextual filter, I suggest doing the following :

  • Create your Contextual filter (in my case : nid)
  • Click on "Provide a default value" and set the type as PHP Contextual Filter expect a string with multiple value separated by a + or a comma.

return str_replace("|", "+", $_GET['nid']);

  • In the "More" section, check "Allow multiple values".

Now when calling your service, provide your contextual filters that way : .../my_service?nid=1|2|3

2

For those of you still having issues, ensure that when you add a Filter to the View, under the settings for the specified Field, there is a Filter identifier. This needs to be the argument passed into the querystring in the two formats listed on the Services Views documentation.

  • I was struggling on that, not intuitive at all since I imagined the title to be used as a filter key. Thank you! – donnadulcinea Oct 16 '15 at 9:58
1

I was able to get a contextual filter with Drupal Services by: 1) enabling the 'Views Contextual Filter Query' module (https://drupal.org/project/views_contextual_filter_query) 2) passing the contextual filter through the URL as a querystring.

In the contextual filter, you will have to select 'WHEN THE FILTER VALUE IS NOT AVAILABLE' -> 'Query Parameter from the URL. You will then be able to add the parameter name which you will use in the URL. This also allows you to use the + and | characters for selecting multiple filter criteria.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.