dev/core#2850 add comment
[civicrm-core.git] / CRM / Core / EntityTokens.php
index 5226f5e9fe4a8073e9452d0a4af0c261dfaeb0ff..cc9350bb1374a9f9a1021c230b1c2e80d080ac68 100644 (file)
@@ -40,6 +40,10 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber {
   public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
     $this->prefetch = (array) $prefetch;
     $fieldValue = $this->getFieldValue($row, $field);
+    if (is_array($fieldValue)) {
+      // eg. role_id for participant would be an array here.
+      $fieldValue = implode(',', $fieldValue);
+    }
 
     if ($this->isPseudoField($field)) {
       if (!empty($fieldValue)) {
@@ -444,8 +448,38 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber {
     return CRM_Core_Config::singleton()->defaultCurrency;
   }
 
+  /**
+   * Get the fields required to prefetch the entity.
+   *
+   * @param \Civi\Token\Event\TokenValueEvent $e
+   *
+   * @return array
+   * @throws \API_Exception
+   */
   public function getPrefetchFields(TokenValueEvent $e): array {
-    return array_intersect(array_merge($this->getActiveTokens($e), $this->getCurrencyFieldName(), ['id']), array_keys($this->getAllTokens()));
+    $allTokens = array_keys($this->getAllTokens());
+    $requiredFields = array_intersect($this->getActiveTokens($e), $allTokens);
+    if (empty($requiredFields)) {
+      return [];
+    }
+    $requiredFields = array_merge($requiredFields, array_intersect($allTokens, array_merge(['id'], $this->getCurrencyFieldName())));
+    foreach ($this->getDependencies() as $field => $required) {
+      if (in_array($field, $this->getActiveTokens($e), TRUE)) {
+        foreach ((array) $required as $key) {
+          $requiredFields[] = $key;
+        }
+      }
+    }
+    return $requiredFields;
+  }
+
+  /**
+   * Get fields which need to be returned to render another token.
+   *
+   * @return array
+   */
+  public function getDependencies(): array {
+    return [];
   }
 
 }