Merge pull request #14103 from jitendrapurohit/core-889
[civicrm-core.git] / api / v3 / Setting.php
index 21a38ed52d6d77357af284786b4135f5d835fc89..a7ce04b76fcd4356bdd4268096e20674851b1897 100644 (file)
@@ -44,12 +44,14 @@ function civicrm_api3_setting_getfields($params) {
       'name' => [
         'title' => 'name of setting field',
         'api.required' => 1,
-        'type' => CRM_Utils_Type::T_STRING],
+        'type' => CRM_Utils_Type::T_STRING,
+      ],
       'group' => [
         'api.required' => 0,
         'title' => 'Setting Group',
         'description' => 'Settings Group. This is required if the setting is not stored in config',
-        'type' => CRM_Utils_Type::T_STRING],
+        'type' => CRM_Utils_Type::T_STRING,
+      ],
     ];
     return civicrm_api3_create_success($result, $params, 'Setting', 'getfields');
   }
@@ -121,6 +123,7 @@ function civicrm_api3_setting_getdefaults(&$params) {
   }
   return civicrm_api3_create_success($defaults, $params, 'Setting', 'getfields');
 }
+
 /**
  * Metadata for Setting create function.
  *
@@ -145,33 +148,14 @@ function _civicrm_api3_setting_getdefaults_spec(&$params) {
  * @throws \API_Exception
  */
 function civicrm_api3_setting_getoptions($params) {
-  $specs = CRM_Core_BAO_Setting::getSettingSpecification();
+  $domainId = CRM_Utils_Array::value('domain_id', $params);
+  $specs = \Civi\Core\SettingsMetadata::getMetadata(['name' => $params['field']], $domainId, TRUE);
 
-  if (empty($specs[$params['field']]) || empty($specs[$params['field']]['pseudoconstant'])) {
+  if (empty($specs[$params['field']]) || !is_array(CRM_Utils_Array::value('options', $specs[$params['field']]))) {
     throw new API_Exception("The field '" . $params['field'] . "' has no associated option list.");
   }
 
-  $pseudoconstant = $specs[$params['field']]['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($pseudoconstant['callback'])) {
-    $values = Civi\Core\Resolver::singleton()->call($pseudoconstant['callback'], []);
-    return civicrm_api3_create_success($values, $params, 'Setting', 'getoptions');
-  }
-  elseif (!empty($pseudoconstant['optionGroupName'])) {
-    $keyColumn = 'value';
-    if (!empty($pseudoconstant['keyColumn'])) {
-      $keyColumn = $pseudoconstant['keyColumn'];
-    }
-    return civicrm_api3_create_success(
-      CRM_Core_OptionGroup::values($pseudoconstant['optionGroupName'], FALSE, FALSE, TRUE, NULL, 'label', TRUE, FALSE, $keyColumn),
-      $params, 'Setting', 'getoptions'
-    );
-  }
-
-  throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list.");
+  return civicrm_api3_create_success($specs[$params['field']]['options'], $params, 'Setting', 'getoptions');
 }
 
 /**
@@ -320,6 +304,7 @@ function civicrm_api3_setting_get($params) {
   $result = CRM_Core_BAO_Setting::getItems($params, $domains, CRM_Utils_Array::value('return', $params, []));
   return civicrm_api3_create_success($result, $params, 'Setting', 'get');
 }
+
 /**
  * Metadata for setting create function.
  *
@@ -337,6 +322,7 @@ function _civicrm_api3_setting_get_spec(&$params) {
     'description' => 'if you know the group defining it will make the api more efficient',
   ];
 }
+
 /**
  * Returns value for specific parameter.
  *
@@ -417,7 +403,7 @@ function _civicrm_api3_setting_getDomainArray(&$params) {
   }
 
   if ($params['domain_id'] == 'current_domain') {
-    $params['domain_id']    = CRM_Core_Config::domainID();
+    $params['domain_id'] = CRM_Core_Config::domainID();
   }
 
   if ($params['domain_id'] == 'all') {