Add Index Listener
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\CalendarEventsModel;
|
||||
use Contao\Template;
|
||||
|
||||
class MeilisearchEventMarkerListener
|
||||
{
|
||||
public function onParseTemplate(Template $template): void
|
||||
{
|
||||
// 🔥 Event-Detailseite erkennen
|
||||
if (
|
||||
!isset($template->event) ||
|
||||
!$template->event instanceof CalendarEventsModel
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event = $template->event;
|
||||
|
||||
$priority = (int) ($event->priority ?? 0);
|
||||
$keywords = trim((string) ($event->keywords ?? ''));
|
||||
|
||||
if ($priority <= 0 && $keywords === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
// ✅ Marker setzen (merge-sicher)
|
||||
$GLOBALS['MEILISEARCH_MARKERS']['event'] = array_merge(
|
||||
$GLOBALS['MEILISEARCH_MARKERS']['event'] ?? [],
|
||||
[
|
||||
'priority' => $priority > 0 ? $priority : null,
|
||||
'keywords' => $keywords !== '' ? $keywords : null,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\NewsModel;
|
||||
use Contao\Template;
|
||||
|
||||
class MeilisearchNewsMarkerListener
|
||||
{
|
||||
public function onParseTemplate(Template $template): void
|
||||
{
|
||||
// Nur News-Reader
|
||||
if ($template->getName() !== 'mod_newsreader') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
!isset($GLOBALS['objArticle']) ||
|
||||
!$GLOBALS['objArticle'] instanceof NewsModel
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 🔥 News vollständig laden (inkl. Custom-Felder)
|
||||
$news = NewsModel::findByPk($GLOBALS['objArticle']->id);
|
||||
|
||||
if ($news === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$GLOBALS['MEILISEARCH_MARKERS']['news'] = [
|
||||
'priority' => (int) ($news->priority ?? 0),
|
||||
'keywords' => trim((string) ($news->keywords ?? '')),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\CalendarEventsModel;
|
||||
use Contao\Config;
|
||||
use Contao\PageModel;
|
||||
use Contao\StringUtil;
|
||||
@@ -10,62 +11,59 @@ class MeilisearchPageMarkerListener
|
||||
{
|
||||
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
||||
{
|
||||
// Nur im Frontend
|
||||
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
// 🔹 Marker aus vorherigen Listenern übernehmen
|
||||
$markers = $GLOBALS['MEILISEARCH_MARKERS'] ?? [];
|
||||
$lines = ['MEILISEARCH'];
|
||||
|
||||
// =========================
|
||||
// PAGE
|
||||
// =========================
|
||||
$page = $GLOBALS['objPage'];
|
||||
|
||||
// --- Page-Daten ---
|
||||
$pagePriority = (int) ($page->priority ?? 0);
|
||||
$pageKeywords = trim((string) ($page->keywords ?? ''));
|
||||
if ((int) $page->priority > 0) {
|
||||
$lines[] = 'page.priority=' . (int) $page->priority;
|
||||
}
|
||||
|
||||
// --- Page searchimage → Fallback ---
|
||||
if (trim((string) $page->keywords) !== '') {
|
||||
$lines[] = 'page.keywords=' . trim((string) $page->keywords);
|
||||
}
|
||||
|
||||
// searchimage (Page → Fallback)
|
||||
$searchImageUuid = null;
|
||||
|
||||
if (!empty($page->searchimage)) {
|
||||
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
|
||||
}
|
||||
|
||||
if (!$searchImageUuid) {
|
||||
$fallback = Config::get('meilisearch_fallback_image');
|
||||
if ($fallback) {
|
||||
} elseif ($fallback = Config::get('meilisearch_fallback_image')) {
|
||||
$searchImageUuid = StringUtil::binToUuid($fallback);
|
||||
}
|
||||
|
||||
if ($searchImageUuid) {
|
||||
$lines[] = 'page.searchimage=' . $searchImageUuid;
|
||||
}
|
||||
|
||||
// Page-Marker nur ergänzen (niemals löschen)
|
||||
$markers['page'] = array_merge(
|
||||
$markers['page'] ?? [],
|
||||
[
|
||||
'priority' => $pagePriority > 0 ? $pagePriority : null,
|
||||
'keywords' => $pageKeywords !== '' ? $pageKeywords : null,
|
||||
'searchimage' => $searchImageUuid ?: null,
|
||||
]
|
||||
);
|
||||
// =========================
|
||||
// EVENT (Detailseite!)
|
||||
// =========================
|
||||
if (
|
||||
isset($GLOBALS['objEvent']) &&
|
||||
$GLOBALS['objEvent'] instanceof CalendarEventsModel
|
||||
) {
|
||||
$event = $GLOBALS['objEvent'];
|
||||
|
||||
// 🔹 Ausgabe bauen
|
||||
$lines = ['MEILISEARCH'];
|
||||
|
||||
foreach ($markers as $scope => $data) {
|
||||
if (!is_array($data)) {
|
||||
continue;
|
||||
if ((int) $event->priority > 0) {
|
||||
$lines[] = 'event.priority=' . (int) $event->priority;
|
||||
}
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if ($value === null || $value === '' || $value === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$lines[] = $scope . '.' . $key . '=' . $value;
|
||||
if (trim((string) $event->keywords) !== '') {
|
||||
$lines[] = 'event.keywords=' . trim((string) $event->keywords);
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn nichts drinsteht → nichts anhängen
|
||||
// =========================
|
||||
// OUTPUT
|
||||
// =========================
|
||||
if (count($lines) === 1) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener;
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchNewsMarkerListener;
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchEventMarkerListener;
|
||||
|
||||
|
||||
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
||||
MeilisearchPageMarkerListener::class,
|
||||
@@ -15,15 +14,6 @@ $GLOBALS['TL_HOOKS']['indexPage'][] = [
|
||||
'onIndexPage'
|
||||
];
|
||||
|
||||
$GLOBALS['TL_HOOKS']['parseTemplate'][] = [
|
||||
MeilisearchNewsMarkerListener::class,
|
||||
'onParseTemplate',
|
||||
];
|
||||
|
||||
$GLOBALS['TL_HOOKS']['parseTemplate'][] = [
|
||||
MeilisearchEventMarkerListener::class,
|
||||
'onParseTemplate',
|
||||
];
|
||||
|
||||
if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) {
|
||||
$GLOBALS['MEILISEARCH_MARKERS'] = [];
|
||||
|
||||
Reference in New Issue
Block a user