Only set defaults when creating the custom field
authorMichael McAndrew <michaelmcandrew@thirdsectordesign.org>
Thu, 31 May 2018 23:50:55 +0000 (00:50 +0100)
committerMichael McAndrew <michaelmcandrew@thirdsectordesign.org>
Thu, 31 May 2018 23:50:55 +0000 (00:50 +0100)
CRM/Core/BAO/CustomField.php
tests/phpunit/api/v3/CustomFieldTest.php

index ce816c53d1e44db8e74e9fa851551850ecf25120..03e0cb65717bc55e0a6aa052e27fce501db8bcc2 100644 (file)
@@ -274,13 +274,15 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
 
     $customField = new CRM_Core_DAO_CustomField();
     $customField->copyValues($params);
-    $customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE);
-    $customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE);
-    $customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE);
-    $customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE);
-    //CRM-15792 - Custom field gets disabled if is_active not set
-    $customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
-    $customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
+    if ($op == 'create') {
+      $customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE);
+      $customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE);
+      $customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE);
+      $customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE);
+      //CRM-15792 - Custom field gets disabled if is_active not set
+      $customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
+      $customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
+    }
     $customField->save();
 
     // make sure all values are present in the object for further processing
index 76a14abcb3aadd4ee2878b7394217d39e8f344e9..abb5c19f1eb2729495f6401259dcd1d79c7e91c4 100644 (file)
@@ -569,4 +569,31 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase {
     return $r;
   }
 
+  /**
+   * This test is designed to ensure that when a custom field is updated via the
+   * API, params that are not supplied do not revert to the defaults. This was
+   * happening with, for example, is_searchable
+   */
+  public function testDisableSearchableContactReferenceField() {
+    $customGroup = $this->customGroupCreate(array(
+      'name' => 'testCustomGroup',
+      'title' => 'testCustomGroup',
+      'extends' => 'Individual',
+    ));
+    $params = array(
+      'name' => 'testCustomField',
+      'label' => 'testCustomField',
+      'custom_group_id' => 'testCustomGroup',
+      'data_type' => 'ContactReference',
+      'html_type' => 'Autocomplete-Select',
+      'is_searchable' => '1',
+    );
+    $result = $this->callAPISuccess('CustomField', 'create', $params);
+    $params = [
+      'id' => $result['id'],
+      'is_active' => 0,
+    ];
+    $result = $this->callAPISuccess('CustomField', 'create', $params);
+  }
+
 }