The loadFormField
hook is triggered when a form field is loaded. It passes the
widget object, the form ID and the form data as arguments and expects a widget
object as return value. This hook can be used to dynamically alter or extend a form
field of the form generator.
\Contao\Widget $widget
Instance of the current front end widget.
string $formId
Alias of the current form. Used in the value
attribute of the hidden form
field FORM_SUBMIT
. Don’t confuse with $form->id
.
array $formData
The form configuration, a tl_form
record.
\Contao\Form $form
The form instance.
Return the $widget
instance. You can override it’s settings or even create
your own widget instead.
// src/EventListener/LoadFormFieldListener.php
namespace App\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\Form;
use Contao\Widget;
#[AsHook('loadFormField')]
class LoadFormFieldListener
{
public function __invoke(Widget $widget, string $formId, array $formData, Form $form): Widget
{
if ('myForm' === $formId) {
$widget->class.= ' myclass';
}
return $widget;
}
}