Skip to content

Commit

Permalink
Add a root page dependent module selector (see #3613)
Browse files Browse the repository at this point in the history
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
bytehead and leofeyer committed Jan 17, 2022
1 parent 4c62406 commit 803ca9b
Show file tree
Hide file tree
Showing 14 changed files with 891 additions and 32 deletions.
13 changes: 10 additions & 3 deletions core-bundle/src/Controller/AbstractFragmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,18 @@ protected function render(string $view, array $parameters = [], Response $respon
if (null === $response) {
$response = new Response();

// Mark this response to affect the caching of the current page but remove any default cache headers
$response->headers->set(SubrequestCacheSubscriber::MERGE_CACHE_HEADER, '1');
$response->headers->remove('Cache-Control');
$this->markResponseForInternalCaching($response);
}

return parent::render($view, $parameters, $response);
}

/**
* Marks the response to affect the caching of the current page but removes any default cache header.
*/
protected function markResponseForInternalCaching(Response $response): void
{
$response->headers->set(SubrequestCacheSubscriber::MERGE_CACHE_HEADER, '1');
$response->headers->remove('Cache-Control');
}
}
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');
}
}
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&amp;table=tl_module&amp;act=edit&amp;id='.$id.'&amp;popup=1&amp;nb=1&amp;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;
}
}
4 changes: 4 additions & 0 deletions core-bundle/src/Resources/config/controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ services:
tags:
- controller.service_arguments

Contao\CoreBundle\Controller\FrontendModule\RootPageDependentModulesController:
tags:
- { name: contao.frontend_module, category: miscellaneous }

Contao\CoreBundle\Controller\FrontendModule\TemplateController:
tags:
- { name: contao.frontend_module, category: miscellaneous }
Expand Down
7 changes: 7 additions & 0 deletions core-bundle/src/Resources/config/listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,13 @@ services:
arguments:
- '@translator'

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

# Backwards compatibility
Contao\CoreBundle\EventListener\DataContainer\ContentCompositionListener:
alias: contao.listener.data_container.content_composition
Expand Down
58 changes: 30 additions & 28 deletions core-bundle/src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
use Contao\PurgeData;
use Contao\RadioButton;
use Contao\RadioTable;
use Contao\RootPageDependentSelect;
use Contao\SectionWizard;
use Contao\SelectMenu;
use Contao\SerpPreview;
Expand Down Expand Up @@ -336,34 +337,35 @@
// Back end form fields
$GLOBALS['BE_FFL'] = array
(
'text' => TextField::class,
'password' => Password::class,
'textStore' => TextStore::class,
'textarea' => TextArea::class,
'select' => SelectMenu::class,
'checkbox' => CheckBox::class,
'checkboxWizard' => CheckBoxWizard::class,
'radio' => RadioButton::class,
'radioTable' => RadioTable::class,
'inputUnit' => InputUnit::class,
'trbl' => TrblField::class,
'chmod' => ChmodTable::class,
'picker' => Picker::class,
'pageTree' => PageTree::class,
'pageSelector' => PageSelector::class,
'fileTree' => FileTree::class,
'fileSelector' => FileSelector::class,
'fileUpload' => Upload::class,
'tableWizard' => TableWizard::class,
'listWizard' => ListWizard::class,
'optionWizard' => OptionWizard::class,
'moduleWizard' => ModuleWizard::class,
'keyValueWizard' => KeyValueWizard::class,
'imageSize' => ImageSize::class,
'timePeriod' => TimePeriod::class,
'metaWizard' => MetaWizard::class,
'sectionWizard' => SectionWizard::class,
'serpPreview' => SerpPreview::class
'text' => TextField::class,
'password' => Password::class,
'textStore' => TextStore::class,
'textarea' => TextArea::class,
'select' => SelectMenu::class,
'checkbox' => CheckBox::class,
'checkboxWizard' => CheckBoxWizard::class,
'radio' => RadioButton::class,
'radioTable' => RadioTable::class,
'inputUnit' => InputUnit::class,
'trbl' => TrblField::class,
'chmod' => ChmodTable::class,
'picker' => Picker::class,
'pageTree' => PageTree::class,
'pageSelector' => PageSelector::class,
'fileTree' => FileTree::class,
'fileSelector' => FileSelector::class,
'fileUpload' => Upload::class,
'tableWizard' => TableWizard::class,
'listWizard' => ListWizard::class,
'optionWizard' => OptionWizard::class,
'moduleWizard' => ModuleWizard::class,
'keyValueWizard' => KeyValueWizard::class,
'imageSize' => ImageSize::class,
'timePeriod' => TimePeriod::class,
'metaWizard' => MetaWizard::class,
'sectionWizard' => SectionWizard::class,
'serpPreview' => SerpPreview::class,
'rootPageDependentSelect' => RootPageDependentSelect::class
);

// Front end form fields
Expand Down
10 changes: 9 additions & 1 deletion core-bundle/src/Resources/contao/dca/tl_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
'template' => '{title_legend},name,headline,type;{template_legend},data,customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID',
'rssReader' => '{title_legend},name,headline,type;{config_legend},rss_feed,numberOfItems,perPage,skipFirst,rss_cache;{template_legend:hide},rss_template;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID',
'two_factor' => '{title_legend},name,headline,type;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},cssID',
'root_page_dependent_modules' => '{title_legend},name,type;{config_legend},rootPageDependentModules;{template_legend:hide},customTpl;{protected_legend:hide},protected'
),

// Subpalettes
Expand Down Expand Up @@ -640,7 +641,14 @@
'inputType' => 'text',
'eval' => array('multiple'=>true, 'size'=>2, 'tl_class'=>'w50'),
'sql' => "varchar(255) NOT NULL default ''"
)
),
'rootPageDependentModules' => array
(
'exclude' => true,
'inputType' => 'rootPageDependentSelect',
'eval' => array('submitOnChange'=>true, 'includeBlankOption'=>true, 'tl_class'=>'w50'),
'sql' => 'blob NULL'
),
)
);

Expand Down
6 changes: 6 additions & 0 deletions core-bundle/src/Resources/contao/languages/en/modules.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@
<trans-unit id="FMD.two_factor.1">
<source>Generates a form to set up or change two-factor authentication</source>
</trans-unit>
<trans-unit id="FMD.root_page_dependent_modules.0">
<source>Root page dependent modules</source>
</trans-unit>
<trans-unit id="FMD.root_page_dependent_module.1">
<source>Generates different modules depending on the root page</source>
</trans-unit>
</body>
</file>
</xliff>
9 changes: 9 additions & 0 deletions core-bundle/src/Resources/contao/languages/en/tl_module.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@
<trans-unit id="tl_module.data.1">
<source>These values are available as &lt;code&gt;$this->data&lt;/code&gt; in the template.</source>
</trans-unit>
<trans-unit id="tl_module.rootPageDependentModules.0">
<source>Modules</source>
</trans-unit>
<trans-unit id="tl_module.rootPageDependentModules.1">
<source>Please select a module for every root page.</source>
</trans-unit>
<trans-unit id="tl_module.rootPageDependentModulesBlankOptionLabel">
<source>Module for "%s"</source>
</trans-unit>
<trans-unit id="tl_module.title_legend">
<source>Title and type</source>
</trans-unit>
Expand Down
1 change: 1 addition & 0 deletions core-bundle/src/Resources/contao/models/ModuleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
* @property string|array|null $groups
* @property string|boolean $guests
* @property string|array $cssID
* @property string|array|null $rootPageDependentModules
*
* @property string $typePrefix
* @property array $classes
Expand Down

0 comments on commit 803ca9b

Please sign in to comment.