Skip to content

Commit c33f193

Browse files
author
arnoson
committed
feat: provide hit and formatted attributes per page
1 parent eab037c commit c33f193

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/KirbyLoupeCollection.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,40 @@
33
namespace arnoson;
44

55
use Kirby\Cms\Collection;
6+
use Kirby\Cms\Page;
67
use Kirby\Cms\Pagination;
78
use Loupe\Loupe\SearchResult;
89

910
class KirbyLoupeCollection extends Collection {
11+
private array $hitsByUuid = [];
12+
1013
public function __construct(protected SearchResult $searchResult) {
1114
parent::__construct();
1215
foreach ($searchResult->getHits() as $hit) {
13-
$this->append(page($hit["uuid"]));
16+
$uuid = $hit["uuid"];
17+
$this->hitsByUuid[$uuid] = $hit;
18+
$this->append(page($uuid));
1419
}
1520
}
1621

1722
public function searchResult() {
1823
return $this->searchResult;
1924
}
2025

26+
/**
27+
* Get the raw search hit from Loupe for the specified page.
28+
*/
29+
public function hit(Page $page) {
30+
return $this->hitsByUuid[$page->uuid()->toString()];
31+
}
32+
33+
/**
34+
* Get the formatted attributes from Loupe for the specified page.
35+
*/
36+
public function formatted(Page $page) {
37+
return $this->hitsByUuid[$page->uuid()->toString()];
38+
}
39+
2140
public function paginate(...$arguments) {
2241
// The original Collection class uses Pagination::for() which will
2342
// automatically set the total parameter based on the collection length

0 commit comments

Comments
 (0)