Bugfix
This commit is contained in:
@@ -2,41 +2,118 @@
|
|||||||
|
|
||||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||||
|
|
||||||
|
use Contao\PageModel;
|
||||||
|
use Contao\CalendarEventsModel;
|
||||||
|
use Contao\NewsModel;
|
||||||
|
use Contao\StringUtil;
|
||||||
|
|
||||||
class MeilisearchPageMarkerListener
|
class MeilisearchPageMarkerListener
|
||||||
{
|
{
|
||||||
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
public function onOutputFrontendTemplate(string $buffer, string $template): string
|
||||||
{
|
{
|
||||||
$debug = [];
|
// Nur Haupt-Frontend-Templates
|
||||||
|
if (!in_array($template, ['fe_page', 'fe_custom'], true)) {
|
||||||
|
return $buffer;
|
||||||
|
}
|
||||||
|
|
||||||
if (preg_match(
|
$lines = ['MEILISEARCH'];
|
||||||
'#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s',
|
|
||||||
$buffer,
|
|
||||||
$eventBlock
|
|
||||||
)) {
|
|
||||||
$debug[] = 'context=event';
|
|
||||||
|
|
||||||
// ✅ KORREKT für deinen Buffer
|
/*
|
||||||
if (preg_match(
|
* =====================
|
||||||
'#\\\/schema\\\/events\\\/(\d+)#',
|
* PAGE (tl_page)
|
||||||
$eventBlock[0],
|
* =====================
|
||||||
$m
|
*/
|
||||||
)) {
|
if (isset($GLOBALS['objPage']) && $GLOBALS['objPage'] instanceof PageModel) {
|
||||||
$debug[] = 'event.id=' . $m[1];
|
$page = $GLOBALS['objPage'];
|
||||||
} else {
|
|
||||||
$debug[] = 'event.id=NOT_FOUND';
|
if (!empty($page->priority)) {
|
||||||
|
$lines[] = 'page.priority=' . (int) $page->priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($page->keywords)) {
|
||||||
|
$lines[] = 'page.keywords=' . trim((string) $page->keywords);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($page->searchimage)) {
|
||||||
|
$lines[] = 'page.searchimage=' . StringUtil::binToUuid($page->searchimage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($debug)) {
|
/*
|
||||||
|
* =====================
|
||||||
|
* EVENT (schema.org)
|
||||||
|
* =====================
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
preg_match(
|
||||||
|
'#\{[^}]*"@type"\s*:\s*"Event"[^}]*\}#s',
|
||||||
|
$buffer,
|
||||||
|
$eventBlock
|
||||||
|
)
|
||||||
|
&& preg_match(
|
||||||
|
'#\\\/schema\\\/events\\\/(\d+)#',
|
||||||
|
$eventBlock[0],
|
||||||
|
$m
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
$event = CalendarEventsModel::findByPk((int) $m[1]);
|
||||||
|
|
||||||
|
if ($event !== null) {
|
||||||
|
if (!empty($event->priority)) {
|
||||||
|
$lines[] = 'event.priority=' . (int) $event->priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($event->keywords)) {
|
||||||
|
$lines[] = 'event.keywords=' . trim((string) $event->keywords);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =====================
|
||||||
|
* NEWS (schema.org)
|
||||||
|
* =====================
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
preg_match(
|
||||||
|
'#\{[^}]*"@type"\s*:\s*"NewsArticle"[^}]*\}#s',
|
||||||
|
$buffer,
|
||||||
|
$newsBlock
|
||||||
|
)
|
||||||
|
&& preg_match(
|
||||||
|
'#\\\/schema\\\/news\\\/(\d+)#',
|
||||||
|
$newsBlock[0],
|
||||||
|
$m
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
$news = NewsModel::findByPk((int) $m[1]);
|
||||||
|
|
||||||
|
if ($news !== null) {
|
||||||
|
if (!empty($news->priority)) {
|
||||||
|
$lines[] = 'news.priority=' . (int) $news->priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($news->keywords)) {
|
||||||
|
$lines[] = 'news.keywords=' . trim((string) $news->keywords);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nichts Relevantes → nichts einfügen
|
||||||
|
if (count($lines) === 1) {
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
$marker =
|
$marker =
|
||||||
"\n<!--\n" .
|
"\n<!--\n" .
|
||||||
"MEILISEARCH DEBUG\n" .
|
implode("\n", $lines) .
|
||||||
implode("\n", $debug) .
|
|
||||||
"\n-->\n";
|
"\n-->\n";
|
||||||
|
|
||||||
return str_replace('</body>', $marker . '</body>', $buffer);
|
// Robust einfügen
|
||||||
|
if (str_contains($buffer, '</body>')) {
|
||||||
|
return str_replace('</body>', $marker . '</body>', $buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $buffer . $marker;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user