0

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/<entryId:\d+>.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?

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/<slug:{slug}>.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
                                ];
                            },
                        ];
                    },
|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.