6

On my current setup I'm exposing a view that outputs json through a service.

The view can be called like this: http://domain.com/api/books.json.

I also exposed a filter to a referenced content type "Author". I'm NOT using context filters, I'm using the regular Filters and exposed that particular filter to the user, this way I can give my filter a nice argument name author_nid.

So now I can call this: http://domain.com/api/books.json?author_nid=38. And this returns all books of the author with that nid.

But I can't seem to figure out how to get the list of books of both author 38 or author 32.

The things i tried:

http://domain.com/api/books.json?author_nid=38,32

http://domain.com/api/books.json?author_nid=38+32

http://domain.com/api/books.json?author_nid[]=38,32

http://domain.com/api/books.json?author_nid[]=38+32

http://domain.com/api/books.json?author_nid[0]=38&author_nid[1]=32

I seem to have the feeling that this isn't supported right now. Am I correct? If so, what are my options? I'd like to give my arguments a name because I'm about to expose 10 to 15 filters (some optional, others required). Using context filters and filtering like this: args[]=2&args[]=43,23&args[]=44 isn't very readable/maintainable.

3

4 Answers 4

4
+50

If using ?author_nid[]=38&author_nid[]=32 as suggested by milkovsky doesn't work you may need to look at using contextual filters. Or, as I think might be your only option, use hook_views_query_alter where you can build your own views query WHERE clause based on $_GET parameters.

3
  • Please check the comment on milkovsky's answer. Sadly enough it's not working. Using context filters isn't really an option, check my initial question. Any references or examples how I should write such a hook? Never did this before :p
    – polyclick
    Feb 12, 2014 at 10:08
  • 1
    I commented a little bit too fast. I now see the link thirdender posted as a comment on my question. I'll try that first.
    – polyclick
    Feb 12, 2014 at 10:45
  • Using ?author_nid[]=38&author_nid[]=32 worked for me. This is the only documentation I could find on it too...
    – AlxVallejo
    Jul 30, 2021 at 12:14
0

Maybe it's just a problem with encoding. I've the same construction (views with multivalued field in exposed filter), and here is the result when I apply the submit button:

http://mywebsite.com/search-housing?field_rooms_value%5B%5D=1&field_rooms_value%5B%5D=2

Could you try this and tell me if it's working?

http://domain.com/api/books.json?author_nid%5B%5D=38&author_nid%5B%5D=32

1
  • Thanks for the reply but this isn't working.
    – polyclick
    Feb 17, 2014 at 12:56
0

I agree with Christian and MPD, there is no "is one of" option for NID. Christian's answer about hook_views_query_alter will work.

As alternative solution - you can replace filter handler with a custom one. Here is solution for search API query. Next handler will use "Allow multiple selections" option:

class MYMODULESearchApiViewsHandlerFilter extends SearchApiViewsHandlerFilter {

  /**
   * Extends query to handle multiple values.
   */
  public function query() {
    if (!empty($this->options['expose']['multiple'])) {
      $values = array();
      // ...
      // DO YOUR CODE HERE TO GET VALUES FROM $this->value
      // E.g. $values = explode(',', $this->value);
      // ...
      if (!empty($values)) {
        $or = $this->query->createFilter('OR');
        foreach ($values as $value) {
          $or->condition($this->real_field, $value, $this->operator, $this->options['group']);
        }
        $this->query->filter($or);
      }
    }
    else {
      parent::query();
    }
  }

}

You can add your handler using hook_views_data_alter().

/**
 * Implements hook_views_data_alter().
 */
function MYMODULE_views_data_alter(&$data) {
  $data['search_api_index_MYINDEX']['MYFIELD']['filter']['handler'] = 'MYMODULESearchApiViewsHandlerFilter';
}
0

You can pass multiple values using contextual filter,

Select Raw Value from Url and select PathComponent/arugument.

Below we have More section from that you can check Allow multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).

http://example.com/some-path/24+25

or

http://example.com/some-path/24,25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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