Remove unused getting & setting (copy & paste)
[civicrm-core.git] / CRM / Event / ParticipantTokens.php
index e132d8faa16fdd4380ba62887d684605ca1835a0..b29050fcf143fd24f98f5fdfba01af4557788751 100644 (file)
@@ -19,13 +19,6 @@ use Civi\Token\TokenRow;
  */
 class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens {
 
-  public static function getSubscribedEvents() {
-    $events = parent::getSubscribedEvents();
-    // Set the weight so it runs before event tokens.
-    $events['civi.token.eval'] = ['evaluateTokens', 400];
-    return $events;
-  }
-
   /**
    * Get the entity name for api v4 calls.
    *
@@ -45,8 +38,25 @@ class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens {
   /**
    * Get any tokens with custom calculation.
    */
-  public function getBespokeTokens(): array {
-    return ['balance' => ts('Event Balance')];
+  protected function getBespokeTokens(): array {
+    return [
+      'balance' => [
+        'title' => ts('Event Balance'),
+        'name' => 'balance',
+        'type' => 'calculated',
+        'options' => NULL,
+        'data_type' => 'Money',
+        'audience' => 'user',
+      ],
+    ];
+  }
+
+  public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void {
+    // When targeting `civicrm_participant` records, we enable both `{participant.*}` (per usual) and the related `{event.*}`.
+    parent::alterActionScheduleQuery($e);
+    if ($e->mapping->getEntity() === $this->getExtendableTableName()) {
+      $e->query->select('e.event_id AS tokenContext_eventId');
+    }
   }
 
   /**
@@ -55,9 +65,6 @@ class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens {
    */
   public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
     $this->prefetch = (array) $prefetch;
-    if (empty($row->context['eventId'])) {
-      $row->context['eventId'] = $this->getFieldValue($row, 'event_id');
-    }
     if ($field === 'balance') {
       // @todo - is this really a good idea to call this & potentially get the
       // balance of the contribution attached to 'registered_by_id'
@@ -86,7 +93,7 @@ class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens {
    *
    * @return string[]
    */
-  public function getSkippedFields(): array {
+  protected function getSkippedFields(): array {
     $fields = parent::getSkippedFields();
     // Never add these 2 fields - may not be a stable part of the schema.
     // This field is on it's way out of core.