From 7f70e32995b4b4508a837ca26999cf5d1f2755c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Mummert?= Date: Mon, 22 Dec 2025 22:01:30 +0100 Subject: [PATCH] Bugfix --- .../MeilisearchPageMarkerListener.php | 117 +++++++++++++++--- 1 file changed, 97 insertions(+), 20 deletions(-) diff --git a/src/EventListener/MeilisearchPageMarkerListener.php b/src/EventListener/MeilisearchPageMarkerListener.php index 975b6a5..5633e63 100644 --- a/src/EventListener/MeilisearchPageMarkerListener.php +++ b/src/EventListener/MeilisearchPageMarkerListener.php @@ -2,41 +2,118 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener; +use Contao\PageModel; +use Contao\CalendarEventsModel; +use Contao\NewsModel; +use Contao\StringUtil; + class MeilisearchPageMarkerListener { public function onOutputFrontendTemplate(string $buffer, string $template): string { - $debug = []; + // Nur Haupt-Frontend-Templates + if (!in_array($template, ['fe_page', 'fe_custom'], true)) { + return $buffer; + } - if (preg_match( - '#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s', - $buffer, - $eventBlock - )) { - $debug[] = 'context=event'; + $lines = ['MEILISEARCH']; - // ✅ KORREKT für deinen Buffer - if (preg_match( - '#\\\/schema\\\/events\\\/(\d+)#', - $eventBlock[0], - $m - )) { - $debug[] = 'event.id=' . $m[1]; - } else { - $debug[] = 'event.id=NOT_FOUND'; + /* + * ===================== + * PAGE (tl_page) + * ===================== + */ + if (isset($GLOBALS['objPage']) && $GLOBALS['objPage'] instanceof PageModel) { + $page = $GLOBALS['objPage']; + + if (!empty($page->priority)) { + $lines[] = 'page.priority=' . (int) $page->priority; + } + + if (!empty($page->keywords)) { + $lines[] = 'page.keywords=' . trim((string) $page->keywords); + } + + if (!empty($page->searchimage)) { + $lines[] = 'page.searchimage=' . StringUtil::binToUuid($page->searchimage); } } - if (empty($debug)) { + /* + * ===================== + * EVENT (schema.org) + * ===================== + */ + if ( + preg_match( + '#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s', + $buffer, + $eventBlock + ) + && preg_match( + '#\\\/schema\\\/events\\\/(\d+)#', + $eventBlock[0], + $m + ) + ) { + $event = CalendarEventsModel::findByPk((int) $m[1]); + + if ($event !== null) { + if (!empty($event->priority)) { + $lines[] = 'event.priority=' . (int) $event->priority; + } + + if (!empty($event->keywords)) { + $lines[] = 'event.keywords=' . trim((string) $event->keywords); + } + } + } + + /* + * ===================== + * NEWS (schema.org) + * ===================== + */ + if ( + preg_match( + '#\{[^}]*"@type"\s*:\s*"NewsArticle"[^}]*\}#s', + $buffer, + $newsBlock + ) + && preg_match( + '#\\\/schema\\\/news\\\/(\d+)#', + $newsBlock[0], + $m + ) + ) { + $news = NewsModel::findByPk((int) $m[1]); + + if ($news !== null) { + if (!empty($news->priority)) { + $lines[] = 'news.priority=' . (int) $news->priority; + } + + if (!empty($news->keywords)) { + $lines[] = 'news.keywords=' . trim((string) $news->keywords); + } + } + } + + // Nichts Relevantes → nichts einfügen + if (count($lines) === 1) { return $buffer; } $marker = "\n\n"; - return str_replace('', $marker . '', $buffer); + // Robust einfügen + if (str_contains($buffer, '')) { + return str_replace('', $marker . '', $buffer); + } + + return $buffer . $marker; } } \ No newline at end of file