Add Index Listener

This commit is contained in:
Jürgen Mummert
2025-12-22 19:54:16 +01:00
parent 66014e6bb7
commit 5de05412ba
4 changed files with 48 additions and 83 deletions
-13
View File
@@ -6,19 +6,6 @@ class IndexPageListener
{ {
public function onIndexPage(string $content, array &$data, array &$set): void public function onIndexPage(string $content, array &$data, array &$set): void
{ {
echo PHP_EOL;
echo '================ MEILI INDEX DEBUG ================' . PHP_EOL;
echo '--- CONTENT (first 500 chars) ---------------------' . PHP_EOL;
echo substr(strip_tags($content), 0, 500) . PHP_EOL;
echo '--- DATA (tl_search record, BEFORE write) ---------' . PHP_EOL;
print_r($data);
echo '--- SET (context) ---------------------------------' . PHP_EOL;
print_r($set);
echo '==================================================' . PHP_EOL;
echo PHP_EOL;
} }
} }
@@ -0,0 +1,38 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\PageModel;
class MeilisearchPageMarkerListener
{
public function onGeneratePage(PageModel $page, string $layout, array &$pageData): void
{
$priority = (int) $page->priority;
$keywords = trim((string) $page->keywords);
// nichts gesetzt → nichts schreiben
if ($priority <= 0 && $keywords === '') {
return;
}
$lines = [];
$lines[] = 'MEILISEARCH';
if ($priority > 0) {
$lines[] = 'page.priority=' . $priority;
}
if ($keywords !== '') {
$lines[] = 'page.keywords=' . $keywords;
}
$comment =
"\n<!--\n" .
implode("\n", $lines) .
"\n-->\n";
// HTML am Ende anhängen
$pageData['output'] .= $comment;
}
}
+10 -1
View File
@@ -1,5 +1,14 @@
<?php <?php
use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener; use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener;
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
$GLOBALS['TL_HOOKS']['indexPage'][] = [IndexPageListener::class, 'onIndexPage']; $GLOBALS['TL_HOOKS']['generatePage'][] = [
MeilisearchPageMarkerListener::class,
'onGeneratePage'
];
$GLOBALS['TL_HOOKS']['indexPage'][] = [
IndexPageListener::class,
'onIndexPage'
];
-69
View File
@@ -1,69 +0,0 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\Service;
use Contao\Database;
class SearchDataProvider
{
public function getSearchData(array $set): ?array
{
return match ($set['type'] ?? null) {
'page' => $this->getPageSearchData((int) ($set['pageId'] ?? 0)),
'news' => $this->getNewsSearchData((int) ($set['newsId'] ?? 0)),
'calendar' => $this->getEventSearchData((int) ($set['eventId'] ?? 0)),
default => null,
};
}
private function getPageSearchData(int $pageId): ?array
{
if ($pageId <= 0) {
return null;
}
$row = Database::getInstance()
->prepare('SELECT priority, keywords FROM tl_page WHERE id=?')
->execute($pageId)
->fetchAssoc();
return $row ? [
'priority' => (int) $row['priority'],
'keywords' => (string) $row['keywords'],
] : null;
}
private function getNewsSearchData(int $newsId): ?array
{
if ($newsId <= 0) {
return null;
}
$row = Database::getInstance()
->prepare('SELECT priority, keywords FROM tl_news WHERE id=?')
->execute($newsId)
->fetchAssoc();
return $row ? [
'priority' => (int) $row['priority'],
'keywords' => (string) $row['keywords'],
] : null;
}
private function getEventSearchData(int $eventId): ?array
{
if ($eventId <= 0) {
return null;
}
$row = Database::getInstance()
->prepare('SELECT priority, keywords FROM tl_calendar_events WHERE id=?')
->execute($eventId)
->fetchAssoc();
return $row ? [
'priority' => (int) $row['priority'],
'keywords' => (string) $row['keywords'],
] : null;
}
}