<p>It should work the same way because the <code>modifyElementsQuery</code> should be fired no matter where the <code>ElementQuery</code> object is build so you could insert the same array in your element-api endpoint as you insert in your <code>CraftVariables</code> <code>entries</code> function to  build the <a href="https://github.com/craftcms/cms/blob/develop/src/elements/db/EntryQuery.php" rel="nofollow noreferrer">EntryQuery</a> since they both use <a href="https://github.com/yiisoft/yii2/blob/master/framework/BaseYii.php#L543" rel="nofollow noreferrer">Yiis configure</a> function to build the </p>

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

<p>should theoretically build the same object like </p>

<pre><code>{% set entries = craft.entries.map({
    location: 'M2 5ND',
    country: 'GB',
    radius: 100,
    unit: 'mi'
}).orderBy('distance') %}
</code></pre>

CC BY-SA 3.0
2018-02-28T15:45:49.947Z
2018-02-28T16:00:31.650Z
2018-02-28T16:00:31.650Z
Robin Schambach
<p>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).</p>

<p>Simple Map provides a custom field that uses the Google Maps API for address data and geolocation.</p>

<p>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?</p>

<p>I'm using Craft 3:</p>

<pre><code>"require": {
    "php": "&gt;=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"
},
</code></pre>

<p><strong>element-api</strong>
<a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">https://github.com/craftcms/element-api</a></p>

<p><strong>simple map</strong> <a href="https://github.com/ethercreative/simplemap" rel="nofollow noreferrer">https://github.com/ethercreative/simplemap</a></p>

<p>The below will return entries that are within 100 miles of the location provided:</p>

<pre><code>{% set entries = craft.entries.map({
    location: 'M2 5ND',
    country: 'GB',
    radius: 100,
    unit: 'mi'
}).orderBy('distance').all() %}
</code></pre>

<p>.. and the simple look up in my element-api.php file currently returns all 'customer' entries:</p>

<pre><code>&lt;?php

namespace Craft;

use craft\elements\Entry;

// https://github.com/ethercreative/simplemap
// https://github.com/craftcms/element-api

return [
    'defaults' =&gt; [
        'elementType' =&gt; Entry::class,
        'elementsPerPage' =&gt; 9,
        'pageParam' =&gt; 'pg',
        'resourceKey' =&gt; 'entries',
        'transformer' =&gt; function(Entry $entry) {
            return [
                'id' =&gt; $entry-&gt;id,
                'title' =&gt; $entry-&gt;title,
                'url' =&gt; $entry-&gt;url,
                'map' =&gt; $entry-&gt;map,
            ];
        },
    ],
    'endpoints' =&gt; [
        'api/customer-search.json' =&gt; [
            'criteria' =&gt; [
                'section' =&gt; 'customers',
                'type' =&gt; 'customer',
                // how to include map({}) filtering of results?
            ],
        ],
    ]
];
</code></pre>

<p>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: <a href="https://craftcms.com/docs/templating/craft.entries" rel="nofollow noreferrer">https://craftcms.com/docs/templating/craft.entries</a></p>

<p>Thanks in advance for any direction you can provide.</p>

<php><plugins>
Craft CMS combining element-api and Simple Map plugin
# Craft CMS combining element-api and Simple Map plugin

(Asked by: username_0 on 2018-01-10)

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:
 [
        '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.

**Tags:** php, plugins


---

## 1 answers

### Answer by username_1 on 2018-02-28. Score: 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') %}


---

