From dbf3dac2f0e727c7bbeac250b8beb45e68193bc3 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Thu, 17 Aug 2023 16:07:36 +0200 Subject: [PATCH] Filter on enabled search documentable --- src/Controller/SearchController.php | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index e214057..49e5f0c 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -82,12 +82,8 @@ public function searchAction( ); $result = $this->search->search($requestConfiguration); - $documentableRegistries = array_filter($this->documentableRegistry->all(), function (DocumentableInterface $documentable) { - return (bool) $this->searchSettings->getCurrentValue($this->channelContext->getChannel(), null, 'search_enabled__' . $documentable->getIndexCode()); - }); - return $this->render('@MonsieurBizSyliusSearchPlugin/Search/result.html.twig', [ - 'documentableRegistries' => $documentableRegistries, + 'documentableRegistries' => $this->getSearchEnabledDocumentables(), 'documentable' => $result->getDocumentable(), 'requestConfiguration' => $requestConfiguration, 'query' => urldecode($query), @@ -120,11 +116,7 @@ public function instantAction(Request $request): Response { $results = []; /** @var DocumentableInterface $documentable */ - foreach ($this->documentableRegistry->all() as $documentable) { - if (!(bool) $this->searchSettings->getCurrentValue($this->channelContext->getChannel(), null, 'instant_search_enabled__' . $documentable->getIndexCode())) { - continue; - } - + foreach ($this->getInstantSearchEnabledDocumentables() as $documentable) { $requestConfiguration = new RequestConfiguration( $request, RequestInterface::INSTANT_TYPE, @@ -170,7 +162,7 @@ public function taxonAction( private function getDocumentable(?string $documentType): DocumentableInterface { if (null === $documentType) { - $documentables = $this->documentableRegistry->all(); + $documentables = $this->getSearchEnabledDocumentables(); return reset($documentables); } @@ -182,4 +174,18 @@ private function getDocumentable(?string $documentType): DocumentableInterface throw new NotFoundHttpException(sprintf('Documentable "%s" not found', $documentType)); } } + + private function getSearchEnabledDocumentables(): array + { + return array_filter($this->documentableRegistry->all(), function (DocumentableInterface $documentable) { + return (bool) $this->searchSettings->getCurrentValue($this->channelContext->getChannel(), null, 'search_enabled__' . $documentable->getIndexCode()); + }); + } + + private function getInstantSearchEnabledDocumentables(): array + { + return array_filter($this->documentableRegistry->all(), function (DocumentableInterface $documentable) { + return (bool) $this->searchSettings->getCurrentValue($this->channelContext->getChannel(), null, 'instant_search_enabled__' . $documentable->getIndexCode()); + }); + } }