CRM-16373 - Setting.getoptions - Read/execute callbacks from *.settings.php
authorTim Otten <totten@civicrm.org>
Mon, 17 Aug 2015 06:53:42 +0000 (23:53 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 17 Aug 2015 06:57:39 +0000 (23:57 -0700)
CRM/Core/PseudoConstant.php
api/v3/Setting.php

index 70ce51a8a80de42ea5c8a96f555b42c6778e7f8c..e77ce3097a9b3186d285e9f8b0b897816744a7cc 100644 (file)
@@ -192,6 +192,11 @@ class CRM_Core_PseudoConstant {
    * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
    * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Pseudoconstant+%28option+list%29+Reference
    *
+   * NOTE: If someone undertakes a refactoring of this, please consider the use-case of
+   * the Setting.getoptions API. There is no DAO/field, but it would be nice to use the
+   * same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling
+   * between $field lookup and the $pseudoconstant evaluation.
+   *
    * @param string $daoName
    * @param string $fieldName
    * @param array $params
index d2ff10ad1859e7d1afb1ab4a77a7561df1e6b3b1..f2b4f4b64ca628ad16434f1a7bca4afdf883c4a1 100644 (file)
@@ -144,6 +144,26 @@ function _civicrm_api3_setting_getdefaults_spec(&$params) {
   );
 }
 
+function civicrm_api3_setting_getoptions($params) {
+  $specs = CRM_Core_BAO_Setting::getSettingSpecification();
+
+  if (empty($specs[$params['field']]) || empty($specs[$params['field']]['pseudoconstant'])) {
+    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'], array());
+    return civicrm_api3_create_success($values, $params, 'Setting', 'getoptions');
+  }
+
+  throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list.");
+}
+
 /**
  * Revert settings to defaults.
  *