Skip to content

Commit

Permalink
Move configuration to DCA file
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead committed Dec 29, 2021
1 parent 214fd31 commit 7c8b11b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,6 @@ public function onLoadDataContainer(string $table): void
$key = $keys[0];
$fieldConfig = $GLOBALS['TL_DCA'][$table]['fields'][$key];

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

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

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

if (
!\array_key_exists('options', $fieldConfig) &&
!\array_key_exists('options_callback', $fieldConfig)
) {
$types = [];

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

$fieldConfig['options'] = $this->getFrontendModules($types);
}

if (!\array_key_exists('rootPages', $fieldConfig['eval'])) {
$fieldConfig['eval']['rootPages'] = $this->getRootPages();
}
Expand All @@ -119,21 +94,45 @@ public function onLoadDataContainer(string $table): void
);
}

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

$fieldConfig['save_callback'][] = [
static::class,
'onSaveCallback',
];
public function onOptionsCallback(DataContainer $dc): array
{
$options = [];
$types = $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['eval']['modules'] ?? [];

$fieldConfig['wizard'][] = [
static::class,
'onEditModule',
];
$statement = $this->connection->executeQuery(
"SELECT m.id, m.name
FROM tl_module m
WHERE m.type <> 'root_page_dependent_module' AND
m.pid = ?
ORDER BY m.name",
[$dc->activeRecord->pid]
);

// Save modified configuration back to global DCA
$GLOBALS['TL_DCA'][$table]['fields'][$key] = $fieldConfig;
if (\count($types)) {
$statement = $this->connection->executeQuery(
"SELECT m.id, m.name
FROM tl_module m
WHERE m.type IN (?) AND
m.type <> 'root_page_dependent_module' AND
m.pid = ?
ORDER BY m.name",
[$types, $dc->activeRecord->pid],
[Connection::PARAM_STR_ARRAY]
);
}

$modules = $statement->fetchAllAssociative();

foreach ($modules as $module) {
$options[$module['id']] = $module['name'];
}

return $options;
}

public function onSaveCallback($value, DataContainer $dataContainer)
Expand Down Expand Up @@ -197,35 +196,4 @@ private function getRootPages(): array

return $pages;
}

private function getFrontendModules(array $types = []): array
{
$statement = $this->connection->executeQuery("
SELECT m.id, m.name
FROM tl_module m
WHERE m.type <> 'root_page_dependent_module'
ORDER BY m.name
");

if (\count($types)) {
$statement = $this->connection->executeQuery(
"SELECT m.id, m.name
FROM tl_module m
WHERE m.type IN (?) AND m.type <> 'root_page_dependent_module'
ORDER BY m.name",
[$types],
[Connection::PARAM_STR_ARRAY]
);
}

$modules = $statement->fetchAllAssociative();

$list = [];

foreach ($modules as $module) {
$list[$module['id']] = $module['name'];
}

return $list;
}
}
12 changes: 11 additions & 1 deletion core-bundle/src/Resources/contao/dca/tl_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Contao\Backend;
use Contao\BackendUser;
use Contao\Controller;
use Contao\CoreBundle\EventListener\DataContainer\RootPageDependentModuleListener;
use Contao\CoreBundle\Exception\AccessDeniedException;
use Contao\CoreBundle\Security\ContaoCorePermissions;
use Contao\DataContainer;
Expand Down Expand Up @@ -640,7 +641,16 @@
(
'exclude' => true,
'inputType' => 'rootPageDependentModule',
'eval' => array('submitOnChange' => true, 'tl_class' => 'w50'),
'options_callback' => array(RootPageDependentModuleListener::class, 'onOptionsCallback'),
'save_callback' => array
(
array(RootPageDependentModuleListener::class, 'onSaveCallback')
),
'wizard' => array
(
array(RootPageDependentModuleListener::class, 'onEditModule')
),
'eval' => array('submitOnChange'=>true, 'tl_class'=>'w50', 'includeBlankOption'=>true),
'sql' => 'blob NULL'
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ If you did not request this e-mail, please contact the website administrator.
<source>Delete module ID %s</source>
</trans-unit>
<trans-unit id="tl_module.rootPageDependentModulesBlankOptionLabel">
<source>Please choose your module for root page %s</source>
<source>Please choose your module for %s</source>
</trans-unit>
<trans-unit id="tl_module.rootPageDependentModules.0">
<source>Module</source>
Expand Down

0 comments on commit 7c8b11b

Please sign in to comment.