This commit is contained in:
Jürgen Mummert
2025-12-22 21:54:14 +01:00
parent e325ec7a36
commit 8108148f24
@@ -2,56 +2,41 @@
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
{ {
$lines = ['MEILISEARCH']; $debug = [];
// ===================== // =====================
// EVENT (schema.org) // EVENT
// ===================== // =====================
if (preg_match( if (preg_match(
'#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s', '#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s',
$buffer, $buffer,
$eventBlock $eventBlock
)) { )) {
if (preg_match('#"/schema/events/(\d+)"#', $eventBlock[0], $m)) { $debug[] = 'context=event';
$event = CalendarEventsModel::findByPk((int) $m[1]);
if ($event) { if (preg_match(
$lines[] = 'event.priority=' . (int) $event->priority; '#"#\\\/schema\\\/events\\\/(\d+)"#',
$lines[] = 'event.keywords=' . trim((string) $event->keywords); $eventBlock[0],
} $m
)) {
$debug[] = 'event.id=' . $m[1];
} else {
$debug[] = 'event.id=NOT_FOUND';
} }
} }
// ===================== 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; return $buffer;
} }
$marker = $marker =
"\n<!--\n" . "\n<!--\n" .
implode("\n", $lines) . "MEILISEARCH DEBUG\n" .
implode("\n", $debug) .
"\n-->\n"; "\n-->\n";
return str_replace('</body>', $marker . '</body>', $buffer); return str_replace('</body>', $marker . '</body>', $buffer);