2

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.

2

It should work the same way because the modifyElementsQuery should be fired no matter where the ElementQuery object is build so you could insert the same array in your element-api endpoint as you insert in your CraftVariables entries function to build the EntryQuery since they both use Yiis configure function to build the

'endpoints' => [
    'api/customer-search.json' => [
        'criteria' => [
            'map' => [
                'location' => 'M2 5ND',
                'country' => 'GB',
                'radius' =>  100,
                'unit' => 'mi'
            ],
            'orderBy' => 'distance'
        ],
    ],
]

should theoretically build the same object like

{% set entries = craft.entries.map({
    location: 'M2 5ND',
    country: 'GB',
    radius: 100,
    unit: 'mi'
}).orderBy('distance') %}
| improve this answer | |

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.