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