From 37836cb1757e186d38bf48be0817351500f9b868 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 3 May 2022 11:55:45 -0400 Subject: [PATCH] APIv4 - Support pseudoconstant suffixes in getFields --- Civi/Api4/Generic/DAOGetFieldsAction.php | 27 +++++++++++++++++++ .../api/v4/Action/BasicCustomFieldTest.php | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Civi/Api4/Generic/DAOGetFieldsAction.php b/Civi/Api4/Generic/DAOGetFieldsAction.php index e11ac0c8f7c3..e877e6dd8b2b 100644 --- a/Civi/Api4/Generic/DAOGetFieldsAction.php +++ b/Civi/Api4/Generic/DAOGetFieldsAction.php @@ -12,6 +12,9 @@ namespace Civi\Api4\Generic; +use Civi\Api4\Utils\CoreUtil; +use Civi\Api4\Utils\FormattingUtil; + /** * @inheritDoc * @method bool getIncludeCustom() @@ -36,6 +39,7 @@ protected function getRecords() { // Any fields name with a dot in it is either custom or an implicit join $includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE; } + $this->formatValues(); $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $includeCustom, $this->values); $fields = $this->specToArray($spec->getFields($fieldsToGet)); foreach ($fieldsToGet ?? [] as $fieldName) { @@ -91,6 +95,29 @@ private function getFkFieldSpec($fieldName, $fields) { } } + /** + * Special handling for pseudoconstant replacements. + * + * Normally this would involve calling getFields... but this IS getFields. + * + * @throws \API_Exception + */ + private function formatValues() { + foreach (array_keys($this->values) as $key) { + if (strpos($key, ':')) { + [$fieldName, $suffix] = explode(':', $key); + $context = FormattingUtil::$pseudoConstantContexts[$suffix] ?? NULL; + if (!$context) { + throw new \API_Exception('Illegal expression'); + } + $baoName = CoreUtil::getBAOFromApiName($this->getEntityName()); + $options = $baoName::buildOptions($fieldName, $context); + $this->values[$fieldName] = FormattingUtil::replacePseudoconstant($options, $this->values[$key], TRUE); + unset($this->values[$key]); + } + } + } + public function fields() { $fields = parent::fields(); $fields[] = [ diff --git a/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php b/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php index c070b86fe743..9e4ab6ef4095 100644 --- a/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php +++ b/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php @@ -52,7 +52,7 @@ public function testWithSingleField(): void { $getFields = Contact::getFields(FALSE); $this->assertEquals('Custom', $getFields->execute()->indexBy('name')['MyIndividualFields.FavColor']['type']); $this->assertContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type' => 'Individual'])->execute()->column('name')); - $this->assertNotContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type' => 'Household'])->execute()->column('name')); + $this->assertNotContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type:name' => 'Household'])->execute()->column('name')); $contactId = Contact::create(FALSE) ->addValue('first_name', 'Johann')