Add Index Listener
This commit is contained in:
@@ -3,41 +3,43 @@
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\PageModel;
|
||||
use Contao\LayoutModel;
|
||||
use Contao\PageRegular;
|
||||
|
||||
class MeilisearchPageMarkerListener
|
||||
{
|
||||
public function onGeneratePage(
|
||||
PageModel $page,
|
||||
LayoutModel $layout,
|
||||
PageRegular $pageRegular
|
||||
): void {
|
||||
$priority = (int) $page->priority;
|
||||
$keywords = trim((string) $page->keywords);
|
||||
|
||||
// nichts gesetzt → nichts schreiben
|
||||
if ($priority <= 0 && $keywords === '') {
|
||||
return;
|
||||
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
||||
{
|
||||
// Nur Frontend-Seiten
|
||||
if (!isset($GLOBALS['objPage']) || !$GLOBALS['objPage'] instanceof PageModel) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$page = $GLOBALS['objPage'];
|
||||
|
||||
// Werte aus tl_page
|
||||
$priority = (int) ($page->priority ?? 0);
|
||||
$keywords = trim((string) ($page->keywords ?? ''));
|
||||
|
||||
// Wenn nichts gesetzt ist, nichts tun
|
||||
if ($priority <= 0 && $keywords === '') {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
// Strukturierter Marker (maschinenlesbar)
|
||||
$lines = [];
|
||||
$lines[] = 'MEILISEARCH';
|
||||
|
||||
if ($priority > 0) {
|
||||
$lines[] = 'page.priority=' . $priority;
|
||||
}
|
||||
|
||||
if ($keywords !== '') {
|
||||
$lines[] = 'page.keywords=' . $keywords;
|
||||
}
|
||||
|
||||
$comment =
|
||||
$marker =
|
||||
"\n<!--\n" .
|
||||
implode("\n", $lines) .
|
||||
"\n-->\n";
|
||||
|
||||
// 🔥 HTML-Ausgabe am Ende erweitern (Contao 5 korrekt)
|
||||
$pageRegular->Template->output .= $comment;
|
||||
// Marker sicher vor </body> einfügen
|
||||
return str_replace('</body>', $marker . '</body>', $buffer);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener;
|
||||
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
|
||||
|
||||
$GLOBALS['TL_HOOKS']['generatePage'][] = [
|
||||
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
||||
MeilisearchPageMarkerListener::class,
|
||||
'onGeneratePage'
|
||||
'onOutputFrontendTemplate',
|
||||
];
|
||||
|
||||
$GLOBALS['TL_HOOKS']['indexPage'][] = [
|
||||
|
||||
Reference in New Issue
Block a user