Skip to content

Commit

Permalink
Pass api4 getFields values through to getOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Dec 31, 2019
1 parent 8830e2e commit f53bad0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Civi/Api4/Generic/DAOGetFieldsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions Civi/Api4/Service/Spec/FieldSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions Civi/Api4/Service/Spec/SpecFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/api/v4/Action/GetExtraFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace api\v4\Action;

use api\v4\UnitTestCase;
use Civi\Api4\Address;
use Civi\Api4\Contact;

/**
Expand All @@ -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']);
}
}

0 comments on commit f53bad0

Please sign in to comment.