-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a root page dependent module selector (see #3613)
Description ----------- To-Do ----- - [x] Naming - [x] Tests The idea is to get rid of multiple page layouts (or inserttags with module IDs in combination with `{{iflng::*}}` tags) for multiple root pages (and languages). Usage ----- You can now easy configure you modules for every root page available: <img width="1232" alt="Bildschirmfoto 2021-12-15 um 22 36 21" src="https://user-images.githubusercontent.com/754921/146269104-58ae3bb8-f818-495b-8bd1-fa771213f4c4.png"> and include this module in your layout: <img width="830" alt="Bildschirmfoto 2021-12-15 um 22 39 41" src="https://user-images.githubusercontent.com/754921/146269130-9ede184a-c4d4-4efe-be76-7177a1c5255d.png"> This module will then render the configured module for each configured root page. Developers ---------- The module provides an additional input type to use in your own code: **Use the default configuration** (loads all available modules): ```php <?php // config/dca/tl_modules.php $GLOBALS['TL_DCA']['tl_module']['fields'] += [ 'myRootPageDependentModule' => [ 'inputType' => 'rootPageDependentSelect', 'eval' => [ 'tl_class' => 'w50', ], 'sql' => 'blob NULL', ], ]; ``` There is even more and you can customize this widget to your needs: **use `options`** (for custom list of modules): ```php <?php // config/dca/tl_modules.php $GLOBALS['TL_DCA']['tl_module']['fields'] += [ 'myRootPageDependentModule' => [ 'inputType' => 'rootPageDependentSelect', 'options' => [ 0 => 'My module 0', 1 => 'My module 1', ], 'eval' => [ 'tl_class' => 'w50', ], 'sql' => 'blob NULL', ], ]; ``` **use `options_callback`** (for custom list of modules): ```php <?php // config/dca/tl_modules.php $GLOBALS['TL_DCA']['tl_module']['fields'] += [ 'myRootPageDependentModule' => [ 'inputType' => 'rootPageDependentSelect', 'options_callback' => ['my.service_id', 'methodName'], 'eval' => [ 'tl_class' => 'w50', ], 'sql' => 'blob NULL', ], ]; // you can also use this with service tagging, see https://docs.contao.org/dev/framework/dca/#registering-callbacks ``` **use `eval['modules']`** (to filter for custom types): ```php <?php // config/dca/tl_modules.php $GLOBALS['TL_DCA']['tl_module']['fields'] += [ 'myRootPageDependentModule' => [ 'inputType' => 'rootPageDependentSelect', 'eval' => [ 'tl_class' => 'w50', 'modules' => [ 'navigation', 'customnav', 'search', 'html', 'myCustomModule', ], ], 'sql' => 'blob NULL', ], ]; ``` **Change the label for the blank option**: ```php <?php // config/dca/tl_modules.php $GLOBALS['TL_DCA']['tl_module']['fields'] += [ 'myRootPageDependentModule' => [ 'inputType' => 'rootPageDependentSelect', 'eval' => [ 'tl_class' => 'w50', 'blankOptionLabel' => 'My Label' ], 'sql' => 'blob NULL', ], ]; ``` _Note:_ If you just want to change the label for the blank option of the default field, the key is: `tl_module.rootPageDependentModulesBlankOptionLabel`. Commits ------- b637e97 Add new language dependent module 056913d Fix method name 0d9824b Make it root page dependent d78957c Remove loadLanguageFile call 214fd31 Remove use 7c8b11b Move configuration to DCA file 91235b8 Fix merge services.yml 7447cde Fix merge of listener.yml f09c231 Fix listener.yml 8dd0d75 Fix services.yml acfb8cf Fixes 1672060 Fix CS ad83e0d Fix listener.yml 4bd2ead Fix services.yml e8d2d79 Merge branch '4.x' of github.com:contao/contao into feature/language-dependent-module-configuration 83aab2e Fix sorting 127497d Merge branch '4.x' of github.com:contao/contao into feature/language-dependent-module-configuration 243920b Merge branch '4.x' of github.com:contao/contao into feature/language-dependent-module-configuration 67fde19 Add unit test for controller 6082295 More tests d1e7104 CS 3fcbcc2 CS 0894a40 Remove unused f6b8d50 Remove obsolete registration b638dd2 Use Hook annotation 654cd29 Adjust wording 86cbde1 Remove template e97704c Adjust palette 971a70a Tag response 6c85b7b Use callback annotation 68256d2 Rename and fixes 126ed6a Rename methods 64833ff Add test for wizardCallback a91283f Fix CS 8f8cbc7 Fix tests on PHP8 d1a508f Cleanup 873e440 Merge branch '4.x' of github.com:contao/contao into feature/language-dependent-module-configuration d960754 Test save callback 5c9f0a5 Test options callback d60e29f Merge branch '4.x' of github.com:contao/contao into feature/language-dependent-module-configuration 6bbe3f3 CS fix 4d18783 Do not mark for internal caching 9920305 Merge branch '4.x' of github.com:contao/contao into feature/language-dependent-module-configuration c742a6b Refactored logic into widget 0309d6b CS 2b801f2 Remove comment de2b7dc Use ContaoCsrfTokenManager 9f56fda Search for type='root' instead of pid=0 a4762f9 Simplify 24f07a4 Merge branch '4.x' of github.com:contao/contao into feature/language-dependent-module-configuration e2174c9 Add widget test c48ea1b CS a6e5469 Fix CS d80d5e6 Remove useless mocks 9f2d99d Use PageModels & Collection in test ae1520c CS a562e5d CS f2984b7 Adjusted controller 496e90a Remove buggy option groups feature 7b69035 CS 6335a6b Extend from AbstractFrontendModuleController (#4) 0442d14 Add test for Controller 728c12f Register the be_wildcard template in the unit test 91d6559 Canonicalize the path Co-authored-by: Leo Feyer <github@contao.org>
- Loading branch information
Showing
14 changed files
with
891 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
core-bundle/src/Controller/FrontendModule/RootPageDependentModulesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Controller\FrontendModule; | ||
|
||
use Contao\Controller; | ||
use Contao\ModuleModel; | ||
use Contao\StringUtil; | ||
use Contao\Template; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class RootPageDependentModulesController extends AbstractFrontendModuleController | ||
{ | ||
public function __invoke(Request $request, ModuleModel $model, string $section, array $classes = null): Response | ||
{ | ||
if ($this->container->get('contao.routing.scope_matcher')->isBackendRequest($request)) { | ||
return $this->getBackendWildcard($model); | ||
} | ||
|
||
if (!$pageModel = $this->getPageModel()) { | ||
return new Response(''); | ||
} | ||
|
||
$modules = StringUtil::deserialize($model->rootPageDependentModules); | ||
|
||
if (empty($modules) || !\is_array($modules) || !\array_key_exists($pageModel->rootId, $modules)) { | ||
return new Response(''); | ||
} | ||
|
||
$controller = $this->container->get('contao.framework')->getAdapter(Controller::class); | ||
$content = $controller->getFrontendModule($modules[$pageModel->rootId]); | ||
|
||
$this->tagResponse($model); | ||
|
||
return new Response($content); | ||
} | ||
|
||
public function getResponse(Template $template, ModuleModel $model, Request $request): Response | ||
{ | ||
throw new \LogicException('This method should never be called'); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
core-bundle/src/EventListener/Widget/RootPageDependentSelectListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\EventListener\Widget; | ||
|
||
use Contao\CoreBundle\Csrf\ContaoCsrfTokenManager; | ||
use Contao\CoreBundle\ServiceAnnotation\Callback; | ||
use Contao\DataContainer; | ||
use Contao\Image; | ||
use Contao\StringUtil; | ||
use Doctrine\DBAL\Connection; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class RootPageDependentSelectListener | ||
{ | ||
private Connection $connection; | ||
private TranslatorInterface $translator; | ||
private ContaoCsrfTokenManager $csrfTokenManager; | ||
|
||
public function __construct(Connection $connection, TranslatorInterface $translator, ContaoCsrfTokenManager $csrfTokenManager) | ||
{ | ||
$this->connection = $connection; | ||
$this->translator = $translator; | ||
$this->csrfTokenManager = $csrfTokenManager; | ||
} | ||
|
||
/** | ||
* @Callback(table="tl_module", target="fields.rootPageDependentModules.options") | ||
*/ | ||
public function optionsCallback(DataContainer $dc): array | ||
{ | ||
$options = []; | ||
$types = $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['eval']['modules'] ?? []; | ||
$hasTypes = \count($types) > 0; | ||
|
||
$rows = $this->connection->executeQuery( | ||
"SELECT m.id, m.name, m.type | ||
FROM tl_module m | ||
WHERE m.type != 'root_page_dependent_modules' AND m.pid = ? | ||
ORDER BY m.name", | ||
[$dc->activeRecord->pid] | ||
); | ||
|
||
foreach ($rows->iterateAssociative() as $module) { | ||
if ($hasTypes && !\in_array($module['type'], $types, true)) { | ||
continue; | ||
} | ||
|
||
$options[$module['id']] = $module['name']; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @Callback(table="tl_module", target="fields.rootPageDependentModules.save") | ||
*/ | ||
public function saveCallback($value, DataContainer $dataContainer): string | ||
{ | ||
$values = StringUtil::deserialize($value); | ||
|
||
if (!\is_array($values)) { | ||
return $value; | ||
} | ||
|
||
$newValues = []; | ||
$availableRootPages = array_keys($this->getRootPages()); | ||
|
||
foreach ($values as $v) { | ||
$newValues[array_shift($availableRootPages)] = $v; | ||
} | ||
|
||
return serialize($newValues); | ||
} | ||
|
||
/** | ||
* @Callback(table="tl_module", target="fields.rootPageDependentModules.wizard") | ||
*/ | ||
public function wizardCallback(DataContainer $dc): string | ||
{ | ||
$wizards = []; | ||
$values = StringUtil::deserialize($dc->value, true); | ||
|
||
if (empty($values)) { | ||
return ''; | ||
} | ||
|
||
foreach ($values as $rootPage => $id) { | ||
if ('' === $id) { | ||
continue; | ||
} | ||
|
||
$title = $this->translator->trans('tl_content.editalias', [$id], 'contao_content'); | ||
|
||
$wizards[$rootPage] = ' <a href="contao/main.php?do=themes&table=tl_module&act=edit&id='.$id.'&popup=1&nb=1&rt='.$this->csrfTokenManager->getDefaultTokenValue().'" | ||
title="'.StringUtil::specialchars($title).'" | ||
onclick="Backend.openModalIframe({\'title\':\''.StringUtil::specialchars(str_replace("'", "\\'", $title)).'\',\'url\':this.href});return false">'.Image::getHtml('alias.svg', $title).'</a>'; | ||
} | ||
|
||
return serialize($wizards); | ||
} | ||
|
||
private function getRootPages(): array | ||
{ | ||
$statement = $this->connection->prepare(' | ||
SELECT p.id, p.title, p.language | ||
FROM tl_page p | ||
WHERE p.pid = 0 | ||
ORDER BY p.sorting ASC | ||
'); | ||
|
||
$rows = $statement->executeQuery(); | ||
$pages = []; | ||
|
||
foreach ($rows->iterateAssociative() as $rootPage) { | ||
$pages[$rootPage['id']] = sprintf('%s (%s)', $rootPage['title'], $rootPage['language']); | ||
} | ||
|
||
return $pages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.