Skip to content

Commit

Permalink
Refactored logic into widget
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead committed Jan 13, 2022
1 parent 9920305 commit c742a6b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,100 +10,31 @@
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\EventListener\DataContainer;
namespace Contao\CoreBundle\EventListener\Widget;

use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\DataContainer;
use Contao\Image;
use Contao\StringUtil;
use Doctrine\DBAL\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class RootPageDependentSelectListener
{
private Connection $connection;
private TranslatorInterface $translator;
private ScopeMatcher $scopeMatcher;
private RequestStack $requestStack;
private CsrfTokenManagerInterface $csrfTokenManager;
private string $csrfTokenName;

public function __construct(Connection $connection, TranslatorInterface $translator, ScopeMatcher $scopeMatcher, RequestStack $requestStack, CsrfTokenManagerInterface $csrfTokenManager, string $csrfTokenName)
public function __construct(Connection $connection, TranslatorInterface $translator, CsrfTokenManagerInterface $csrfTokenManager, string $csrfTokenName)
{
$this->connection = $connection;
$this->translator = $translator;
$this->scopeMatcher = $scopeMatcher;
$this->requestStack = $requestStack;
$this->csrfTokenManager = $csrfTokenManager;
$this->csrfTokenName = $csrfTokenName;
}

/**
* @Hook("loadDataContainer")
*/
public function onLoadDataContainer(string $table): void
{
/** @var Request $request */
$request = $this->requestStack->getCurrentRequest();

if (!$request instanceof Request || !$this->scopeMatcher->isBackendRequest($request)) {
return;
}

$fields = $GLOBALS['TL_DCA'][$table]['fields'];

if (
!\is_array($fields) ||
!\in_array('rootPageDependentSelect', array_column($fields, 'inputType'), true)
) {
return;
}

foreach ($fields as $key => $field) {
if (!\array_key_exists('inputType', $field)) {
unset($fields[$key]);
}
}

$affectedFields = array_keys(
array_column($fields, 'inputType'),
'rootPageDependentSelect',
true
);

foreach ($affectedFields as $affectedField) {
$field = \array_slice($fields, $affectedField, 1);
$keys = array_keys($field);

$key = $keys[0];
$fieldConfig = $GLOBALS['TL_DCA'][$table]['fields'][$key];

if (!\array_key_exists('eval', $fieldConfig)) {
$fieldConfig['eval'] = [];
}

if (!\array_key_exists('rootPages', $fieldConfig['eval'])) {
$fieldConfig['eval']['rootPages'] = $this->getRootPages();
}

if (!\array_key_exists('blankOptionLabel', $fieldConfig['eval'])) {
$fieldConfig['eval']['blankOptionLabel'] = $this->translator->trans(
sprintf('tl_module.%sBlankOptionLabel', $key),
[],
'contao_module'
);
}

// Save modified configuration back to global DCA
$GLOBALS['TL_DCA'][$table]['fields'][$key] = $fieldConfig;
}
}

/**
* @Callback(table="tl_module", target="fields.rootPageDependentModules.options")
*/
Expand All @@ -113,7 +44,7 @@ public function optionsCallback(DataContainer $dc): array
$types = $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['eval']['modules'] ?? [];
$hasTypes = \count($types) > 0;

$result = $this->connection->executeQuery(
$rows = $this->connection->executeQuery(
"SELECT m.id, m.name, m.type
FROM tl_module m
WHERE m.type <> 'root_page_dependent_modules' AND
Expand All @@ -122,9 +53,7 @@ public function optionsCallback(DataContainer $dc): array
[$dc->activeRecord->pid]
);

$modules = $result->fetchAllAssociative();

foreach ($modules as $module) {
foreach ($rows->iterateAssociative() as $module) {
if ($hasTypes && !\in_array($module['type'], $types, true)) {
continue;
}
Expand Down Expand Up @@ -194,11 +123,11 @@ private function getRootPages(): array
ORDER BY p.sorting ASC
');

$rootPages = $statement->executeQuery()->fetchAllAssociative();
$rows = $statement->executeQuery();

$pages = [];

foreach ($rootPages as $rootPage) {
foreach ($rows->iterateAssociative() as $rootPage) {
$pages[$rootPage['id']] = sprintf('%s (%s)', $rootPage['title'], $rootPage['language']);
}

Expand Down
18 changes: 8 additions & 10 deletions core-bundle/src/Resources/config/listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ services:
arguments:
- '@database_connection'

contao.listener.data_container.root_page_dependent_select:
class: Contao\CoreBundle\EventListener\DataContainer\RootPageDependentSelectListener
arguments:
- '@database_connection'
- '@translator'
- '@contao.routing.scope_matcher'
- '@request_stack'
- '@contao.csrf.token_manager'
- '%contao.csrf_token_name%'

contao.listener.data_container.theme_templates:
class: Contao\CoreBundle\EventListener\DataContainer\ThemeTemplatesListener
arguments:
Expand Down Expand Up @@ -463,6 +453,14 @@ services:
arguments:
- '@translator'

contao.listener.widget.root_page_dependent_select:
class: Contao\CoreBundle\EventListener\Widget\RootPageDependentSelectListener
arguments:
- '@database_connection'
- '@translator'
- '@contao.csrf.token_manager'
- '%contao.csrf_token_name%'

# Backwards compatibility
Contao\CoreBundle\EventListener\DataContainer\ContentCompositionListener:
alias: contao.listener.data_container.content_composition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

namespace Contao;

/**
* @property array $rootPages
*/
class RootPageDependentSelect extends SelectMenu
{
public function generate(): string
{
$fields = array();
$label = $this->strLabel;
$originalLabel = $this->strLabel;
$cssClasses = 'tl_select tl_chosen';

$rootPages = $this->rootPages;
$rootPages = PageModel::findByPid(0, array('order' => 'sorting'));

$wizard = StringUtil::deserialize($this->wizard);

foreach ($rootPages as $id => $label)
$this->blankOptionLabel = $GLOBALS['TL_LANG']['tl_module'][sprintf('%sBlankOptionLabel', $this->name)];

foreach ($rootPages as $rootPage)
{
$label = sprintf('%s (%s)', $rootPage->title, $rootPage->language);
$this->arrOptions[0]['label'] = sprintf($this->blankOptionLabel, $label);
$this->strLabel = $label;

Expand All @@ -37,12 +37,12 @@ public function generate(): string
$cssClasses,
($this->strClass ? ' ' . $this->strClass : ''),
$this->getAttributes(),
implode('', $this->getOptions($id)),
$wizard[$id] ?? ''
implode('', $this->getOptions($rootPage->id)),
$wizard[$rootPage->id] ?? ''
);
}

$this->strLabel = $label;
$this->strLabel = $originalLabel;

return implode('', $fields);
}
Expand Down

0 comments on commit c742a6b

Please sign in to comment.