Skip to content

Commit

Permalink
Use dependency injection where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Dec 8, 2022
1 parent 15227e2 commit 99b6e9e
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 115 deletions.
15 changes: 13 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ services:
autoconfigure: true
autowire: true

Terminal42\ChangeLanguage\EventListener\DataContainer\UserLabelsListener: ~
Terminal42\ChangeLanguage\EventListener\:
resource: ../src/EventListener/*
exclude: ../src/EventListener/{BackendView,DataContainer}/*
public: true

Terminal42\ChangeLanguage\Migration\CustomLanguageTextMigration: ~
Terminal42\ChangeLanguage\EventListener\DataContainer\MissingLanguageIconListener: ~
Terminal42\ChangeLanguage\EventListener\DataContainer\PageFieldsListener: ~
Terminal42\ChangeLanguage\EventListener\DataContainer\PageInitializationListener: ~
Terminal42\ChangeLanguage\EventListener\DataContainer\PageOperationListener: ~

Terminal42\ChangeLanguage\EventListener\CallbackSetupListener:

Terminal42\ChangeLanguage\Migration\:
resource: ../src/Migration/*
24 changes: 3 additions & 21 deletions contao/config/config.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
<?php

/**
* Frontend modules
*/
$GLOBALS['FE_MOD']['miscellaneous']['changelanguage'] = 'Terminal42\ChangeLanguage\FrontendModule\ChangeLanguageModule';

use Terminal42\ChangeLanguage\FrontendModule\ChangeLanguageModule;

/**
* Hooks
* Frontend modules
*/
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = ['Terminal42\ChangeLanguage\EventListener\InsertTagsListener', 'onReplaceInsertTags'];
$GLOBALS['TL_HOOKS']['loadDataContainer'][] = ['Terminal42\ChangeLanguage\EventListener\CallbackSetupListener', 'onLoadDataContainer'];
$GLOBALS['TL_HOOKS']['changelanguageNavigation'][] = ['Terminal42\ChangeLanguage\EventListener\Navigation\ArticleNavigationListener', 'onChangelanguageNavigation'];

if (class_exists(Contao\CalendarBundle\ContaoCalendarBundle::class)) {
$GLOBALS['TL_HOOKS']['changelanguageNavigation'][] = ['Terminal42\ChangeLanguage\EventListener\Navigation\CalendarNavigationListener', 'onChangelanguageNavigation'];
}

if (class_exists(Contao\FaqBundle\ContaoFaqBundle::class)) {
$GLOBALS['TL_HOOKS']['changelanguageNavigation'][] = ['Terminal42\ChangeLanguage\EventListener\Navigation\FaqNavigationListener', 'onChangelanguageNavigation'];
}

if (class_exists(Contao\NewsBundle\ContaoNewsBundle::class)) {
$GLOBALS['TL_HOOKS']['changelanguageNavigation'][] = ['Terminal42\ChangeLanguage\EventListener\Navigation\NewsNavigationListener', 'onChangelanguageNavigation'];
}
$GLOBALS['FE_MOD']['miscellaneous']['changelanguage'] = ChangeLanguageModule::class;
6 changes: 3 additions & 3 deletions contao/dca/tl_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
'inputType' => 'pageTree',
'eval' => array('fieldType'=>'radio', 'multiple'=>false, 'rootNodes'=>[0], 'tl_class'=>'w50 clr'),
'sql' => "int(10) unsigned NOT NULL default '0'",
'load_callback' => [['Terminal42\ChangeLanguage\EventListener\DataContainer\PageFieldsListener', 'onLoadLanguageMain']],
'save_callback' => [['Terminal42\ChangeLanguage\EventListener\DataContainer\PageFieldsListener', 'onSaveLanguageMain']],
//'load_callback' => [['Terminal42\ChangeLanguage\EventListener\DataContainer\PageFieldsListener', 'onLoadLanguageMain']],
//'save_callback' => [['Terminal42\ChangeLanguage\EventListener\DataContainer\PageFieldsListener', 'onSaveLanguageMain']],
);

