(REF) Consolidate the 'dispatchSubevent()' methods
authorTim Otten <totten@civicrm.org>
Tue, 18 May 2021 08:56:37 +0000 (01:56 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 7 Jun 2021 03:18:52 +0000 (20:18 -0700)
Civi/Core/Container.php
Civi/Core/Event/PostEvent.php
Civi/Core/Event/PreEvent.php

index e6d1cff3b32285fe9c3ed510e1c9a93bfc8359f7..2785d04da87031de377c5b053d813bc3656ab677 100644 (file)
@@ -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']);
index 100be7b7547a33cb297d2fb5378bcc70e2dbf325..f3885af444ca36255806099d6d89b56b2fed53dd 100644 (file)
@@ -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'
    *
index dc08f1d0c98c63eef4c99e47e36b36bdac3d4462..db25c344e09538779d9e8ca5e666665d0d7943d8 100644 (file)
@@ -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'
    *