Merge pull request #17358 from seamuslee001/d8_prevnext_cache_test_fix
[civicrm-core.git] / CRM / Core / BAO / OptionValue.php
index 4da2ece077499f1c44ca73fb275456c10f7f1c77..8b8f6693895f15c8fdd3cf74872eefd655a786d2 100644 (file)
@@ -31,17 +31,14 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
    * @param array $params
    *   Input parameters.
    *
-   * @return object
+   * @return CRM_Core_DAO_OptionValue
+   * @throws \CRM_Core_Exception
    */
   public static function create($params) {
     if (empty($params['id'])) {
       self::setDefaults($params);
     }
-    $ids = [];
-    if (!empty($params['id'])) {
-      $ids = ['optionValue' => $params['id']];
-    }
-    return CRM_Core_BAO_OptionValue::add($params, $ids);
+    return CRM_Core_BAO_OptionValue::add($params);
   }
 
   /**
@@ -58,18 +55,10 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
    * @param array $params
    */
   public static function setDefaults(&$params) {
-    if (CRM_Utils_Array::value('label', $params, NULL) === NULL) {
-      $params['label'] = $params['name'];
-    }
-    if (CRM_Utils_Array::value('name', $params, NULL) === NULL) {
-      $params['name'] = $params['label'];
-    }
-    if (CRM_Utils_Array::value('weight', $params, NULL) === NULL) {
-      $params['weight'] = self::getDefaultWeight($params);
-    }
-    if (CRM_Utils_Array::value('value', $params, NULL) === NULL) {
-      $params['value'] = self::getDefaultValue($params);
-    }
+    $params['label'] = $params['label'] ?? $params['name'];
+    $params['name'] = $params['name'] ?? CRM_Utils_String::titleToVar($params['label']);
+    $params['weight'] = $params['weight'] ?? self::getDefaultWeight($params);
+    $params['value'] = $params['value'] ?? self::getDefaultValue($params);
   }
 
   /**
@@ -151,23 +140,18 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
    *   deprecated Reference array contains the id.
    *
    * @return \CRM_Core_DAO_OptionValue
+   *
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public static function add(&$params, $ids = []) {
-    $id = $params['id'] ?? $ids['optionValue'] ?? NULL;
-    // CRM-10921: do not reset attributes to default if this is an update
-    //@todo consider if defaults are being set in the right place. 'dumb' defaults like
-    // these would be usefully set @ the api layer so they are visible to api users
-    // complex defaults like the domain id below would make sense in the setDefauls function
-    // but unclear what other ways this function is being used
-    if (!$id) {
-      $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
-      $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
-      $params['is_optgroup'] = CRM_Utils_Array::value('is_optgroup', $params, FALSE);
-      $params['filter'] = CRM_Utils_Array::value('filter', $params, FALSE);
+    if (!empty($ids['optionValue']) && empty($params['id'])) {
+      CRM_Core_Error::deprecatedFunctionWarning('$params[\'id\'] should be set, $ids is deprecated');
     }
+    $id = $params['id'] ?? $ids['optionValue'] ?? NULL;
+
     // Update custom field data to reflect the new value
-    elseif (isset($params['value'])) {
+    if ($id && isset($params['value'])) {
       CRM_Core_BAO_CustomOption::updateValue($id, $params['value']);
     }