Home > Developer Guide > Hooks and events > FormBuilder Hooks
civi.afform.get
Summary
This Symfony event allows developers to register or modify Afforms programmatically. It is dispatched during the Afform APIv4 get action.
Definition
This event is dispatched via the Symfony EventDispatcher.
public static function onAfformGet(\Civi\Core\Event\GenericHookEvent $event): void
Parameters
\Civi\Core\Event\GenericHookEvent $event
Event Properties
Since this event uses GenericHookEvent, properties are accessed dynamically:
$event->afforms:array- A reference to the array of Afform definitions, keyed by form name. Alterable.$event->getNames:array- Filters for names requested by the API (includesname,module_name,directive_name). Used for optimization.$event->getTypes:array- Filters for types of Afforms requested by the API. Used for optimization.$event->getLayout:bool- Indicates if the API request expects the layout (HTML markup) of the form to be returned. Used for optimization.
Availability
- Available since CiviCRM 5.51
Example
use Civi\Core\Event\GenericHookEvent;
public static function onAfformGet(GenericHookEvent $event): void {
$getNames = $event->getNames;
// Optimize: Only generate if our specific form is requested (or if no name filter is applied)
if (isset($getNames['name']) && !in_array('myCustomAfform', $getNames['name'])) {
return;
}
$event->afforms['myCustomAfform'] = [
'name' => 'myCustomAfform',
'type' => 'form',
'title' => ts('My Programmatic Afform'),
'description' => ts('This form was generated dynamically by an event listener.'),
'icon' => 'fa-cog',
'permission' => ['access CiviCRM'],
];
if ($event->getLayout) {
$event->afforms['myCustomAfform']['layout'] = '<div><h3>My Form</h3><af-field name="first_name" /></div>';
}
}