if (empty($spec['pseudoconstant'])) {
continue;
}
+ $pseudoconstant = $spec['pseudoconstant'];
// It would be nice if we could leverage CRM_Core_PseudoConstant::get() somehow,
// but it's tightly coupled to DAO/field. However, if you really need to support
// more pseudoconstant types, then probably best to refactor it. For now, KISS.
- if (!empty($spec['pseudoconstant']['callback'])) {
- $spec['options'] = Resolver::singleton()->call($spec['pseudoconstant']['callback'], []);
+ if (!empty($pseudoconstant['callback'])) {
+ $spec['options'] = Resolver::singleton()->call($pseudoconstant['callback'], []);
}
- elseif (!empty($spec['pseudoconstant']['optionGroupName'])) {
- $keyColumn = \CRM_Utils_Array::value('keyColumn', $spec['pseudoconstant'], 'value');
- $spec['options'] = \CRM_Core_OptionGroup::values($spec['pseudoconstant']['optionGroupName'], FALSE, FALSE, TRUE, NULL, 'label', TRUE, FALSE, $keyColumn);
+ elseif (!empty($pseudoconstant['optionGroupName'])) {
+ $keyColumn = \CRM_Utils_Array::value('keyColumn', $pseudoconstant, 'value');
+ $spec['options'] = \CRM_Core_OptionGroup::values($pseudoconstant['optionGroupName'], FALSE, FALSE, TRUE, NULL, 'label', TRUE, FALSE, $keyColumn);
+ }
+ if (!empty($pseudoconstant['table'])) {
+ $params = [
+ 'condition' => $pseudoconstant['condition'] ?? [],
+ 'keyColumn' => $pseudoconstant['keyColumn'] ?? NULL,
+ 'labelColumn' => $pseudoconstant['labelColumn'] ?? NULL,
+ ];
+ $spec['options'] = \CRM_Core_PseudoConstant::renderOptionsFromTablePseudoconstant($pseudoconstant, $params, ($spec['localize_context'] ?? NULL), 'get');
}
}
}
'quick_form_type' => 'Select',
'default' => NULL,
'pseudoconstant' => [
- // @todo - handle table style pseudoconstants for settings & avoid deprecated function.
- 'callback' => 'CRM_Contribute_PseudoConstant::contributionPage',
+ 'table' => 'civicrm_contribution_page',
+ 'keyColumn' => 'id',
+ 'labelColumn' => 'title',
],
'html_type' => 'select',
'add' => '4.7',
*/
class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
+ /**
+ * Original value of civicrm_setting global.
+ * @var array
+ */
+ private $origSetting;
+
public function setUp() {
parent::setUp();
global $civicrm_setting;
$this->origSetting = $civicrm_setting;
- CRM_Utils_Cache::singleton()->flush();
+ CRM_Utils_Cache::singleton()->clear();
}
+ /**
+ * Clean up after test.
+ *
+ * @throws \CRM_Core_Exception
+ */
public function tearDown() {
global $civicrm_setting;
$civicrm_setting = $this->origSetting;
- CRM_Utils_Cache::singleton()->flush();
+ $this->quickCleanup(['civicrm_contribution']);
+ CRM_Utils_Cache::singleton()->clear();
parent::tearDown();
}
$this->assertEquals('Development', $environment);
}
+ /**
+ * Test that options defined as a pseudoconstant can be converted to options.
+ */
+ public function testPseudoConstants() {
+ $this->contributionPageCreate();
+ $metadata = \Civi\Core\SettingsMetadata::getMetadata(['name' => ['default_invoice_page']], NULL, TRUE);
+ $this->assertEquals('Test Contribution Page', $metadata['default_invoice_page']['options'][1]);
+ }
+
}