Fix booleans to use :label format
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 10 Sep 2021 03:59:42 +0000 (15:59 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 10 Sep 2021 04:02:18 +0000 (16:02 +1200)
CRM/Core/EntityTokens.php
tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

index c3882b4bbf5959e87730d848d62cc84c1c2aa351..8841dcdb6e12a21e2630f3c9b1175539529e7f9c 100644 (file)
@@ -124,7 +124,30 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber {
    * @return array|string[]
    */
   public function getAllTokens(): array {
-    return array_merge($this->getBasicTokens(), $this->getPseudoTokens(), CRM_Utils_Token::getCustomFieldTokens($this->getApiEntityName()));
+    $basicTokens = $this->getBasicTokens();
+    foreach (array_keys($basicTokens) as $fieldName) {
+      // The goal is to be able to render more complete tokens
+      // (eg. actual booleans, field names, raw ids) for a more
+      // advanced audiences - ie those using conditionals
+      // and to specify that audience in the api that retrieves.
+      // But, for now, let's not advertise, given that most of these fields
+      // aren't really needed even once...
+      if ($this->isBooleanField($fieldName)) {
+        unset($basicTokens[$fieldName]);
+      }
+    }
+    return array_merge($basicTokens, $this->getPseudoTokens(), CRM_Utils_Token::getCustomFieldTokens($this->getApiEntityName()));
+  }
+
+  /**
+   * Is the given field a boolean field.
+   *
+   * @param string $fieldName
+   *
+   * @return bool
+   */
+  public function isBooleanField(string $fieldName): bool {
+    return $this->getFieldMetadata()[$fieldName]['data_type'] === 'Boolean';
   }
 
   /**
@@ -204,6 +227,9 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber {
         $return[$fieldName . ':label'] = $fieldLabel;
         $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $fieldLabel;
       }
+      if ($this->isBooleanField($fieldName)) {
+        $return[$fieldName . ':label'] = $this->getFieldMetadata()[$fieldName]['title'];
+      }
     }
     return $return;
   }
index 14da2ef4251fad398d8cdbf658c0c33df6c4739a..2140dd8ed46d23a64789a835a6d32272dc3468ed 100644 (file)
@@ -397,7 +397,9 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr
     $fields = (array) Contribution::getFields()->addSelect('name', 'title')->execute()->indexBy('name');
     $allFields = [];
     foreach ($fields as $field) {
-      $allFields[$field['name']] = $field['title'];
+      if (!in_array($field['name'], ['is_test', 'is_pay_later', 'is_template'], TRUE)) {
+        $allFields[$field['name']] = $field['title'];
+      }
     }
     // contact ID is skipped.
     unset($allFields['contact_id']);
index f0826af266f5ac1d8913d3af50d40abf6bf84a1c..662bbe9323f8f038ce57fad53af4022ae370345e 100644 (file)
@@ -221,16 +221,16 @@ No
       'trxn_id' => 'Transaction ID',
       'invoice_id' => 'Invoice ID',
       'contribution_status_id' => 'Status',
-      'is_test' => 'Test',
+      'is_test:label' => 'Test',
       'cycle_day' => 'Cycle Day',
       'next_sched_contribution_date' => 'Next Scheduled Contribution Date',
       'failure_count' => 'Number of Failures',
       'failure_retry_date' => 'Retry Failed Attempt Date',
-      'auto_renew' => 'Auto Renew',
+      'auto_renew:label' => 'Auto Renew',
       'payment_processor_id' => 'Payment Processor ID',
       'financial_type_id' => 'Financial Type ID',
       'payment_instrument_id' => 'Payment Method',
-      'is_email_receipt' => 'Send email Receipt?',
+      'is_email_receipt:label' => 'Send email Receipt?',
       'frequency_unit:label' => 'Frequency Unit',
       'frequency_unit:name' => 'Machine name: Frequency Unit',
       'contribution_status_id:label' => 'Status',
@@ -309,22 +309,22 @@ abc
 123
 inv123
 2
-1
+Yes
 15
 
 0
 January 3rd, 2020 12:00 AM
-1
+Yes
 1
 2
 4
-1
+Yes
 year
 year
 Pending Label**
 Pending
-Dummy
-Dummy
+Dummy (test)
+Dummy (test)
 Member Dues
 Member Dues
 Check