From ab4e99bb187fb4273dcad2fa3c731ba46e49a585 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 13 Nov 2021 12:44:27 +0000 Subject: [PATCH] Added name highlighting in search results --- app/Entities/Tools/SearchResultsFormatter.php | 14 ++++++++++---- resources/views/entities/list-item-basic.blade.php | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Entities/Tools/SearchResultsFormatter.php b/app/Entities/Tools/SearchResultsFormatter.php index aaa5c129d..24dc820a4 100644 --- a/app/Entities/Tools/SearchResultsFormatter.php +++ b/app/Entities/Tools/SearchResultsFormatter.php @@ -30,11 +30,17 @@ class SearchResultsFormatter $textContent = $entity->$textProperty; $terms = array_merge($options->exacts, $options->searches); - $matchRefs = $this->getMatchPositions($textContent, $terms); - $mergedRefs = $this->sortAndMergeMatchPositions($matchRefs); - $content = $this->formatTextUsingMatchPositions($mergedRefs, $textContent); + $originalContentByNewAttribute = [ + 'preview_name' => $entity->name, + 'preview_content' => $textContent, + ]; - $entity->setAttribute('preview_content', new HtmlString($content)); + foreach ($originalContentByNewAttribute as $attributeName => $content) { + $matchRefs = $this->getMatchPositions($content, $terms); + $mergedRefs = $this->sortAndMergeMatchPositions($matchRefs); + $formatted = $this->formatTextUsingMatchPositions($mergedRefs, $content); + $entity->setAttribute($attributeName, new HtmlString($formatted)); + } } /** diff --git a/resources/views/entities/list-item-basic.blade.php b/resources/views/entities/list-item-basic.blade.php index 2ec4bee5c..398c33b93 100644 --- a/resources/views/entities/list-item-basic.blade.php +++ b/resources/views/entities/list-item-basic.blade.php @@ -2,7 +2,7 @@ @icon($type)
-

{{ $entity->name }}

+

{{ $entity->preview_name ?? $entity->name }}

{{ $slot ?? '' }}
\ No newline at end of file