loadFormField

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.

Parameters

  1. \Contao\Widget $widget

    Instance of the current front end widget.

  2. string $formId

    Alias of the current form. Used in the value attribute of the hidden form field FORM_SUBMIT. Don’t confuse with $objForm->id.

  3. array $formData

    The form configuration, a tl_form record.

  4. \Contao\Form $form

    The form instance.

Return Values

Return the $widget instance. You can override it’s settings or even create your own widget instead.

Example

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

use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\Form;
use Contao\Widget;
use Terminal42\ServiceAnnotationBundle\ServiceAnnotationInterface;

class LoadFormFieldListener implements ServiceAnnotationInterface
{
    /**
     * @Hook("loadFormField")
     */
    public function onLoadFormField(Widget $widget, string $formId, array $formData, Form $form): Widget
    {
        if ('myForm' === $formId) {
            $widget->class.= ' myclass';
        }

        return $widget;
    }
}

References