Add Index Listener

This commit is contained in:
Jürgen Mummert
2025-12-22 21:04:36 +01:00
parent ef9d2e0bac
commit f14c1e34ff
2 changed files with 34 additions and 44 deletions
@@ -9,7 +9,6 @@ class MeilisearchEventMarkerListener
{ {
public function onParseTemplate(Template $template): void public function onParseTemplate(Template $template): void
{ {
// Exakter Template-Name kein Raten
if ($template->getName() !== 'mod_eventreader') { if ($template->getName() !== 'mod_eventreader') {
return; return;
} }
@@ -21,7 +20,6 @@ class MeilisearchEventMarkerListener
return; return;
} }
// 🔥 Event vollständig laden (inkl. Custom-Felder)
$event = CalendarEventsModel::findByPk($GLOBALS['objEvent']->id); $event = CalendarEventsModel::findByPk($GLOBALS['objEvent']->id);
if ($event === null) { if ($event === null) {
@@ -2,79 +2,71 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\Config;
use Contao\PageModel; use Contao\PageModel;
use Contao\StringUtil; use Contao\StringUtil;
use Contao\Config;
class MeilisearchPageMarkerListener 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'];
$lines = ['MEILISEARCH'];
/* // ======================
* PAGE-DATEN (immer vorhanden) // PAGE
*/ // ======================
$priority = (int) ($page->priority ?? 0); if ((int) $page->priority > 0) {
$keywords = trim((string) ($page->keywords ?? '')); $lines[] = 'page.priority=' . (int) $page->priority;
}
// searchimage: Page → Fallback if (!empty($page->keywords)) {
$searchImageUuid = null; $lines[] = 'page.keywords=' . trim($page->keywords);
}
if (!empty($page->searchimage)) { if (!empty($page->searchimage)) {
$searchImageUuid = StringUtil::binToUuid($page->searchimage); $lines[] = 'page.searchimage=' . StringUtil::binToUuid($page->searchimage);
} }
if (!$searchImageUuid) { // ======================
$fallback = Config::get('meilisearch_fallback_image'); // EVENT
if ($fallback) { // ======================
$searchImageUuid = StringUtil::binToUuid($fallback); if (!empty($GLOBALS['MEILISEARCH_MARKERS']['event'])) {
$event = $GLOBALS['MEILISEARCH_MARKERS']['event'];
if (!empty($event['priority'])) {
$lines[] = 'event.priority=' . $event['priority'];
}
if (!empty($event['keywords'])) {
$lines[] = 'event.keywords=' . $event['keywords'];
} }
} }
// Page-Marker sammeln // ======================
$markers['page'] = [ // NEWS (später)
'priority' => $priority > 0 ? $priority : null, // ======================
'keywords' => $keywords !== '' ? $keywords : null, if (!empty($GLOBALS['MEILISEARCH_MARKERS']['news'])) {
'searchimage' => $searchImageUuid, $news = $GLOBALS['MEILISEARCH_MARKERS']['news'];
];
/* if (!empty($news['priority'])) {
* MARKER AUFBAUEN $lines[] = 'news.priority=' . $news['priority'];
*/
$lines = [];
$lines[] = 'MEILISEARCH';
foreach ($markers as $scope => $data) {
if (!is_array($data)) {
continue;
} }
foreach ($data as $key => $value) { if (!empty($news['keywords'])) {
if ($value === null || $value === '' || $value === 0) { $lines[] = 'news.keywords=' . $news['keywords'];
continue;
}
$lines[] = $scope . '.' . $key . '=' . $value;
} }
} }
// Wenn wirklich nichts da ist → nichts anhängen
if (count($lines) === 1) { if (count($lines) === 1) {
return $buffer; return $buffer;
} }
$marker = $marker = "\n<!--\n" . implode("\n", $lines) . "\n-->\n";
"\n<!--\n" .
implode("\n", $lines) .
"\n-->\n";
return str_replace('</body>', $marker . '</body>', $buffer); return str_replace('</body>', $marker . '</body>', $buffer);
} }