<p>Finally figured this out.
By using the variable slug in the endpoint URL I was able to listen for the category in the currently viewed category slug and pull the data i needed for my google map. The trick is to typomake the end point a function - i was trying to set the criteria outside of the endpoint which is, in retrospect, silly.</p>

<pre><code>'locations/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {

                        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
                        $criteria-&gt;slug = $slug;
                        $category = $criteria-&gt;first();
                        return [
                            'elementType' =&gt; 'Entry',
                            'criteria' =&gt; [
                                'section' =&gt; 'directory',
                                'relatedTo' =&gt; ['targetElement' =&gt; $category],
                                'limit' =&gt; null,
                            ],
                            'transformer' =&gt; function(EntryModel $entry) {
                                return [
                                    'title' =&gt; $entry-&gt;title,
                                    'mapinfo' =&gt; $entry-&gt;mapaddress,
                                    'url' =&gt; $entry-&gt;slug
                                ];
                            },
                        ];
                    },
</code></pre>

CC BY-SA 3.0
2016-07-21T21:19:21.000Z
2016-07-21T21:19:21.000Z
joomkit
<p>How can I set the criteria for an endpoint based on the current category view?</p>

<p>My <code>elementapi.php</code> looks like this where I can pass the slug of a category url to force a return of entries from the slug name.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;slug = "albania";
$category = $criteria-&gt;first();

return [
                'endpoints' =&gt; [
                    'directory.json' =&gt; [
                        'elementType' =&gt; 'Entry',
                        'criteria' =&gt; [
                                'section' =&gt; 'directory',
                                'limit' =&gt; null,
                            ],
                        'paginate' =&gt; false,
                        'transformer' =&gt; function(EntryModel $entry) {
                            return [
                                'title' =&gt; $entry-&gt;title,
                                'mapinfo' =&gt; $entry-&gt;mapaddress,
                                'url' =&gt; $entry-&gt;slug
                            ];
                        },
                    ],
                    'directory-category.json' =&gt; [
                        'elementType' =&gt; 'Entry',
                        'criteria' =&gt; [
                            'section' =&gt; 'directory',
                            'relatedTo' =&gt; ['targetElement' =&gt; $category],
                            'limit' =&gt; null,
                        ],
                        'paginate' =&gt; false,
                        'transformer' =&gt; function(EntryModel $entry) {
                            return [
                                'title' =&gt; $entry-&gt;title,
                                'mapinfo' =&gt; $entry-&gt;mapaddress,
                                'url' =&gt; $entry-&gt;slug
                            ];
                        },
                    ],
                    'seafarers-directory/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
                        return [
                            'elementType' =&gt; 'Entry',
                            'criteria' =&gt; ['id' =&gt; $entryId],
                            'first' =&gt; true,
                            'transformer' =&gt; function(EntryModel $entry) {
                                return [
                                    'title' =&gt; $entry-&gt;title,
                                    'url' =&gt; $entry-&gt;url,
                                    'body' =&gt; $entry-&gt;body,
                                    'mapinfo' =&gt; $entry-&gt;mapaddress
                                ];
                            },
                        ];
                    },
                ]
            ];
</code></pre>

<p>I tried setting the criteria category slug name with</p>

<pre><code>craft()-&gt;urlManager-&gt;getMatchedElement(); 
</code></pre>

<p>but it always returned nothing.</p>

<p>Can a dynamic category be set for the criteria from the existing url like the dynamic entry is?</p>

<plugin-elementapi>
How to set dynamic criteria for endpoint with the Element API plugin?
# How to set dynamic criteria for endpoint with the Element API plugin?

(Asked by: username_0 on 2016-07-20)

How can I set the criteria for an endpoint based on the current category view?
My elementapi.php looks like this where I can pass the slug of a category url to force a return of entries from the slug name.
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->slug = "albania";
$category = $criteria->first();

return [
                'endpoints' => [
                    'directory.json' => [
                        'elementType' => 'Entry',
                        'criteria' => [
                                'section' => 'directory',
                                'limit' => null,
                            ],
                        'paginate' => false,
                        'transformer' => function(EntryModel $entry) {
                            return [
                                'title' => $entry->title,
                                'mapinfo' => $entry->mapaddress,
                                'url' => $entry->slug
                            ];
                        },
                    ],
                    'directory-category.json' => [
                        'elementType' => 'Entry',
                        'criteria' => [
                            'section' => 'directory',
                            'relatedTo' => ['targetElement' => $category],
                            'limit' => null,
                        ],
                        'paginate' => false,
                        'transformer' => function(EntryModel $entry) {
                            return [
                                'title' => $entry->title,
                                'mapinfo' => $entry->mapaddress,
                                'url' => $entry->slug
                            ];
                        },
                    ],
                    'seafarers-directory/.json' => function($entryId) {
                        return [
                            'elementType' => 'Entry',
                            'criteria' => ['id' => $entryId],
                            'first' => true,
                            'transformer' => function(EntryModel $entry) {
                                return [
                                    'title' => $entry->title,
                                    'url' => $entry->url,
                                    'body' => $entry->body,
                                    'mapinfo' => $entry->mapaddress
                                ];
                            },
                        ];
                    },
                ]
            ];

I tried setting the criteria category slug name with
craft()->urlManager->getMatchedElement(); 

but it always returned nothing.
Can a dynamic category be set for the criteria from the existing url like the dynamic entry is?

**Tags:** plugin-elementapi

---

## 1 answers

### Answer by username_0 on 2016-07-21. Score: 2

Finally figured this out.
By using the variable slug in the endpoint URL I was able to listen for the category in the currently viewed category slug and pull the data i needed for my google map. The trick is to typomake the end point a function - i was trying to set the criteria outside of the endpoint which is, in retrospect, silly.
'locations/.json' => function($slug) {

                        $criteria = craft()->elements->getCriteria(ElementType::Category);
                        $criteria->slug = $slug;
                        $category = $criteria->first();
                        return [
                            'elementType' => 'Entry',
                            'criteria' => [
                                'section' => 'directory',
                                'relatedTo' => ['targetElement' => $category],
                                'limit' => null,
                            ],
                            'transformer' => function(EntryModel $entry) {
                                return [
                                    'title' => $entry->title,
                                    'mapinfo' => $entry->mapaddress,
                                    'url' => $entry->slug
                                ];
                            },
                        ];
                    },


---

