This commit is contained in:
Jürgen Mummert
2025-12-26 22:08:38 +01:00
parent fa97460877
commit 640cccc928
3 changed files with 78 additions and 5 deletions
+2 -3
View File
@@ -1,16 +1,15 @@
services: services:
# EventListener + Service automatisch laden
MummertMedia\ContaoMeilisearchBundle\: MummertMedia\ContaoMeilisearchBundle\:
resource: '../../{Command,Cron,EventListener,Service}' resource: '../../{Command,Cron,EventListener,Service}'
autowire: true autowire: true
autoconfigure: true autoconfigure: true
public: true
# IndexPageListener als Contao-Hook registrieren # IndexPageListener als Contao-Hook
MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener: MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener:
tags: tags:
- { name: contao.hook, hook: indexPage, method: onIndexPage } - { name: contao.hook, hook: indexPage, method: onIndexPage }
# täglicher Reindex
MummertMedia\ContaoMeilisearchBundle\Cron\MeilisearchIndexCron: MummertMedia\ContaoMeilisearchBundle\Cron\MeilisearchIndexCron:
tags: tags:
- { name: contao.cron, interval: daily } - { name: contao.cron, interval: daily }
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\Service;
use Contao\Config;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Image\Studio\Studio;
use Contao\FilesModel;
class MeilisearchImageHelper
{
public function __construct(
private readonly ContaoFramework $framework,
private readonly Studio $studio,
) {}
/**
* Wandelt eine Bild-UUID aus tl_search.imagepath
* in einen generierten Asset-Pfad (/assets/images/…)
*/
public function resolveImagePath(?string $uuid): ?string
{
if (!$uuid) {
return null;
}
// Contao-Framework sicher initialisieren
$this->framework->initialize();
/** @var FilesModel|null $file */
$file = FilesModel::findByUuid($uuid);
if (!$file) {
return null;
}
// ImageSize aus tl_settings
$imageSizeId = (int) Config::get('meilisearch_imagesize');
// Fallback: Originaldatei
if ($imageSizeId <= 0) {
return $file->path;
}
try {
$figure = $this->studio
->createFigureBuilder()
->from($file->path)
->setSize($imageSizeId)
->build();
$image = $figure->getImage();
if ($image === null) {
return null;
}
return $image->getUrl();
} catch (\Throwable) {
// bewusst still kein Bild = kein Index-Fail
return null;
}
}
}
+14 -2
View File
@@ -15,6 +15,7 @@ class MeilisearchIndexService
public function __construct( public function __construct(
private readonly Connection $connection, private readonly Connection $connection,
private readonly ContaoFramework $framework, private readonly ContaoFramework $framework,
private readonly MeilisearchImageHelper $imageHelper,
) {} ) {}
/** /**
@@ -67,7 +68,7 @@ class MeilisearchIndexService
foreach ($rows as $row) { foreach ($rows as $row) {
$type = $this->detectTypeFromMeta($row['meta'] ?? null); $type = $this->detectTypeFromMeta($row['meta'] ?? null);
$documents[] = [ $doc = [
'id' => $type . '_' . $row['id'], 'id' => $type . '_' . $row['id'],
'type' => $type, 'type' => $type,
'title' => $row['title'], 'title' => $row['title'],
@@ -78,6 +79,16 @@ class MeilisearchIndexService
'keywords' => (string) ($row['keywords'] ?? ''), 'keywords' => (string) ($row['keywords'] ?? ''),
'priority' => (int) ($row['priority'] ?? 0), 'priority' => (int) ($row['priority'] ?? 0),
]; ];
// ✅ Bild aus UUID erzeugen (falls vorhanden)
if (!empty($row['imagepath'])) {
$imagePath = $this->imageHelper->resolveImagePath($row['imagepath']);
if ($imagePath !== null) {
$doc['poster'] = $imagePath;
}
}
$documents[] = $doc;
} }
$index->addDocuments($documents); $index->addDocuments($documents);
@@ -104,7 +115,7 @@ class MeilisearchIndexService
'id' => $fileType . '_' . $row['id'], 'id' => $fileType . '_' . $row['id'],
'type' => $fileType, 'type' => $fileType,
'title' => $row['title'], 'title' => $row['title'],
'text' => $row['text'], // ✅ korrekt 'text' => $row['text'],
'url' => $row['url'], 'url' => $row['url'],
'checksum' => $row['checksum'], 'checksum' => $row['checksum'],
]; ];
@@ -112,6 +123,7 @@ class MeilisearchIndexService
$index->addDocuments($documents); $index->addDocuments($documents);
} }
private function detectTypeFromMeta(?string $meta): string private function detectTypeFromMeta(?string $meta): string
{ {
if (!$meta) { if (!$meta) {