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;
|
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||||
|
|
||||||
|
use Contao\CalendarEventsModel;
|
||||||
use Contao\Config;
|
use Contao\Config;
|
||||||
use Contao\PageModel;
|
use Contao\PageModel;
|
||||||
use Contao\StringUtil;
|
use Contao\StringUtil;
|
||||||
@@ -10,62 +11,59 @@ class MeilisearchPageMarkerListener
|
|||||||
{
|
{
|
||||||
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
||||||
{
|
{
|
||||||
// Nur im Frontend
|
|
||||||
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔹 Marker aus vorherigen Listenern übernehmen
|
$lines = ['MEILISEARCH'];
|
||||||
$markers = $GLOBALS['MEILISEARCH_MARKERS'] ?? [];
|
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// PAGE
|
||||||
|
// =========================
|
||||||
$page = $GLOBALS['objPage'];
|
$page = $GLOBALS['objPage'];
|
||||||
|
|
||||||
// --- Page-Daten ---
|
if ((int) $page->priority > 0) {
|
||||||
$pagePriority = (int) ($page->priority ?? 0);
|
$lines[] = 'page.priority=' . (int) $page->priority;
|
||||||
$pageKeywords = trim((string) ($page->keywords ?? ''));
|
}
|
||||||
|
|
||||||
// --- Page searchimage → Fallback ---
|
if (trim((string) $page->keywords) !== '') {
|
||||||
|
$lines[] = 'page.keywords=' . trim((string) $page->keywords);
|
||||||
|
}
|
||||||
|
|
||||||
|
// searchimage (Page → Fallback)
|
||||||
$searchImageUuid = null;
|
$searchImageUuid = null;
|
||||||
|
|
||||||
if (!empty($page->searchimage)) {
|
if (!empty($page->searchimage)) {
|
||||||
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
|
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
|
||||||
|
} elseif ($fallback = Config::get('meilisearch_fallback_image')) {
|
||||||
|
$searchImageUuid = StringUtil::binToUuid($fallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$searchImageUuid) {
|
if ($searchImageUuid) {
|
||||||
$fallback = Config::get('meilisearch_fallback_image');
|
$lines[] = 'page.searchimage=' . $searchImageUuid;
|
||||||
if ($fallback) {
|
}
|
||||||
$searchImageUuid = StringUtil::binToUuid($fallback);
|
|
||||||
|
// =========================
|
||||||
|
// EVENT (Detailseite!)
|
||||||
|
// =========================
|
||||||
|
if (
|
||||||
|
isset($GLOBALS['objEvent']) &&
|
||||||
|
$GLOBALS['objEvent'] instanceof CalendarEventsModel
|
||||||
|
) {
|
||||||
|
$event = $GLOBALS['objEvent'];
|
||||||
|
|
||||||
|
if ((int) $event->priority > 0) {
|
||||||
|
$lines[] = 'event.priority=' . (int) $event->priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trim((string) $event->keywords) !== '') {
|
||||||
|
$lines[] = 'event.keywords=' . trim((string) $event->keywords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Page-Marker nur ergänzen (niemals löschen)
|
// =========================
|
||||||
$markers['page'] = array_merge(
|
// OUTPUT
|
||||||
$markers['page'] ?? [],
|
// =========================
|
||||||
[
|
|
||||||
'priority' => $pagePriority > 0 ? $pagePriority : null,
|
|
||||||
'keywords' => $pageKeywords !== '' ? $pageKeywords : null,
|
|
||||||
'searchimage' => $searchImageUuid ?: null,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// 🔹 Ausgabe bauen
|
|
||||||
$lines = ['MEILISEARCH'];
|
|
||||||
|
|
||||||
foreach ($markers as $scope => $data) {
|
|
||||||
if (!is_array($data)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($data as $key => $value) {
|
|
||||||
if ($value === null || $value === '' || $value === 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$lines[] = $scope . '.' . $key . '=' . $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wenn nichts drinsteht → nichts anhängen
|
|
||||||
if (count($lines) === 1) {
|
if (count($lines) === 1) {
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener;
|
use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener;
|
||||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
|
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
|
||||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchNewsMarkerListener;
|
|
||||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchEventMarkerListener;
|
|
||||||
|
|
||||||
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
||||||
MeilisearchPageMarkerListener::class,
|
MeilisearchPageMarkerListener::class,
|
||||||
@@ -15,15 +14,6 @@ $GLOBALS['TL_HOOKS']['indexPage'][] = [
|
|||||||
'onIndexPage'
|
'onIndexPage'
|
||||||
];
|
];
|
||||||
|
|
||||||
$GLOBALS['TL_HOOKS']['parseTemplate'][] = [
|
|
||||||
MeilisearchNewsMarkerListener::class,
|
|
||||||
'onParseTemplate',
|
|
||||||
];
|
|
||||||
|
|
||||||
$GLOBALS['TL_HOOKS']['parseTemplate'][] = [
|
|
||||||
MeilisearchEventMarkerListener::class,
|
|
||||||
'onParseTemplate',
|
|
||||||
];
|
|
||||||
|
|
||||||
if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) {
|
if (!isset($GLOBALS['MEILISEARCH_MARKERS'])) {
|
||||||
$GLOBALS['MEILISEARCH_MARKERS'] = [];
|
$GLOBALS['MEILISEARCH_MARKERS'] = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user