Add Index Listener

This commit is contained in:
Jürgen Mummert
2025-12-22 21:34:11 +01:00
parent f41357bee5
commit ac1db3ff9a
3 changed files with 53 additions and 21 deletions
@@ -0,0 +1,32 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\CalendarEventsModel;
use Contao\Template;
class MeilisearchEventContextListener
{
public function onParseFrontendTemplate(string $buffer, string $template): string
{
// Nur Event-Reader-Modul
if ($template !== 'mod_eventreader') {
return $buffer;
}
// Event kommt IM Template an
if (
isset($GLOBALS['objEvent']) &&
$GLOBALS['objEvent'] instanceof CalendarEventsModel
) {
$eventId = (int) $GLOBALS['objEvent']->id;
// Minimaler, stabiler Marker
$marker = "\n<!-- MEILI_EVENT id={$eventId} -->\n";
return $buffer . $marker;
}
return $buffer;
}
}
@@ -16,12 +16,10 @@ class MeilisearchPageMarkerListener
} }
$lines = ['MEILISEARCH']; $lines = ['MEILISEARCH'];
// =========================
// PAGE
// =========================
$page = $GLOBALS['objPage']; $page = $GLOBALS['objPage'];
/* ================= PAGE ================= */
if ((int) $page->priority > 0) { if ((int) $page->priority > 0) {
$lines[] = 'page.priority=' . (int) $page->priority; $lines[] = 'page.priority=' . (int) $page->priority;
} }
@@ -30,7 +28,6 @@ class MeilisearchPageMarkerListener
$lines[] = 'page.keywords=' . trim((string) $page->keywords); $lines[] = 'page.keywords=' . trim((string) $page->keywords);
} }
// searchimage (Page → Fallback)
$searchImageUuid = null; $searchImageUuid = null;
if (!empty($page->searchimage)) { if (!empty($page->searchimage)) {
@@ -43,15 +40,13 @@ class MeilisearchPageMarkerListener
$lines[] = 'page.searchimage=' . $searchImageUuid; $lines[] = 'page.searchimage=' . $searchImageUuid;
} }
// ========================= /* ================= EVENT ================= */
// EVENT (Detailseite!)
// =========================
if (
isset($GLOBALS['objEvent']) &&
$GLOBALS['objEvent'] instanceof CalendarEventsModel
) {
$event = $GLOBALS['objEvent'];
if (preg_match('/<!--\s*MEILI_EVENT id=(\d+)\s*-->/', $buffer, $m)) {
$eventId = (int) $m[1];
$event = CalendarEventsModel::findByPk($eventId);
if ($event !== null) {
if ((int) $event->priority > 0) { if ((int) $event->priority > 0) {
$lines[] = 'event.priority=' . (int) $event->priority; $lines[] = 'event.priority=' . (int) $event->priority;
} }
@@ -60,10 +55,10 @@ class MeilisearchPageMarkerListener
$lines[] = 'event.keywords=' . trim((string) $event->keywords); $lines[] = 'event.keywords=' . trim((string) $event->keywords);
} }
} }
}
/* ================= OUTPUT ================= */
// =========================
// OUTPUT
// =========================
if (count($lines) === 1) { if (count($lines) === 1) {
return $buffer; return $buffer;
} }
+5
View File
@@ -2,6 +2,7 @@
use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener; use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener;
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener; use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchEventContextListener;
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [ $GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
@@ -14,6 +15,10 @@ $GLOBALS['TL_HOOKS']['indexPage'][] = [
'onIndexPage' 'onIndexPage'
]; ];
$GLOBALS['TL_HOOKS']['parseFrontendTemplate'][] = [
MeilisearchEventContextListener::class,
'onParseFrontendTemplate',
];
if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) { if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) {
$GLOBALS['MEILISEARCH_MARKERS'] = []; $GLOBALS['MEILISEARCH_MARKERS'] = [];