/** @var CiviEventDispatcher $dispatcher */
$dispatcher = static::getBootService('dispatcher.boot');
+ // Sometimes, you have a generic event ('hook_pre') and wish to fire more targeted aliases ('hook_pre::MyEntity') to allow shorter subscriber lists.
+ $aliasEvent = function($eventName, $fieldName) {
+ return function($e) use ($eventName, $fieldName) {
+ \Civi::dispatcher()->dispatch($eventName . "::" . $e->{$fieldName}, $e);
+ };
+ };
+
$dispatcher->addListener('civi.core.install', ['\Civi\Core\InstallationCanary', 'check']);
$dispatcher->addListener('civi.core.install', ['\Civi\Core\DatabaseInitializer', 'initialize']);
$dispatcher->addListener('civi.core.install', ['\Civi\Core\LocalizationInitializer', 'initialize']);
$dispatcher->addListener('hook_civicrm_post', ['\CRM_Core_Transaction', 'addPostCommit'], -1000);
- $dispatcher->addListener('hook_civicrm_pre', ['\Civi\Core\Event\PreEvent', 'dispatchSubevent'], 100);
+ $dispatcher->addListener('hook_civicrm_pre', $aliasEvent('hook_civicrm_pre', 'entity'), 100);
$dispatcher->addListener('civi.dao.preDelete', ['\CRM_Core_BAO_EntityTag', 'preDeleteOtherEntity']);
- $dispatcher->addListener('hook_civicrm_post', ['\Civi\Core\Event\PostEvent', 'dispatchSubevent'], 100);
+ $dispatcher->addListener('hook_civicrm_post', $aliasEvent('hook_civicrm_post', 'entity'), 100);
$dispatcher->addListener('hook_civicrm_post::Activity', ['\Civi\CCase\Events', 'fireCaseChange']);
$dispatcher->addListener('hook_civicrm_post::Case', ['\Civi\CCase\Events', 'fireCaseChange']);
$dispatcher->addListener('hook_civicrm_caseChange', ['\Civi\CCase\Events', 'delegateToXmlListeners']);
*/
class PostEvent extends GenericHookEvent {
- /**
- * This adapter automatically emits a narrower event.
- *
- * For example, `hook_civicrm_pre(Contact, ...)` will also dispatch `hook_civicrm_pre::Contact`.
- *
- * @param \Civi\Core\Event\PostEvent $event
- */
- public static function dispatchSubevent(PostEvent $event) {
- \Civi::dispatcher()->dispatch("hook_civicrm_post::" . $event->entity, $event);
- }
-
/**
* One of: 'create'|'edit'|'delete'
*
*/
class PreEvent extends GenericHookEvent {
- /**
- * This adapter automatically emits a narrower event.
- *
- * For example, `hook_civicrm_pre(Contact, ...)` will also dispatch `hook_civicrm_pre::Contact`.
- *
- * @param \Civi\Core\Event\PreEvent $event
- */
- public static function dispatchSubevent(PreEvent $event) {
- \Civi::dispatcher()->dispatch("hook_civicrm_pre::" . $event->entity, $event);
- }
-
/**
* One of: 'create'|'edit'|'delete'
*