I need to get the currentSite in Element API. How can this be achieved? Is it as a slug in the endpoint URL? or as a Query param? Here is the current code:
<?php
use craft\elements\Entry;
use craft\helpers\UrlHelper;
use craft\services\Sites;
return [
'defaults' => [
'resourceKey' => 'suggestions',
'cache' => false,
'paginate' => false,
'pretty' => true,
],
'endpoints' => [
'api/search.json' => function() {
$searchQuery = Craft::$app->request->getQueryParam('query');
$site = Craft::$app->request->getQueryParam('site');
$sectionHandles = [
'pages',
'news',
];
// Search criterias
$criteria = [
'section' => $sectionHandles,
'search' => $searchQuery,
'site' => $site,
'orderBy' => 'score',
'limit' => 10,
];
return [
'elementType' => Entry::class,
'criteria' => $criteria,
'paginate' => false,
'transformer' => function(craft\elements\Entry $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
];
},
];
},
]
];