Skip to content

Commit

Permalink
Do not register table listeners if Contao bundles are not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Nov 27, 2023
1 parent 3af2014 commit 0a2ee69
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 40 deletions.
76 changes: 45 additions & 31 deletions src/EventListener/CallbackSetupListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Terminal42\ChangeLanguage\EventListener;

use Composer\InstalledVersions;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use Terminal42\ChangeLanguage\EventListener\BackendView\ArticleViewListener;
use Terminal42\ChangeLanguage\EventListener\BackendView\PageViewListener;
Expand All @@ -19,43 +20,56 @@
*/
class CallbackSetupListener
{
private static array $listeners = [
'tl_page' => [
PageViewListener::class,
],
'tl_article' => [
ArticleListener::class,
PageViewListener::class,
ArticleViewListener::class,
],
'tl_content' => [
ArticleViewListener::class,
ParentChildViewListener::class,
],
'tl_news_archive' => [ParentTableListener::class],
'tl_calendar' => [ParentTableListener::class],
'tl_faq_category' => [ParentTableListener::class],
'tl_news' => [
NewsListener::class,
ParentChildViewListener::class,
],
'tl_calendar_events' => [
CalendarEventsListener::class,
ParentChildViewListener::class,
],
'tl_faq' => [
FaqListener::class,
ParentChildViewListener::class,
],
];
private static $listeners;

public function __invoke(string $table): void
{
if (\array_key_exists($table, self::$listeners)) {
foreach (self::$listeners[$table] as $class) {
$listeners = self::getListeners();

if (\array_key_exists($table, $listeners)) {
foreach ($listeners[$table] as $class) {
$listener = new $class($table);
$listener->register();
}
}
}

private static function getListeners(): array
{
if (null !== self::$listeners) {
return self::$listeners;
}

$listeners = [
'tl_page' => [
PageViewListener::class,
],
'tl_article' => [
ArticleListener::class,
PageViewListener::class,
ArticleViewListener::class,
],
'tl_content' => [
ArticleViewListener::class,
ParentChildViewListener::class,
],
];

if (InstalledVersions::isInstalled('contao/news-bundle')) {
$listeners['tl_news_archive'] = [ParentTableListener::class];
$listeners['tl_news'] = [NewsListener::class, ParentChildViewListener::class];
}

if (InstalledVersions::isInstalled('contao/calendar-bundle')) {
$listeners['tl_calendar'] = [ParentTableListener::class];
$listeners['tl_calendar_events'] = [CalendarEventsListener::class, ParentChildViewListener::class];
}

if (InstalledVersions::isInstalled('contao/faq-bundle')) {
$listeners['tl_faq_category'] = [ParentTableListener::class];
$listeners['tl_faq'] = [FaqListener::class, ParentChildViewListener::class];
}

return self::$listeners = $listeners;
}
}
40 changes: 31 additions & 9 deletions src/EventListener/DataContainer/MissingLanguageIconListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Terminal42\ChangeLanguage\EventListener\DataContainer;

use Composer\InstalledVersions;
use Contao\ArticleModel;
use Contao\Backend;
use Contao\BackendUser;
Expand All @@ -27,13 +28,7 @@
*/
class MissingLanguageIconListener
{
private static array $callbacks = [
'tl_page' => 'onPageLabel',
'tl_article' => 'onArticleLabel',
'tl_news' => 'onNewsChildRecords',
'tl_calendar_events' => 'onCalendarEventChildRecords',
'tl_faq' => 'onFaqChildRecords',
];
private static $callbacks;

private Security $security;

Expand All @@ -47,10 +42,12 @@ public function __construct(Security $security)
*/
public function __invoke(string $table): void
{
if (\array_key_exists($table, self::$callbacks)) {
$callbacks = self::getCallbacks();

if (\array_key_exists($table, $callbacks)) {
LabelCallback::createAndRegister(
$table,
fn (array $args, $previousResult) => $this->{self::$callbacks[$table]}($args, $previousResult),
fn (array $args, $previousResult) => $this->{$callbacks[$table]}($args, $previousResult),
);
}
}
Expand Down Expand Up @@ -213,4 +210,29 @@ private function generateLabelWithWarning($label, $imgStyle = '')
$imgStyle,
);
}

private static function getCallbacks(): array {
if (null !== self::$callbacks) {
return self::$callbacks;
}

$callbacks = [
'tl_page' => 'onPageLabel',
'tl_article' => 'onArticleLabel',
];

if (InstalledVersions::isInstalled('contao/news-bundle')) {
$callbacks['tl_news'] = 'onNewsChildRecords';
}

if (InstalledVersions::isInstalled('contao/calendar-bundle')) {
$callbacks['tl_calendar_events'] = 'onCalendarEventChildRecords';
}

if (InstalledVersions::isInstalled('contao/faq-bundle')) {
$callbacks['tl_faq'] = 'onFaqChildRecords';
}

return self::$callbacks = $callbacks;
}
}

0 comments on commit 0a2ee69

Please sign in to comment.