From f53bad04b93e065fb0e7b1f15776fb65904af986 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 30 Dec 2019 23:35:43 -0500 Subject: [PATCH] Pass api4 getFields values through to getOptions --- Civi/Api4/Generic/DAOGetFieldsAction.php | 2 +- Civi/Api4/Service/Spec/FieldSpec.php | 5 +++-- Civi/Api4/Service/Spec/SpecFormatter.php | 5 +++-- tests/phpunit/api/v4/Action/GetExtraFieldsTest.php | 14 ++++++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Civi/Api4/Generic/DAOGetFieldsAction.php b/Civi/Api4/Generic/DAOGetFieldsAction.php index edf7684c65e5..d64155fd0b67 100644 --- a/Civi/Api4/Generic/DAOGetFieldsAction.php +++ b/Civi/Api4/Generic/DAOGetFieldsAction.php @@ -52,7 +52,7 @@ protected function getRecords() { $this->includeCustom = strpos(implode('', $fields), '.') !== FALSE; } $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $this->includeCustom, $this->values); - return SpecFormatter::specToArray($spec->getFields($fields), $this->loadOptions); + return SpecFormatter::specToArray($spec->getFields($fields), $this->loadOptions, $this->values); } public function fields() { diff --git a/Civi/Api4/Service/Spec/FieldSpec.php b/Civi/Api4/Service/Spec/FieldSpec.php index 84f29f1d6390..90a684403608 100644 --- a/Civi/Api4/Service/Spec/FieldSpec.php +++ b/Civi/Api4/Service/Spec/FieldSpec.php @@ -376,9 +376,10 @@ private function getValidDataTypes() { } /** + * @param array $values * @return array */ - public function getOptions() { + public function getOptions($values = []) { if (!isset($this->options) || $this->options === TRUE) { $fieldName = $this->getName(); @@ -388,7 +389,7 @@ public function getOptions() { } $bao = CoreUtil::getBAOFromApiName($this->getEntity()); - $options = $bao::buildOptions($fieldName); + $options = $bao::buildOptions($fieldName, NULL, $values); if (!is_array($options) || !$options) { $options = FALSE; diff --git a/Civi/Api4/Service/Spec/SpecFormatter.php b/Civi/Api4/Service/Spec/SpecFormatter.php index 379caf8a03c2..8c63e66228ca 100644 --- a/Civi/Api4/Service/Spec/SpecFormatter.php +++ b/Civi/Api4/Service/Spec/SpecFormatter.php @@ -29,15 +29,16 @@ class SpecFormatter { /** * @param FieldSpec[] $fields * @param bool $includeFieldOptions + * @param array $values * * @return array */ - public static function specToArray($fields, $includeFieldOptions = FALSE) { + public static function specToArray($fields, $includeFieldOptions = FALSE, $values = []) { $fieldArray = []; foreach ($fields as $field) { if ($includeFieldOptions) { - $field->getOptions(); + $field->getOptions($values); } $fieldArray[$field->getName()] = $field->toArray(); } diff --git a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php index adab653b86a3..eb8242710d67 100644 --- a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php +++ b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php @@ -22,6 +22,7 @@ namespace api\v4\Action; use api\v4\UnitTestCase; +use Civi\Api4\Address; use Civi\Api4\Contact; /** @@ -42,4 +43,17 @@ public function testBAOFieldsWillBeReturned() { $this->assertEmpty($notReturned); } + public function testGetOptionsAddress() { + $getFields = Address::getFields()->setCheckPermissions(FALSE)->addWhere('name', '=', 'state_province_id')->setLoadOptions(TRUE); + + $usOptions = $getFields->setValues(['country_id' => 1228])->execute()->first(); + + $this->assertContains('Alabama', $usOptions['options']); + $this->assertNotContains('Alberta', $usOptions['options']); + + $caOptions = $getFields->setValues(['country_id' => 1039])->execute()->first(); + + $this->assertNotContains('Alabama', $caOptions['options']); + $this->assertContains('Alberta', $caOptions['options']); + } }