This commit is contained in:
Jürgen Mummert
2025-12-22 21:51:19 +01:00
parent ab2dc091f7
commit e325ec7a36
@@ -2,41 +2,56 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\CalendarEventsModel;
use Contao\NewsModel;
class MeilisearchPageMarkerListener class MeilisearchPageMarkerListener
{ {
public function onOutputFrontendTemplate(string $buffer, string $template): string public function onOutputFrontendTemplate(string $buffer, string $template): string
{ {
$debug = []; $lines = ['MEILISEARCH'];
// ===================== // =====================
// EVENT // EVENT (schema.org)
// ===================== // =====================
if (preg_match( if (preg_match(
'#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s', '#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s',
$buffer, $buffer,
$eventBlock $eventBlock
)) { )) {
$debug[] = 'context=event'; 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);
}
}
}
// =====================
// NEWS (schema.org)
// =====================
if (preg_match( if (preg_match(
'#"#\\\/schema\\\/events\\\/(\d+)"#', '#\{[^}]*"@type"\s*:\s*"NewsArticle"[^}]*\}#s',
$eventBlock[0], $buffer,
$m $newsBlock
)) { )) {
$debug[] = 'event.id=' . $m[1]; if (preg_match('#"/schema/news/(\d+)"#', $newsBlock[0], $m)) {
} else { $news = NewsModel::findByPk((int) $m[1]);
$debug[] = 'event.id=NOT_FOUND'; if ($news) {
$lines[] = 'news.priority=' . (int) $news->priority;
$lines[] = 'news.keywords=' . trim((string) $news->keywords);
}
} }
} }
if (empty($debug)) { if (count($lines) === 1) {
return $buffer; return $buffer;
} }
$marker = $marker =
"\n<!--\n" . "\n<!--\n" .
"MEILISEARCH DEBUG\n" . implode("\n", $lines) .
implode("\n", $debug) .
"\n-->\n"; "\n-->\n";
return str_replace('</body>', $marker . '</body>', $buffer); return str_replace('</body>', $marker . '</body>', $buffer);