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

Allow setting metadata to use the table option. Update example setting default_invoice_page. #16903

Merged
merged 1 commit into from
Apr 9, 2020
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
19 changes: 14 additions & 5 deletions Civi/Core/SettingsMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,24 @@ protected static function loadOptions(&$settingSpec) {
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');
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions settings/Contribute.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@
'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',
Expand Down
25 changes: 23 additions & 2 deletions tests/phpunit/CRM/Core/BAO/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@
*/
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();
}

Expand Down Expand Up @@ -227,4 +239,13 @@ public function testSetCivicrmEnvironment() {
$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]);
}

}