Home > Developer Guide > Hooks and events > Entity Hooks
civi.entity.fields
Summary
This Symfony event is dispatched when retrieving metadata fields for a given entity. It replaces the deprecated fields_callback callback. Developers can listen to this event generally, or target a specific entity using the event name format civi.entity.fields::[EntityName] (e.g. civi.entity.fields::Activity).
Definition
This event is dispatched via the Symfony EventDispatcher.
// Generic listener:
public static function onEntityFields(\Civi\Core\Event\GenericHookEvent $event): void
// Targeted listener for a specific entity (e.g. Activity):
public static function onActivityFields(\Civi\Core\Event\GenericHookEvent $event): void
Parameters
\Civi\Core\Event\GenericHookEvent $event
Event Properties
Since this event uses GenericHookEvent, properties are accessed dynamically:
$event->entity:string- The name of the entity (e.g.,'Activity').$event->fields:array- A reference to the array of field definitions keyed by field name. Alterable.
Example
use Civi\Core\Event\GenericHookEvent;
public static function onActivityFields(GenericHookEvent $event): void {
// Add or modify a field metadata definition
$event->fields['my_custom_activity_field'] = [
'name' => 'my_custom_activity_field',
'type' => \CRM_Utils_Type::T_STRING,
'title' => ts('My Custom Field'),
'description' => ts('A custom field added dynamically'),
];
}