From: Tim Otten Date: Tue, 18 May 2021 08:56:37 +0000 (-0700) Subject: (REF) Consolidate the 'dispatchSubevent()' methods X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=cc69f300c2ede788b96737cb44eab1f8fce9de85;p=civicrm-core.git (REF) Consolidate the 'dispatchSubevent()' methods --- diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index e6d1cff3b3..2785d04da8 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -371,13 +371,20 @@ class Container { /** @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']); diff --git a/Civi/Core/Event/PostEvent.php b/Civi/Core/Event/PostEvent.php index 100be7b754..f3885af444 100644 --- a/Civi/Core/Event/PostEvent.php +++ b/Civi/Core/Event/PostEvent.php @@ -17,17 +17,6 @@ namespace Civi\Core\Event; */ 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' * diff --git a/Civi/Core/Event/PreEvent.php b/Civi/Core/Event/PreEvent.php index dc08f1d0c9..db25c344e0 100644 --- a/Civi/Core/Event/PreEvent.php +++ b/Civi/Core/Event/PreEvent.php @@ -17,17 +17,6 @@ namespace Civi\Core\Event; */ 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' *