Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 - Support pseudoconstant suffixes in getFields #23348

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Civi/Api4/Generic/DAOGetFieldsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace Civi\Api4\Generic;

use Civi\Api4\Utils\CoreUtil;
use Civi\Api4\Utils\FormattingUtil;

/**
* @inheritDoc
* @method bool getIncludeCustom()
Expand All @@ -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) {
Expand Down Expand Up @@ -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[] = [
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v4/Action/BasicCustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down