getContentElement

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.

Parameters

  1. \Contao\ContentModel $contentModel

    Database result set from table tl_content.

  2. string $buffer

    The output buffer of the generated content element.

  3. object $element

    An instance of the content element’s class that is registered for this element’s type.

Return Values

The (modified) content of the content element as a string.

Example

// src/EventListener/GetContentElementListener.php
namespace App\EventListener;

use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\ContentElement;
use Contao\ContentModel;

/**
 * @Hook("getContentElement")
 */
class GetContentElementListener
{
    public function __invoke(ContentModel $contentModel, string $buffer, $element): string
    {
        // Modify or create new $buffer here …

        return $buffer;
    }
}

References