Add Index Listener
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\CalendarEventsModel;
|
||||
use Contao\Template;
|
||||
|
||||
class MeilisearchEventContextListener
|
||||
{
|
||||
public function onParseFrontendTemplate(string $buffer, string $template): string
|
||||
{
|
||||
// Nur Event-Reader-Modul
|
||||
if ($template !== 'mod_eventreader') {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
// Event kommt IM Template an
|
||||
if (
|
||||
isset($GLOBALS['objEvent']) &&
|
||||
$GLOBALS['objEvent'] instanceof CalendarEventsModel
|
||||
) {
|
||||
$eventId = (int) $GLOBALS['objEvent']->id;
|
||||
|
||||
// Minimaler, stabiler Marker
|
||||
$marker = "\n<!-- MEILI_EVENT id={$eventId} -->\n";
|
||||
|
||||
return $buffer . $marker;
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
||||
@@ -2,70 +2,42 @@
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\CalendarEventsModel;
|
||||
use Contao\Config;
|
||||
use Contao\PageModel;
|
||||
use Contao\StringUtil;
|
||||
|
||||
class MeilisearchPageMarkerListener
|
||||
{
|
||||
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
||||
{
|
||||
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
||||
return $buffer;
|
||||
}
|
||||
$debug = [];
|
||||
|
||||
$lines = ['MEILISEARCH'];
|
||||
$page = $GLOBALS['objPage'];
|
||||
// 🔍 Event erkennen
|
||||
if (preg_match('#"@type"\s*:\s*"Event"#', $buffer)) {
|
||||
$debug[] = 'context=event';
|
||||
|
||||
/* ================= PAGE ================= */
|
||||
|
||||
if ((int) $page->priority > 0) {
|
||||
$lines[] = 'page.priority=' . (int) $page->priority;
|
||||
}
|
||||
|
||||
if (trim((string) $page->keywords) !== '') {
|
||||
$lines[] = 'page.keywords=' . trim((string) $page->keywords);
|
||||
}
|
||||
|
||||
$searchImageUuid = null;
|
||||
|
||||
if (!empty($page->searchimage)) {
|
||||
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
|
||||
} elseif ($fallback = Config::get('meilisearch_fallback_image')) {
|
||||
$searchImageUuid = StringUtil::binToUuid($fallback);
|
||||
}
|
||||
|
||||
if ($searchImageUuid) {
|
||||
$lines[] = 'page.searchimage=' . $searchImageUuid;
|
||||
}
|
||||
|
||||
/* ================= EVENT ================= */
|
||||
|
||||
if (preg_match('/<!--\s*MEILI_EVENT id=(\d+)\s*-->/', $buffer, $m)) {
|
||||
$eventId = (int) $m[1];
|
||||
$event = CalendarEventsModel::findByPk($eventId);
|
||||
|
||||
if ($event !== null) {
|
||||
if ((int) $event->priority > 0) {
|
||||
$lines[] = 'event.priority=' . (int) $event->priority;
|
||||
}
|
||||
|
||||
if (trim((string) $event->keywords) !== '') {
|
||||
$lines[] = 'event.keywords=' . trim((string) $event->keywords);
|
||||
if (preg_match('#"#/schema/events/(\d+)"#', $buffer, $m)) {
|
||||
$debug[] = 'event.id=' . $m[1];
|
||||
} else {
|
||||
$debug[] = 'event.id=NOT_FOUND';
|
||||
}
|
||||
}
|
||||
|
||||
// 🔍 News erkennen (vorbereitet)
|
||||
if (preg_match('#"@type"\s*:\s*"NewsArticle"#', $buffer)) {
|
||||
$debug[] = 'context=news';
|
||||
|
||||
if (preg_match('#"#/schema/news/(\d+)"#', $buffer, $m)) {
|
||||
$debug[] = 'news.id=' . $m[1];
|
||||
} else {
|
||||
$debug[] = 'news.id=NOT_FOUND';
|
||||
}
|
||||
}
|
||||
|
||||
/* ================= OUTPUT ================= */
|
||||
|
||||
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);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener;
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchEventContextListener;
|
||||
|
||||
|
||||
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
||||
@@ -15,11 +14,5 @@ $GLOBALS['TL_HOOKS']['indexPage'][] = [
|
||||
'onIndexPage'
|
||||
];
|
||||
|
||||
$GLOBALS['TL_HOOKS']['parseFrontendTemplate'][] = [
|
||||
MeilisearchEventContextListener::class,
|
||||
'onParseFrontendTemplate',
|
||||
];
|
||||
|
||||
if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) {
|
||||
$GLOBALS['MEILISEARCH_MARKERS'] = [];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user