* @author Timo Hund */ class DocumentScoreAnalyzerViewHelper extends AbstractMeilisearchFrontendViewHelper { use CompileWithRenderStatic; /** * @var ScoreCalculationService */ protected static $scoreService; /** * @var bool */ protected $escapeOutput = false; /** * Initializes the arguments */ public function initializeArguments() { parent::initializeArguments(); $this->registerArgument('document', SearchResult::class, 'The meilisearch document', true); } /** * @param array $arguments * @param \Closure $renderChildrenClosure * @param RenderingContextInterface $renderingContext * @return string */ public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) { $content = ''; // only check whether a BE user is logged in, don't need to check // for enabled score analysis as we wouldn't be here if it was disabled $backendUserIsLoggedIn = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('backend.user', 'isLoggedIn'); if ($backendUserIsLoggedIn === false) { return $content; } $document = $arguments['document']; /** @var $resultSet SearchResultSet */ $resultSet = self::getUsedSearchResultSetFromRenderingContext($renderingContext); $debugData = $resultSet->getUsedSearch()->getDebugResponse()->explain->{$document->getId()}; /** @var $scoreService ScoreCalculationService */ $scoreService = self::getScoreService(); $queryFields = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchQueryQueryFields(); $content = $scoreService->getRenderedScores($debugData, $queryFields); return '
' . $content . '
'; } /** * @return ScoreCalculationService */ protected static function getScoreService() { if (isset(self::$scoreService)) { return self::$scoreService; } self::$scoreService = GeneralUtility::makeInstance(ScoreCalculationService::class); return self::$scoreService; } }