Skip to content

Commit

Permalink
Fix for new prefetch key
Browse files Browse the repository at this point in the history
Fixes a bug in
civicrm#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
  • Loading branch information
eileenmcnaughton committed Aug 28, 2021
1 parent 743c452 commit 3d9604f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions CRM/Core/EntityTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ public function getPseudoTokens(): array {
$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;
Expand Down Expand Up @@ -229,7 +230,7 @@ public function isAddPseudoTokens($fieldName): bool {
// 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']));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/Service/Spec/SpecFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function arrayToField(array $data, $entity) {
$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
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/api/v4/Action/GetFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace api\v4\Action;

use api\v4\UnitTestCase;
use Civi\Api4\Campaign;
use Civi\Api4\Contact;
use Civi\Api4\Contribution;

Expand Down Expand Up @@ -82,6 +83,7 @@ public function testInternalPropsAreHidden() {
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)
Expand Down

0 comments on commit 3d9604f

Please sign in to comment.