Skip to content

Commit aa0bdc3

Browse files
committed
fix: correct totalItems not reflecting the hits if filtered out by can() operations
1 parent 3addea3 commit aa0bdc3

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/Service/AlgoliaQuerier.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Wilr\SilverStripe\Algolia\Service;
44

5-
use Exception;
65
use Psr\Log\LoggerInterface;
76
use SilverStripe\Core\Injector\Injector;
87
use SilverStripe\ORM\ArrayList;
@@ -23,7 +22,7 @@ class AlgoliaQuerier
2322
* @param string $selectedIndex
2423
* @param string $query
2524
* @param array $searchParameters
26-
* @param array $ORMFilters filter ORM objects prior to returning the results as a PaginatedList
25+
* @param array $ORMFilters This argument is used to filter ORM objects prior to returning the results as a PaginatedList
2726
*
2827
* @return PaginatedList
2928
*/
@@ -55,13 +54,15 @@ function array_key_first(array $arr)
5554
}
5655

5756
$records = ArrayList::create();
57+
$totalItems = $results['nbHits'];
5858

5959
if ($results && isset($results['hits'])) {
6060
foreach ($results['hits'] as $hit) {
6161
$className = isset($hit['objectClassName']) ? $hit['objectClassName'] : null;
6262
$id = isset($hit['objectSilverstripeID']) ? $hit['objectSilverstripeID'] : null;
6363

6464
if (!$id || !$className) {
65+
$totalItems--;
6566
continue;
6667
}
6768

@@ -70,6 +71,8 @@ function array_key_first(array $arr)
7071

7172
if ($record && $record->canView()) {
7273
$records->push($record);
74+
} else {
75+
$totalItems--;
7376
}
7477
} catch (Throwable $e) {
7578
Injector::inst()->get(LoggerInterface::class)->notice($e);
@@ -87,15 +90,12 @@ function array_key_first(array $arr)
8790

8891
if ($results) {
8992
$output = $output->setCurrentPage($results['page'] + 1)
90-
->setTotalItems($results['nbHits'])
93+
->setTotalItems($totalItems)
9194
->setLimitItems(false)
9295
->setPageStart($results['page'] * $results['hitsPerPage'])
9396
->setPageLength($results['hitsPerPage']);
9497
}
9598

96-
// add raw output from algoia for manipulation
97-
$output->raw = $results;
98-
9999
return $output;
100100
}
101101

0 commit comments

Comments
 (0)