$GLOBALS['TL_DCA']['tl_page']['fields']['languageRoot'] = array
(
'label' => &$GLOBALS['TL_LANG']['tl_page']['languageRoot'],
'exclude' => true,
'inputType' => 'select',
'options_callback' => array('Terminal42\ChangeLanguage\EventListener\DataContainer\PageFieldsListener', 'onLanguageRootOptions'),
//'options_callback' => array('Terminal42\ChangeLanguage\EventListener\DataContainer\PageFieldsListener', 'onLanguageRootOptions'),
'eval' => array('includeBlankOption'=>true, 'blankOptionLabel'=>&$GLOBALS['TL_LANG']['tl_page']['languageRoot'][2], 'tl_class'=>'w50'),
'sql' => "int(10) unsigned NOT NULL default '0'"
);
Expand Down
3 changes: 0 additions & 3 deletions src/EventListener/AbstractTableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ abstract class AbstractTableListener
{
protected string $table;

/**
* Constructor.
*/
public function __construct(string $table)
{
$this->table = $table;
Expand Down
29 changes: 5 additions & 24 deletions src/EventListener/CallbackSetupListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

namespace Terminal42\ChangeLanguage\EventListener;

use Contao\CoreBundle\ServiceAnnotation\Hook;
use Terminal42\ChangeLanguage\EventListener\BackendView\ArticleViewListener;
use Terminal42\ChangeLanguage\EventListener\BackendView\PageViewListener;
use Terminal42\ChangeLanguage\EventListener\BackendView\ParentChildViewListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\ArticleListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\CalendarEventsListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\FaqListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\MissingLanguageIconListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\NewsListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\PageInitializationListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\PageOperationListener;
use Terminal42\ChangeLanguage\EventListener\DataContainer\ParentTableListener;

/**
* @Hook("loadDataContainer")
*/
class CallbackSetupListener
{
private static array $listeners = [
'tl_page' => [
PageInitializationListener::class,
PageOperationListener::class,
PageViewListener::class,
],
'tl_article' => [
Expand Down Expand Up @@ -50,28 +49,10 @@ class CallbackSetupListener
],
];

private MissingLanguageIconListener $labelListener;

/**
* Constructor.
*/
public function __construct()
public function __invoke(string $table): void
{
$this->labelListener = new MissingLanguageIconListener();
}

/**
* Callback for loadDataContainer hook.
*
* @param string $table
*/
public function onLoadDataContainer($table): void
{
$this->labelListener->register($table);

if (\array_key_exists($table, self::$listeners)) {
foreach (self::$listeners[$table] as $class) {
/** @var AbstractTableListener $listener */
$listener = new $class($table);
$listener->register();
}
Expand Down
23 changes: 18 additions & 5 deletions src/EventListener/DataContainer/MissingLanguageIconListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Contao\CalendarEventsModel;
use Contao\CalendarModel;
use Contao\Config;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\Date;
use Contao\FaqCategoryModel;
use Contao\FaqModel;
Expand All @@ -18,8 +19,12 @@
use Contao\NewsModel;
use Contao\PageModel;
use Contao\StringUtil;
use Symfony\Component\Security\Core\Security;
use Terminal42\ChangeLanguage\Helper\LabelCallback;

/**
* @Hook("loadDataContainer")
*/
class MissingLanguageIconListener
{
private static array $callbacks = [
Expand All @@ -30,12 +35,17 @@ class MissingLanguageIconListener
'tl_faq' => 'onFaqChildRecords',
];

private Security $security;

public function __construct(Security $security)
{
$this->security = $security;
}

/**
* Override core labels to show missing language information.
*
* @param string $table
*/
public function register($table): void
public function __invoke(string $table): void
{
if (\array_key_exists($table, self::$callbacks)) {
LabelCallback::createAndRegister(
Expand Down Expand Up @@ -71,11 +81,14 @@ private function onPageLabel(array $args, $previousResult = null): string
return $this->generateLabelWithWarning($label);
}

$user = $this->security->getUser();

if (
isset($mainPage)
&& $mainPage instanceof PageModel
&& \is_array(BackendUser::getInstance()->pageLanguageLabels)
&& \in_array($page->rootId, BackendUser::getInstance()->pageLanguageLabels, false)
&& $user instanceof BackendUser
&& \is_array($user->pageLanguageLabels)
&& \in_array($page->rootId, $user->pageLanguageLabels, false)
) {
return sprintf(
'%s <span style="color:#999;padding-left:3px">(<a href="%s" title="%s" style="color:#999">%s</a>)</span>',
Expand Down
9 changes: 6 additions & 3 deletions src/EventListener/DataContainer/PageFieldsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Terminal42\ChangeLanguage\EventListener\DataContainer;

use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\Database;
use Contao\DataContainer;
use Contao\Input;
Expand All @@ -18,6 +19,8 @@ class PageFieldsListener
* @param mixed $value
*
* @return mixed
*
* @Callback(table="tl_page", target="fields.languageMain.load")
*/
public function onLoadLanguageMain($value, DataContainer $dc)
{
Expand Down Expand Up @@ -54,9 +57,9 @@ public function onLoadLanguageMain($value, DataContainer $dc)
*
* @param mixed $value
*
* @throws \RuntimeException
*
* @return mixed
*
* @Callback(table="tl_page", target="fields.languageMain.save")
*/
public function onSaveLanguageMain($value, DataContainer $dc)
{
Expand Down Expand Up @@ -91,7 +94,7 @@ public function onSaveLanguageMain($value, DataContainer $dc)
/**
* Gets list of options for language root selection (linking multiple fallback roots on different domains).
*
* @return array
* @Callback(table="tl_page", target="fields.languageRoot.options")
*/
public function onLanguageRootOptions(DataContainer $dc): array
{
Expand Down
10 changes: 9 additions & 1 deletion src/EventListener/DataContainer/PageInitializationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
namespace Terminal42\ChangeLanguage\EventListener\DataContainer;

use Contao\CoreBundle\DataContainer\PaletteManipulator;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\DataContainer;
use Contao\Input;
use Contao\PageModel;

/**
* @Hook("loadDataContainer")
*/
class PageInitializationListener
{
/**
* Register our own callbacks.
*/
public function register(): void
public function __invoke(string $table): void
{
if ('tl_page' !== $table) {
return;
}

$GLOBALS['TL_DCA']['tl_page']['config']['onload_callback'][] = function (DataContainer $dc): void {
$this->onLoad($dc);
};
Expand Down
Loading

0 comments on commit 99b6e9e

Please sign in to comment.