Fix for new prefetch key
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 28 Aug 2021 02:17:57 +0000 (14:17 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 28 Aug 2021 06:30:36 +0000 (18:30 +1200)
Fixes a bug in
https://github.com/civicrm/civicrm-core/pull/21184
which was masked in the test by the lack of campaigns to load.

The ?? operator handles FALSE differently to the ?: operator
so this was casting FALSE to TRUE

CRM/Core/EntityTokens.php
Civi/Api4/Service/Spec/SpecFormatter.php
tests/phpunit/api/v4/Action/GetFieldsTest.php

index 438cb3d007b03a6bf60e95681c427757c2f1634f..6ded3ebec5ace00d59b497641a2305e6bdb465ef 100644 (file)
@@ -196,8 +196,9 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber {
     $return = [];
     foreach (array_keys($this->getBasicTokens()) as $fieldName) {
       if ($this->isAddPseudoTokens($fieldName)) {
-        $return[$fieldName . ':label'] = $this->fieldMetadata[$fieldName]['input_attrs']['label'];
-        $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $this->fieldMetadata[$fieldName]['input_attrs']['label'];
+        $fieldLabel = $this->fieldMetadata[$fieldName]['input_attrs']['label'] ?? $this->fieldMetadata[$fieldName]['label'];
+        $return[$fieldName . ':label'] = $fieldLabel;
+        $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $fieldLabel;
       }
     }
     return $return;
@@ -229,7 +230,7 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber {
       // v4 style custom tokens - but medium term this IF will probably go.
       return FALSE;
     }
-    return (bool) $this->getFieldMetadata()[$fieldName]['options'];
+    return (bool) ($this->getFieldMetadata()[$fieldName]['options'] || !empty($this->getFieldMetadata()[$fieldName]['suffixes']));
   }
 
   /**
index 27a1e3bdc1766edaaada6c163efdd4d866f92dae..413c1f3670b8f85b323432a9517f82db1739fee9 100644 (file)
@@ -61,7 +61,7 @@ class SpecFormatter {
       $field->setLabel($data['html']['label'] ?? NULL);
       if (!empty($data['pseudoconstant'])) {
         // Do not load options if 'prefetch' is explicitly FALSE
-        if ($data['pseudoconstant']['prefetch'] ?? TRUE) {
+        if (!isset($data['pseudoconstant']['prefetch']) || $data['pseudoconstant']['prefetch'] === FALSE) {
           $field->setOptionsCallback([__CLASS__, 'getOptions']);
         }
         // These suffixes are always supported if a field has options
index e57457b36350558e3386ab4c6fd74fb202f83d8d..d5833dd9e788e15b79bdce44e60c7b40d4449c58 100644 (file)
@@ -20,6 +20,7 @@
 namespace api\v4\Action;
 
 use api\v4\UnitTestCase;
+use Civi\Api4\Campaign;
 use Civi\Api4\Contact;
 use Civi\Api4\Contribution;
 
@@ -82,6 +83,7 @@ class GetFieldsTest extends UnitTestCase {
   public function testPreloadFalse() {
     \CRM_Core_BAO_ConfigSetting::enableComponent('CiviContribute');
     \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
+    Campaign::create()->setValues(['name' => 'Big Campaign', 'title' => 'Biggie'])->execute();
     // The campaign_id field has preload = false in the schema,
     // Which means the options will NOT load but suffixes are still available
     $fields = Contribution::getFields(FALSE)