The getFrontendModule
hook allows to manipulate the generation of the front end
modules.
This hook is only executed, when a front end module is rendered directly via
\Contao\Controller::getFrontendModule()
. This will not be the case if a module
is inserted into a page via the Module content element for example. The hook is
executed when a front end module is rendered via the page layout or via an insert
tag - or in some cases when a module dynamically inserts another module (e.g. when
the news, events or faq list module dynamically shows the selected reader module).
You will need to implement the getContentElement
hook as well, if you want to cover all bases.
This hook is also executed for forms that are integrated into a page layout via a front end module.
\Contao\ModuleModel $model
Database result of the front end module as a \Contao\ModuleModel
instance.
string $buffer
The generated front end module buffer.
object $module
An instance of the front end module’s class that is registered for this module’s type.
Return $buffer
or your custom modification.
// src/EventListener/GetFrontendModuleListener.php
namespace App\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\Form;
use Contao\Module;
use Contao\ModuleModel;
#[AsHook('getFrontendModule')]
class GetFrontendModuleListener
{
public function __invoke(ModuleModel $model, string $buffer, $module): string
{
// Wrap a specific module in an additional wrapper div
if (2 === (int) $model->id) {
return '<div class="module">' . $buffer . '</div>';
}
return $buffer;
}
}