Add Index Listener

This commit is contained in:
Jürgen Mummert
2025-12-22 20:13:20 +01:00
parent 6d22552a14
commit c4b1d653d0
@@ -3,43 +3,52 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\PageModel; use Contao\PageModel;
use Contao\StringUtil;
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;
} }
$page = $GLOBALS['objPage']; $page = $GLOBALS['objPage'];
// Werte aus tl_page
$priority = (int) ($page->priority ?? 0); $priority = (int) ($page->priority ?? 0);
$keywords = trim((string) ($page->keywords ?? '')); $keywords = trim((string) ($page->keywords ?? ''));
// Wenn nichts gesetzt ist, nichts tun // 🔹 searchimage (UUID)
if ($priority <= 0 && $keywords === '') { $searchImageUuid = null;
if (!empty($page->searchimage)) {
// falls binär → UUID-String
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
}
if ($priority <= 0 && $keywords === '' && !$searchImageUuid) {
return $buffer; 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;
} }
if ($searchImageUuid) {
$lines[] = 'page.searchimage=' . $searchImageUuid;
}
$marker = $marker =
"\n<!--\n" . "\n<!--\n" .
implode("\n", $lines) . implode("\n", $lines) .
"\n-->\n"; "\n-->\n";
// Marker sicher vor </body> einfügen
return str_replace('</body>', $marker . '</body>', $buffer); return str_replace('</body>', $marker . '</body>', $buffer);
} }
} }