Created
April 18, 2024 14:46
-
-
Save lukaskleinschmidt/cf97ebff8901053df2b085db6d28c7e2 to your computer and use it in GitHub Desktop.
Kirby 4 query pages from a searchengine for the pages field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fields: | |
articles: | |
type: pages | |
query: kirby.collection('articles') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// site/collections/articles.php | |
use Kirby\Cms\Pages; | |
use Kirby\Cms\Pagination; | |
return function () | |
{ | |
return new class extends Pages | |
{ | |
public ?string $query = null; | |
public function search(string $query = null, string|array $params = []): static | |
{ | |
$pages = new static; | |
$pages->query = $query; | |
return $pages; | |
} | |
public function paginate(...$arguments) | |
{ | |
$result = meilisearch('pages')->search($this->query, [ | |
'hitsPerPage' => intval($arguments[0]['limit']) ?: 20, | |
'page' => intval($arguments[0]['page']) ?: 1, | |
'filter' => [ | |
'status = listed', | |
'template = article', | |
], | |
]); | |
$this->pagination = new Pagination([ | |
'limit' => $result->getHitsPerPage(), | |
'total' => $result->getTotalHits(), | |
'page' => $result->getPage(), | |
]); | |
$this->data = array_map(function (array $item) { | |
return page('page://' . $item['uuid']); | |
}, $result->getHits()); | |
return $this; | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment