Add Index Listener

This commit is contained in:
Jürgen Mummert
2025-12-22 21:16:50 +01:00
parent 3948d20261
commit f41357bee5
4 changed files with 38 additions and 124 deletions
@@ -1,38 +0,0 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\CalendarEventsModel;
use Contao\Template;
class MeilisearchEventMarkerListener
{
public function onParseTemplate(Template $template): void
{
// 🔥 Event-Detailseite erkennen
if (
!isset($template->event) ||
!$template->event instanceof CalendarEventsModel
) {
return;
}
$event = $template->event;
$priority = (int) ($event->priority ?? 0);
$keywords = trim((string) ($event->keywords ?? ''));
if ($priority <= 0 && $keywords === '') {
return;
}
// ✅ Marker setzen (merge-sicher)
$GLOBALS['MEILISEARCH_MARKERS']['event'] = array_merge(
$GLOBALS['MEILISEARCH_MARKERS']['event'] ?? [],
[
'priority' => $priority > 0 ? $priority : null,
'keywords' => $keywords !== '' ? $keywords : null,
]
);
}
}
@@ -1,36 +0,0 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\NewsModel;
use Contao\Template;
class MeilisearchNewsMarkerListener
{
public function onParseTemplate(Template $template): void
{
// Nur News-Reader
if ($template->getName() !== 'mod_newsreader') {
return;
}
if (
!isset($GLOBALS['objArticle']) ||
!$GLOBALS['objArticle'] instanceof NewsModel
) {
return;
}
// 🔥 News vollständig laden (inkl. Custom-Felder)
$news = NewsModel::findByPk($GLOBALS['objArticle']->id);
if ($news === null) {
return;
}
$GLOBALS['MEILISEARCH_MARKERS']['news'] = [
'priority' => (int) ($news->priority ?? 0),
'keywords' => trim((string) ($news->keywords ?? '')),
];
}
}
@@ -2,6 +2,7 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\CalendarEventsModel;
use Contao\Config; use Contao\Config;
use Contao\PageModel; use Contao\PageModel;
use Contao\StringUtil; use Contao\StringUtil;
@@ -10,62 +11,59 @@ class MeilisearchPageMarkerListener
{ {
public function onOutputFrontendTemplate(string $buffer, string $template): string public function onOutputFrontendTemplate(string $buffer, string $template): string
{ {
// Nur im Frontend
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) { if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
return $buffer; return $buffer;
} }
// 🔹 Marker aus vorherigen Listenern übernehmen $lines = ['MEILISEARCH'];
$markers = $GLOBALS['MEILISEARCH_MARKERS'] ?? [];
// =========================
// PAGE
// =========================
$page = $GLOBALS['objPage']; $page = $GLOBALS['objPage'];
// --- Page-Daten --- if ((int) $page->priority > 0) {
$pagePriority = (int) ($page->priority ?? 0); $lines[] = 'page.priority=' . (int) $page->priority;
$pageKeywords = trim((string) ($page->keywords ?? '')); }
// --- Page searchimage → Fallback --- if (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)) {
$searchImageUuid = StringUtil::binToUuid($page->searchimage); $searchImageUuid = StringUtil::binToUuid($page->searchimage);
} } elseif ($fallback = Config::get('meilisearch_fallback_image')) {
if (!$searchImageUuid) {
$fallback = Config::get('meilisearch_fallback_image');
if ($fallback) {
$searchImageUuid = StringUtil::binToUuid($fallback); $searchImageUuid = StringUtil::binToUuid($fallback);
} }
if ($searchImageUuid) {
$lines[] = 'page.searchimage=' . $searchImageUuid;
} }
// Page-Marker nur ergänzen (niemals löschen) // =========================
$markers['page'] = array_merge( // EVENT (Detailseite!)
$markers['page'] ?? [], // =========================
[ if (
'priority' => $pagePriority > 0 ? $pagePriority : null, isset($GLOBALS['objEvent']) &&
'keywords' => $pageKeywords !== '' ? $pageKeywords : null, $GLOBALS['objEvent'] instanceof CalendarEventsModel
'searchimage' => $searchImageUuid ?: null, ) {
] $event = $GLOBALS['objEvent'];
);
// 🔹 Ausgabe bauen if ((int) $event->priority > 0) {
$lines = ['MEILISEARCH']; $lines[] = 'event.priority=' . (int) $event->priority;
foreach ($markers as $scope => $data) {
if (!is_array($data)) {
continue;
} }
foreach ($data as $key => $value) { if (trim((string) $event->keywords) !== '') {
if ($value === null || $value === '' || $value === 0) { $lines[] = 'event.keywords=' . trim((string) $event->keywords);
continue;
}
$lines[] = $scope . '.' . $key . '=' . $value;
} }
} }
// Wenn nichts drinsteht → nichts anhängen // =========================
// OUTPUT
// =========================
if (count($lines) === 1) { if (count($lines) === 1) {
return $buffer; return $buffer;
} }
+1 -11
View File
@@ -2,8 +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\MeilisearchNewsMarkerListener;
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchEventMarkerListener;
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [ $GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
MeilisearchPageMarkerListener::class, MeilisearchPageMarkerListener::class,
@@ -15,15 +14,6 @@ $GLOBALS['TL_HOOKS']['indexPage'][] = [
'onIndexPage' 'onIndexPage'
]; ];
$GLOBALS['TL_HOOKS']['parseTemplate'][] = [
MeilisearchNewsMarkerListener::class,
'onParseTemplate',
];
$GLOBALS['TL_HOOKS']['parseTemplate'][] = [
MeilisearchEventMarkerListener::class,
'onParseTemplate',
];
if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) { if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) {
$GLOBALS['MEILISEARCH_MARKERS'] = []; $GLOBALS['MEILISEARCH_MARKERS'] = [];