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;
use Contao\PageModel;
use Contao\StringUtil;
class MeilisearchPageMarkerListener
{
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 === '') {
// 🔹 searchimage (UUID)
$searchImageUuid = null;
if (!empty($page->searchimage)) {
// falls binär → UUID-String
$searchImageUuid = StringUtil::binToUuid($page->searchimage);
}
if ($priority <= 0 && $keywords === '' && !$searchImageUuid) {
return $buffer;
}
// Strukturierter Marker (maschinenlesbar)
$lines = [];
$lines[] = 'MEILISEARCH';
if ($priority > 0) {
$lines[] = 'page.priority=' . $priority;
}
if ($keywords !== '') {
$lines[] = 'page.keywords=' . $keywords;
}
if ($searchImageUuid) {
$lines[] = 'page.searchimage=' . $searchImageUuid;
}
$marker =
"\n<!--\n" .
implode("\n", $lines) .
"\n-->\n";
// Marker sicher vor </body> einfügen
return str_replace('</body>', $marker . '</body>', $buffer);
}
}