getForm

The getForm hook allows to manipulate the generation of the forms. It passes the form object and the current form output buffer as arguments and expects a string as return value.

This hook is only executed, when a form is rendered directly via \Contao\Controller::getForm(). Within the Contao core this is currently only the case if a form is integrated via the {{insert_form::*}} insert tag. The content element and module Form render the form directly and thus the hook is not executed. You will need to implement the getContentElement and getFrontendModule hook as well, if you want to cover all bases.

Parameters

  1. \Contao\FormModel $formModel

    Database result set from table tl_form as a \Contao\FormModel instance.

  2. string $buffer

    The generated form buffer.

  3. \Contao\Form $form

    The Form object.

Return Values

Return $buffer or your custom modification.

Example

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

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\Form;
use Contao\FormModel;

#[AsHook('getForm')]
class GetFormListener
{
    public function __invoke(FormModel $formModel, string $buffer, Form $form): string
    {
        if (2 === (int) $form->id) {
            // Do something …
        }

        return $buffer;
    }
}

References