Add Index Listener
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\CalendarEventsModel;
|
||||
|
||||
class MeilisearchEventMarkerListener
|
||||
{
|
||||
public function onParseFrontendTemplate(string $buffer, string $template): string
|
||||
{
|
||||
if ($template !== 'mod_eventreader') {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
if (!isset($GLOBALS['event']) || !$GLOBALS['event'] instanceof CalendarEventsModel) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$event = $GLOBALS['event'];
|
||||
|
||||
$GLOBALS['MEILISEARCH_MARKERS']['event'] = [
|
||||
'priority' => (int) ($event->priority ?? 0),
|
||||
'keywords' => trim((string) ($event->keywords ?? '')),
|
||||
];
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\NewsModel;
|
||||
use Contao\Template;
|
||||
|
||||
class MeilisearchNewsMarkerListener
|
||||
{
|
||||
public function onParseFrontendTemplate(string $buffer, string $template): string
|
||||
{
|
||||
if ($template !== 'mod_newsreader') {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
if (!isset($GLOBALS['news']) || !$GLOBALS['news'] instanceof NewsModel) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$news = $GLOBALS['news'];
|
||||
|
||||
$GLOBALS['MEILISEARCH_MARKERS']['news'] = [
|
||||
'priority' => (int) ($news->priority ?? 0),
|
||||
'keywords' => trim((string) ($news->keywords ?? '')),
|
||||
];
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
||||
@@ -10,24 +10,28 @@ class MeilisearchPageMarkerListener
|
||||
{
|
||||
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
||||
{
|
||||
// Nur Frontend-Seiten
|
||||
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$markers = $GLOBALS['MEILISEARCH_MARKERS'] ?? [];
|
||||
|
||||
$page = $GLOBALS['objPage'];
|
||||
|
||||
/*
|
||||
* PAGE-DATEN (immer vorhanden)
|
||||
*/
|
||||
$priority = (int) ($page->priority ?? 0);
|
||||
$keywords = trim((string) ($page->keywords ?? ''));
|
||||
|
||||
// 🔹 searchimage (Page → Fallback)
|
||||
// searchimage: Page → Fallback
|
||||
$searchImageUuid = null;
|
||||
|
||||
// 1. Page-spezifisch
|
||||
if (!empty($page->searchimage)) {
|
||||
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
|
||||
}
|
||||
|
||||
// 2. Fallback aus tl_settings
|
||||
if (!$searchImageUuid) {
|
||||
$fallback = Config::get('meilisearch_fallback_image');
|
||||
if ($fallback) {
|
||||
@@ -35,25 +39,36 @@ class MeilisearchPageMarkerListener
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn wirklich GAR nichts vorhanden ist → nichts tun
|
||||
if ($priority <= 0 && $keywords === '' && !$searchImageUuid) {
|
||||
return $buffer;
|
||||
}
|
||||
// Page-Marker sammeln
|
||||
$markers['page'] = [
|
||||
'priority' => $priority > 0 ? $priority : null,
|
||||
'keywords' => $keywords !== '' ? $keywords : null,
|
||||
'searchimage' => $searchImageUuid,
|
||||
];
|
||||
|
||||
// 🔹 Marker aufbauen
|
||||
/*
|
||||
* MARKER AUFBAUEN
|
||||
*/
|
||||
$lines = [];
|
||||
$lines[] = 'MEILISEARCH';
|
||||
|
||||
if ($priority > 0) {
|
||||
$lines[] = 'page.priority=' . $priority;
|
||||
foreach ($markers as $scope => $data) {
|
||||
if (!is_array($data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($keywords !== '') {
|
||||
$lines[] = 'page.keywords=' . $keywords;
|
||||
foreach ($data as $key => $value) {
|
||||
if ($value === null || $value === '' || $value === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($searchImageUuid) {
|
||||
$lines[] = 'page.searchimage=' . $searchImageUuid;
|
||||
$lines[] = $scope . '.' . $key . '=' . $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn wirklich nichts da ist → nichts anhängen
|
||||
if (count($lines) === 1) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$marker =
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
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,
|
||||
@@ -12,3 +14,15 @@ $GLOBALS['TL_HOOKS']['indexPage'][] = [
|
||||
IndexPageListener::class,
|
||||
'onIndexPage'
|
||||
];
|
||||
|
||||
$GLOBALS['TL_HOOKS']['parseFrontendTemplate'][] = [
|
||||
MeilisearchNewsMarkerListener::class,
|
||||
'onParseFrontendTemplate',
|
||||
];
|
||||
|
||||
$GLOBALS['TL_HOOKS']['parseFrontendTemplate'][] = [
|
||||
MeilisearchEventMarkerListener::class,
|
||||
'onParseFrontendTemplate',
|
||||
];
|
||||
|
||||
$GLOBALS['MEILISEARCH_MARKERS'] = [];
|
||||
Reference in New Issue
Block a user