dev/core#1816 - Legacy support for matching custom field options by label
authorColeman Watts <coleman@civicrm.org>
Tue, 16 Jun 2020 00:15:02 +0000 (20:15 -0400)
committerSeamus Lee <seamuslee001@gmail.com>
Tue, 16 Jun 2020 01:40:19 +0000 (11:40 +1000)
api/v3/utils.php
tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
tests/phpunit/api/v3/ContributionTest.php

index 40f73f2871683e8eddf4f252af1b8ae74a1663e8..ae9eaa0a18c0fe73e87f66ebcc80a7ee13080849 100644 (file)
@@ -2342,6 +2342,16 @@ function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldN
       // CiviMagic syntax for Nulling out the field - let it through.
       return;
     }
+    // Legacy support for custom fields: If matching failed by name, fallback to label
+    // @see https://lab.civicrm.org/dev/core/-/issues/1816
+    if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($fieldName)) {
+      $field = new CRM_Core_BAO_CustomField();
+      $field->id = $customFieldId;
+      $options = array_map("strtolower", $field->getOptions());
+      $newValue = array_search(strtolower($value), $options);
+    }
+  }
+  if ($newValue === FALSE) {
     throw new API_Exception("'$value' is not a valid option for field $fieldName", 2001, ['error_field' => $fieldName]);
   }
   $value = $newValue;
index 23f26fa62cd12e90fe28dd81148c978595f0d618..6f76bd1f23a329f07e89ac75f340f1feb8e3347e 100644 (file)
@@ -91,7 +91,7 @@ trait CRMTraits_Custom_CustomDataTrait {
    * @throws \Civi\API\Exception\UnauthorizedException
    */
   public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
-    $supported = ['text', 'select', 'date', 'int', 'contact_reference'];
+    $supported = ['text', 'select', 'date', 'int', 'contact_reference', 'radio'];
     if (!in_array($customFieldType, $supported, TRUE)) {
       throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
     }
@@ -120,6 +120,11 @@ trait CRMTraits_Custom_CustomDataTrait {
       case 'contact_reference':
         $reference = $this->createContactReferenceCustomField($fieldParams)['id'];
         return;
+
+      case 'radio':
+        $reference = $this->createIntegerRadioCustomField($fieldParams)['id'];
+        return;
+
     }
   }
 
@@ -329,6 +334,18 @@ trait CRMTraits_Custom_CustomDataTrait {
     return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
   }
 
+  /**
+   * Create a custom field of  type radio with integer values.
+   *
+   * @param array $params
+   *
+   * @return array
+   */
+  protected function createIntegerRadioCustomField($params): array {
+    $params = array_merge($this->getFieldsValuesByType('Int', 'Radio'), $params);
+    return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
+  }
+
   /**
    * Get default field values for the type of field.
    *
@@ -424,7 +441,7 @@ trait CRMTraits_Custom_CustomDataTrait {
         ],
         'CheckBox' => [
           'label' => 'Pick Color',
-          'html_type' => 'Checkbox',
+          'html_type' => 'CheckBox',
           'data_type' => 'String',
           'text_length' => '',
           'default_value' => '',
@@ -546,6 +563,12 @@ trait CRMTraits_Custom_CustomDataTrait {
               'weight' => 2,
               'is_active' => 1,
             ],
+            [
+              'label' => 'Red Testing',
+              'value' => 5,
+              'weight' => 3,
+              'is_active' => 1,
+            ],
           ],
         ],
       ],
index cceecd738e66fe4be63a7e1efacf89dc4a153c89..c227dff574aa232d3dbdbbe74af5a16ae664ce82 100644 (file)
@@ -4558,6 +4558,9 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     ]);
     $this->assertEquals(5, $getContribution['values'][$contribution['id']][$this->getCustomFieldName('int')]);
     $this->assertEquals('Some Text', $getContribution['values'][$contribution['id']]['custom_' . $this->ids['CustomField']['text']]);
+    $this->callAPISuccess('CustomField', 'delete', ['id' => $this->ids['CustomField']['text']]);
+    $this->callAPISuccess('CustomField', 'delete', ['id' => $this->ids['CustomField']['int']]);
+    $this->callAPISuccess('CustomGroup', 'delete', ['id' => $this->ids['CustomGroup']['Custom Group']]);
   }
 
   /**
@@ -4579,4 +4582,15 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test that passing in label for an option value linked to a custom field works
+   * @see dev/core#1816
+   */
+  public function testCustomValueOptionLabelTest() {
+    $this->createCustomGroupWithFieldOfType([], 'radio');
+    $params = $this->_params;
+    $params['custom_' . $this->ids['CustomField']['radio']] = 'Red Testing';
+    $contribution = $this->callAPISuccess('Contribution', 'Create', $params);
+  }
+
 }