3

I have a custom content type called 'Regions', which simply has a title field. Example values are: Asia, North America, Ocenia, etc.

I've set up a view for this list and a REST export for it, so /regions gives me a simple list:

[{"title":"Asia"},{"title":"North America"},{"title":"South America"},{"title":"Ocenia"}]

I want to filter the results, so I added a contextual filter on the title field, making /regions/Asia show only Asia:

[{"title":"Asia"}]

How do I implement a wildcard search? Something like as should match Asia and amer should match both North America and South America. I'd like to do something like /regions?title=Asia

I referred to this guide on setting up contextual filters, but the following doesn't seem to work for me:

This query works as a SQL LIKE statement so you can send just a piece of the name and Views will try to find all records LIKE that argument, example, I have a brand called Land Rover but if I just send Land as an argument it will work http://dev-cars-api.pantheonsite.io/api/cars/Land

The URL in the quote is no longer online, so I can't check if it actually works. For me, only entering a full, matching item title works. I can't even specify a title with spaces, so /regions/North+America is also blank for me.

My Drupal version is 8.4, running on PHP 7.1.

1 Answer 1

0

The solution is to add a search filter to the view.

In the view settings, under Filter Criteria, you'll see Content: Published (= Yes) and Content: Content type (= Regions) (or whatever your view is for).

  1. Click add. In that box that shows all the fields you can add, type Search: Search Keywords.
  2. Tick Expose this filter to visitors, to allow them to change it.
  3. (optional) Rename the Filter Identifier from keys to query or terms or whatever you see fit.
  4. Save the view.

You'll then be able to make a GET http://example.com/api/regions/?_format=json&keys=my+search+string!

Your Answer

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

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