Add Index Listener
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use MummertMedia\ContaoMeilisearchBundle\Service\SearchDataProvider;
|
||||
|
||||
class IndexPageListener
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SearchDataProvider $dataProvider
|
||||
) {}
|
||||
|
||||
public function __invoke(string $content, array &$data, array &$set): void
|
||||
{
|
||||
$searchData = $this->dataProvider->getSearchData($set);
|
||||
|
||||
if ($searchData === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// landet direkt in tl_search
|
||||
$data['priority'] = $searchData['priority'];
|
||||
$data['keywords'] = $searchData['keywords'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
services:
|
||||
MummertMedia\ContaoMeilisearchBundle\Service\SearchDataProvider: ~
|
||||
|
||||
MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener:
|
||||
arguments:
|
||||
- '@MummertMedia\ContaoMeilisearchBundle\Service\SearchDataProvider'
|
||||
tags:
|
||||
- { name: contao.hook, hook: indexPage }
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\Service;
|
||||
|
||||
use Contao\Database;
|
||||
|
||||
class SearchDataProvider
|
||||
{
|
||||
public function getSearchData(array $set): ?array
|
||||
{
|
||||
return match ($set['type'] ?? null) {
|
||||
'page' => $this->getPageSearchData((int) ($set['pageId'] ?? 0)),
|
||||
'news' => $this->getNewsSearchData((int) ($set['newsId'] ?? 0)),
|
||||
'calendar' => $this->getEventSearchData((int) ($set['eventId'] ?? 0)),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
private function getPageSearchData(int $pageId): ?array
|
||||
{
|
||||
if ($pageId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = Database::getInstance()
|
||||
->prepare('SELECT priority, keywords FROM tl_page WHERE id=?')
|
||||
->execute($pageId)
|
||||
->fetchAssoc();
|
||||
|
||||
return $row ? [
|
||||
'priority' => (int) $row['priority'],
|
||||
'keywords' => (string) $row['keywords'],
|
||||
] : null;
|
||||
}
|
||||
|
||||
private function getNewsSearchData(int $newsId): ?array
|
||||
{
|
||||
if ($newsId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = Database::getInstance()
|
||||
->prepare('SELECT priority, keywords FROM tl_news WHERE id=?')
|
||||
->execute($newsId)
|
||||
->fetchAssoc();
|
||||
|
||||
return $row ? [
|
||||
'priority' => (int) $row['priority'],
|
||||
'keywords' => (string) $row['keywords'],
|
||||
] : null;
|
||||
}
|
||||
|
||||
private function getEventSearchData(int $eventId): ?array
|
||||
{
|
||||
if ($eventId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = Database::getInstance()
|
||||
->prepare('SELECT priority, keywords FROM tl_calendar_events WHERE id=?')
|
||||
->execute($eventId)
|
||||
->fetchAssoc();
|
||||
|
||||
return $row ? [
|
||||
'priority' => (int) $row['priority'],
|
||||
'keywords' => (string) $row['keywords'],
|
||||
] : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user