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
|
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
||||||
{
|
{
|
||||||
|
// Nur Frontend-Seiten
|
||||||
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$markers = $GLOBALS['MEILISEARCH_MARKERS'] ?? [];
|
||||||
|
|
||||||
$page = $GLOBALS['objPage'];
|
$page = $GLOBALS['objPage'];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* PAGE-DATEN (immer vorhanden)
|
||||||
|
*/
|
||||||
$priority = (int) ($page->priority ?? 0);
|
$priority = (int) ($page->priority ?? 0);
|
||||||
$keywords = trim((string) ($page->keywords ?? ''));
|
$keywords = trim((string) ($page->keywords ?? ''));
|
||||||
|
|
||||||
// 🔹 searchimage (Page → Fallback)
|
// searchimage: Page → Fallback
|
||||||
$searchImageUuid = null;
|
$searchImageUuid = null;
|
||||||
|
|
||||||
// 1. Page-spezifisch
|
|
||||||
if (!empty($page->searchimage)) {
|
if (!empty($page->searchimage)) {
|
||||||
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
|
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Fallback aus tl_settings
|
|
||||||
if (!$searchImageUuid) {
|
if (!$searchImageUuid) {
|
||||||
$fallback = Config::get('meilisearch_fallback_image');
|
$fallback = Config::get('meilisearch_fallback_image');
|
||||||
if ($fallback) {
|
if ($fallback) {
|
||||||
@@ -35,25 +39,36 @@ class MeilisearchPageMarkerListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wenn wirklich GAR nichts vorhanden ist → nichts tun
|
// Page-Marker sammeln
|
||||||
if ($priority <= 0 && $keywords === '' && !$searchImageUuid) {
|
$markers['page'] = [
|
||||||
return $buffer;
|
'priority' => $priority > 0 ? $priority : null,
|
||||||
}
|
'keywords' => $keywords !== '' ? $keywords : null,
|
||||||
|
'searchimage' => $searchImageUuid,
|
||||||
|
];
|
||||||
|
|
||||||
// 🔹 Marker aufbauen
|
/*
|
||||||
|
* MARKER AUFBAUEN
|
||||||
|
*/
|
||||||
$lines = [];
|
$lines = [];
|
||||||
$lines[] = 'MEILISEARCH';
|
$lines[] = 'MEILISEARCH';
|
||||||
|
|
||||||
if ($priority > 0) {
|
foreach ($markers as $scope => $data) {
|
||||||
$lines[] = 'page.priority=' . $priority;
|
if (!is_array($data)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
if ($value === null || $value === '' || $value === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines[] = $scope . '.' . $key . '=' . $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($keywords !== '') {
|
// Wenn wirklich nichts da ist → nichts anhängen
|
||||||
$lines[] = 'page.keywords=' . $keywords;
|
if (count($lines) === 1) {
|
||||||
}
|
return $buffer;
|
||||||
|
|
||||||
if ($searchImageUuid) {
|
|
||||||
$lines[] = 'page.searchimage=' . $searchImageUuid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$marker =
|
$marker =
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
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,
|
||||||
@@ -11,4 +13,16 @@ $GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
|||||||
$GLOBALS['TL_HOOKS']['indexPage'][] = [
|
$GLOBALS['TL_HOOKS']['indexPage'][] = [
|
||||||
IndexPageListener::class,
|
IndexPageListener::class,
|
||||||
'onIndexPage'
|
'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