Add Index Listener

This commit is contained in:
Jürgen Mummert
2025-12-22 17:01:14 +01:00
parent 99d974af17
commit 3d491b5a0f
+15 -28
View File
@@ -4,55 +4,42 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\CoreBundle\ServiceAnnotation\Hook; use Contao\CoreBundle\ServiceAnnotation\Hook;
use MummertMedia\ContaoMeilisearchBundle\Service\SearchDataProvider; use MummertMedia\ContaoMeilisearchBundle\Service\SearchDataProvider;
use Psr\Log\LoggerInterface;
/**
* IndexPage Listener for Contao search index
*/
class IndexPageListener class IndexPageListener
{ {
public function __construct( public function __construct(
private readonly SearchDataProvider $dataProvider private readonly SearchDataProvider $dataProvider,
private readonly LoggerInterface $logger
) {} ) {}
/** /**
* This hook is executed for every indexed document
*
* @Hook("indexPage") * @Hook("indexPage")
*/ */
public function onIndexPage(string $content, array &$data, array &$set): void public function onIndexPage(string $content, array &$data, array &$set): void
{ {
// --------------------------------------------------------------------- // Log into Symfony / Monolog
// DEBUG: prove that the hook is executed (CLI + Frontend safe) $this->logger->info('[MEILI] onIndexPage fired', [
// --------------------------------------------------------------------- 'type' => $set['type'] ?? null,
error_log('[MEILI] onIndexPage fired'); 'set' => $set,
]);
// log basic context
error_log('[MEILI] type=' . ($set['type'] ?? 'NULL'));
error_log('[MEILI] set=' . json_encode($set, JSON_UNESCAPED_SLASHES));
// ---------------------------------------------------------------------
// Fetch search data (priority + keywords)
// ---------------------------------------------------------------------
$searchData = $this->dataProvider->getSearchData($set); $searchData = $this->dataProvider->getSearchData($set);
// log provider result $this->logger->info('[MEILI] provider result', [
error_log('[MEILI] provider result=' . json_encode($searchData)); 'result' => $searchData,
]);
if ($searchData === null) { if ($searchData === null) {
error_log('[MEILI] no search data resolved');
return; return;
} }
// ---------------------------------------------------------------------
// Write into tl_search (this array IS the DB record)
// ---------------------------------------------------------------------
$data['priority'] = (int) $searchData['priority']; $data['priority'] = (int) $searchData['priority'];
$data['keywords'] = (string) $searchData['keywords']; $data['keywords'] = (string) $searchData['keywords'];
// final confirmation $this->logger->info('[MEILI] tl_search updated', [
error_log('[MEILI] tl_search updated: priority=' 'priority' => $data['priority'],
. $data['priority'] 'keywords' => $data['keywords'],
. ' keywords="' . $data['keywords'] . '"' ]);
);
} }
} }