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;
use Contao\CalendarEventsModel;
use Contao\NewsModel;
class MeilisearchPageMarkerListener
{
public function onOutputFrontendTemplate(string $buffer, string $template): string
{
$lines = ['MEILISEARCH'];
$debug = [];
// =====================
// EVENT (schema.org)
// EVENT
// =====================
if (preg_match(
'#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s',
$buffer,
$eventBlock
)) {
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);
}
}
}
$debug[] = 'context=event';
// =====================
// NEWS (schema.org)
// =====================
if (preg_match(
'#\{[^}]*"@type"\s*:\s*"NewsArticle"[^}]*\}#s',
$buffer,
$newsBlock
'#"#\\\/schema\\\/events\\\/(\d+)"#',
$eventBlock[0],
$m
)) {
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);
}
$debug[] = 'event.id=' . $m[1];
} else {
$debug[] = 'event.id=NOT_FOUND';
}
}
if (count($lines) === 1) {
if (empty($debug)) {
return $buffer;
}
$marker =
"\n<!--\n" .
implode("\n", $lines) .
"MEILISEARCH DEBUG\n" .
implode("\n", $debug) .
"\n-->\n";
return str_replace('</body>', $marker . '</body>', $buffer);