diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 494201eaa68f..17d614da877e 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -162,8 +162,7 @@ class CRM_Core_PseudoConstant { * @param string $fieldName * @param array $params * - name string name of the option group - * - flip boolean results are return in id => label format if false - * if true, the results are reversed + * - flip DEPRECATED * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value) * - localize boolean if true, localize the results before returning * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND' @@ -228,6 +227,11 @@ public static function get($daoName, $fieldName, $params = [], $context = NULL) $fieldOptions = call_user_func(Civi\Core\Resolver::singleton()->get($pseudoconstant['callback']), $context, $params); //CRM-18223: Allow additions to field options via hook. CRM_Utils_Hook::fieldOptions($entity, $fieldName, $fieldOptions, $params); + if ($context === 'validate') { + // This mode requires machine names and key/value pairs don't have a name, so + // use key for name. (labels are translatable so don't make suitable machine names) + return array_combine(array_keys($fieldOptions), array_keys($fieldOptions)); + } return $fieldOptions; } @@ -255,10 +259,10 @@ public static function get($daoName, $fieldName, $params = [], $context = NULL) $params['grouping'], $params['localize'], $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL, - $params['labelColumn'] ? $params['labelColumn'] : 'label', + $params['labelColumn'] ?: 'label', $params['onlyActive'], $params['fresh'], - $params['keyColumn'] ? $params['keyColumn'] : 'value', + $params['keyColumn'] ?: 'value', !empty($params['orderColumn']) ? $params['orderColumn'] : 'weight' ); CRM_Utils_Hook::fieldOptions($entity, $fieldName, $options, $params); diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index 483cf7353e7d..afaee7fa0794 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -177,7 +177,7 @@ private function formatOptionList($options) { if (!is_array($option)) { $option = [ 'id' => $id, - 'name' => $option, + 'name' => $id, 'label' => $option, ]; } diff --git a/tests/phpunit/api/v4/Action/BasicActionsTest.php b/tests/phpunit/api/v4/Action/BasicActionsTest.php index d67a61598c44..35e599967a75 100644 --- a/tests/phpunit/api/v4/Action/BasicActionsTest.php +++ b/tests/phpunit/api/v4/Action/BasicActionsTest.php @@ -212,7 +212,8 @@ public function testGetFields() { // Simple options should be expanded to non-assoc array $this->assertCount(2, $getFields); $this->assertEquals('one', $getFields['group']['options'][0]['id']); - $this->assertEquals('First', $getFields['group']['options'][0]['name']); + // Name is interchangeable with id + $this->assertEquals('one', $getFields['group']['options'][0]['name']); $this->assertEquals('First', $getFields['group']['options'][0]['label']); $this->assertFalse(isset($getFields['group']['options'][0]['color'])); // Complex options should give all requested properties diff --git a/tests/phpunit/api/v4/Entity/MembershipTest.php b/tests/phpunit/api/v4/Entity/MembershipTest.php index 5f553e390463..5355d55ad53a 100644 --- a/tests/phpunit/api/v4/Entity/MembershipTest.php +++ b/tests/phpunit/api/v4/Entity/MembershipTest.php @@ -73,4 +73,16 @@ public function testUpdateWeights() { } + /** + * Test getting options + */ + public function testGetOptions(): void { + $fields = MembershipType::getFields(FALSE) + ->setLoadOptions(['name', 'id', 'label']) + ->execute()->indexBy('name'); + $this->assertEquals('rolling', $fields['period_type']['options'][0]['name']); + $this->assertEquals('rolling', $fields['period_type']['options'][0]['id']); + $this->assertEquals('Rolling', $fields['period_type']['options'][0]['label']); + } + }