Skip to content

civi.afform.submit

Summary

This Symfony event is dispatched during Afform submission processing. It handles the saving of individual form entities. Developers can use this event to customize records before they are saved, perform custom save logic, or intercept submission actions.

Definition

This event is dispatched via the Symfony EventDispatcher.

public static function onAfformSubmit(\Civi\Afform\Event\AfformSubmitEvent $event): void

Parameters

  • \Civi\Afform\Event\AfformSubmitEvent $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 submit processor.
  • getRecords(): array - Returns the current array of records to be saved for this entity.
  • setRecords(array $records): self - Updates the array of records to be saved.
  • getSaved(): array - Returns details/results of saved records.
  • setSaved(int $index, array $savedValues): self - Sets the saved values for a record at the specified index (usually output of API4 save).
  • getSubmittedValues(): array - Returns all submitted entity and field values for the form.
  • getEntityType(): ?string - Returns the entity type of the current entity (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 database ID of the entity at the given repetition index.
  • getEntityIds(?string $entityName = null): array - Returns all resolved database 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.
  • setJoinIds(int $index, string $joinEntity, array $joinIds): self - Sets the join IDs for the entity instance at the specified index.

Example

use Civi\Afform\Event\AfformSubmitEvent;

public static function onAfformSubmit(AfformSubmitEvent $event): void {
  if ($event->getEntityName() === 'Individual1') {
    $records = $event->getRecords();
    foreach ($records as $index => &$record) {
      // Modify a field value before the record is saved by core
      $record['fields']['custom_field'] = 'preprocessed_value';
    }
    $event->setRecords($records);
  }
}