I've been using element-api for simple lazy-loading of things like blog posts, etc and now want to create a simple 'Postcode lookup' by utilising a custom filter (provided by the Simple Map plugin).
Simple Map provides a custom field that uses the Google Maps API for address data and geolocation.
My question is this: How would I call the custom Simple Map filter using the 'criteria' settings in element-api so that I can pass through a postcode (via a javascript call to the api/customer-search.json?postcode=M25ND) and return results based on the custom field filter?
I'm using Craft 3:
"require": {
"php": ">=7.0.0",
"craftcms/cms": "^3.0.0-RC1",
"vlucas/phpdotenv": "^2.4.0",
"roave/security-advisories": "dev-master",
"craftcms/redactor": "1.0.0.1",
"craftcms/element-api": "^2.5",
"ether/simplemap": "^3.1"
},
element-api https://github.com/craftcms/element-api
simple map https://github.com/ethercreative/simplemap
The below will return entries that are within 100 miles of the location provided:
{% set entries = craft.entries.map({
location: 'M2 5ND',
country: 'GB',
radius: 100,
unit: 'mi'
}).orderBy('distance').all() %}
.. and the simple look up in my element-api.php file currently returns all 'customer' entries:
<?php
namespace Craft;
use craft\elements\Entry;
// https://github.com/ethercreative/simplemap
// https://github.com/craftcms/element-api
return [
'defaults' => [
'elementType' => Entry::class,
'elementsPerPage' => 9,
'pageParam' => 'pg',
'resourceKey' => 'entries',
'transformer' => function(Entry $entry) {
return [
'id' => $entry->id,
'title' => $entry->title,
'url' => $entry->url,
'map' => $entry->map,
];
},
],
'endpoints' => [
'api/customer-search.json' => [
'criteria' => [
'section' => 'customers',
'type' => 'customer',
// how to include map({}) filtering of results?
],
],
]
];
What I'm struggling to do is combine the two. As far as I can tell; element-api criteria only works with the parameters listed here: https://craftcms.com/docs/templating/craft.entries
Thanks in advance for any direction you can provide.