diff --git a/src/EventListener/MeilisearchPageMarkerListener.php b/src/EventListener/MeilisearchPageMarkerListener.php index 51ceda1..6df3c09 100644 --- a/src/EventListener/MeilisearchPageMarkerListener.php +++ b/src/EventListener/MeilisearchPageMarkerListener.php @@ -2,41 +2,56 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener; +use Contao\CalendarEventsModel; +use Contao\NewsModel; + class MeilisearchPageMarkerListener { public function onOutputFrontendTemplate(string $buffer, string $template): string { - $debug = []; + $lines = ['MEILISEARCH']; // ===================== - // EVENT + // EVENT (schema.org) // ===================== if (preg_match( '#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s', $buffer, $eventBlock )) { - $debug[] = 'context=event'; - - if (preg_match( - '#"#\\\/schema\\\/events\\\/(\d+)"#', - $eventBlock[0], - $m - )) { - $debug[] = 'event.id=' . $m[1]; - } else { - $debug[] = 'event.id=NOT_FOUND'; + if (preg_match('#"/schema/events/(\d+)"#', $eventBlock[0], $m)) { + $event = CalendarEventsModel::findByPk((int) $m[1]); + if ($event) { + $lines[] = 'event.priority=' . (int) $event->priority; + $lines[] = 'event.keywords=' . trim((string) $event->keywords); + } } } - if (empty($debug)) { + // ===================== + // NEWS (schema.org) + // ===================== + if (preg_match( + '#\{[^}]*"@type"\s*:\s*"NewsArticle"[^}]*\}#s', + $buffer, + $newsBlock + )) { + if (preg_match('#"/schema/news/(\d+)"#', $newsBlock[0], $m)) { + $news = NewsModel::findByPk((int) $m[1]); + if ($news) { + $lines[] = 'news.priority=' . (int) $news->priority; + $lines[] = 'news.keywords=' . trim((string) $news->keywords); + } + } + } + + if (count($lines) === 1) { return $buffer; } $marker = "\n\n"; return str_replace('', $marker . '', $buffer);