Skip to content

civi.afform.prefill

Summary

This Symfony event is dispatched when prefilling an Afform (Angular Form) with default/existing data. It allows developers to customize or override the IDs used to load default values for entities and join relationships on the form.

Definition

This event is dispatched via the Symfony EventDispatcher.

public static function onAfformPrefill(\Civi\Afform\Event\AfformPrefillEvent $event): void

Parameters

  • \Civi\Afform\Event\AfformPrefillEvent $event

Event Methods

  • getAfform(): array - Returns the main Afform configuration array.
  • getFormDataModel(): \Civi\Afform\FormDataModel - Returns the form data model instance.
  • getApiRequest(): \Civi\Api4\Generic\AbstractAction - Returns the active API request initiating the prefill processor.
  • getEntityType(): ?string - Returns the entity type of the current entity being prefilled (e.g. Contact, Activity).
  • getEntityName(): string - Returns the name of the entity on the form (e.g. Individual1, Activity1).
  • getEntityId(int $index = 0): mixed - Returns the ID of the entity at the given repetition index.
  • getEntityIds(?string $entityName = null): array - Returns all resolved ID values for the given entity name.
  • setEntityId(int $index, int|string $entityId): self - Sets the database ID for the entity instance at the specified index, causing its data to be loaded.
  • setJoinIds(int $index, string $joinEntity, array $joinIds): self - Sets the IDs for a joined entity (e.g. relationship or location blocks) at the specified index.

Example

use Civi\Afform\Event\AfformPrefillEvent;

public static function onAfformPrefill(AfformPrefillEvent $event): void {
  // If prefilling a specific entity named 'Individual1'
  if ($event->getEntityName() === 'Individual1') {
    // Force prefilling contact ID 123
    $event->setEntityId(0, 123);
  }
}