The getContentElement
hook is triggered when a content element is rendered.
It passes the database object, the buffer string and the content element object
as arguments and expects a buffer string as return value.
This hook is executed for all content elements, i.e. also for forms and front end modules that are integrated into a page via a content element.
\Contao\ContentModel $contentModel
Database result set from table tl_content
.
string $buffer
The output buffer of the generated content element.
object $element
An instance of the content element’s class that is registered for this element’s type.
The (modified) content of the content element as a string.
// src/EventListener/GetContentElementListener.php
namespace App\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\ContentElement;
use Contao\ContentModel;
#[AsHook('getContentElement')]
class GetContentElementListener
{
public function __invoke(ContentModel $contentModel, string $buffer, $element): string
{
// Modify or create new $buffer here …
return $buffer;
}
}