// create any option group & values if required
if ($params['html_type'] != 'Text' &&
in_array($params['data_type'], array(
- 'String', 'Int', 'Float', 'Money')) &&
- !empty($params['option_value']) && is_array($params['option_value'])
+ 'String', 'Int', 'Float', 'Money'))
) {
$tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
$optionGroup->is_active = 1;
$optionGroup->save();
$params['option_group_id'] = $optionGroup->id;
+ if(!empty($params['option_value']) && is_array($params['option_value'])){
+ foreach ($params['option_value'] as $k => $v) {
+ if (strlen(trim($v))) {
+ $optionValue = new CRM_Core_DAO_OptionValue();
+ $optionValue->option_group_id = $optionGroup->id;
+ $optionValue->label = $params['option_label'][$k];
+ $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]);
+ switch ($params['data_type']) {
+ case 'Money':
+ $optionValue->value = CRM_Utils_Rule::cleanMoney($v);
+ break;
+
+ case 'Int':
+ $optionValue->value = intval($v);
+ break;
+
+ case 'Float':
+ $optionValue->value = floatval($v);
+ break;
+
+ default:
+ $optionValue->value = trim($v);
+ }
-
-
- foreach ($params['option_value'] as $k => $v) {
- if (strlen(trim($v))) {
- $optionValue = new CRM_Core_DAO_OptionValue();
- $optionValue->option_group_id = $optionGroup->id;
- $optionValue->label = $params['option_label'][$k];
- $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]);
- switch ($params['data_type']) {
- case 'Money':
- $optionValue->value = CRM_Utils_Rule::cleanMoney($v);
- break;
-
- case 'Int':
- $optionValue->value = intval($v);
- break;
-
- case 'Float':
- $optionValue->value = floatval($v);
- break;
-
- default:
- $optionValue->value = trim($v);
+ $optionValue->weight = $params['option_weight'][$k];
+ $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], FALSE);
+ $optionValue->save();
}
-
- $optionValue->weight = $params['option_weight'][$k];
- $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], FALSE);
- $optionValue->save();
}
}
}
*/
function civicrm_api3_custom_field_create($params) {
- if (!(CRM_Utils_Array::value('option_type', $params))) {
- if (CRM_Utils_Array::value('id', $params)) {
- $params['option_type'] = 2;
- }
- else {
- $params['option_type'] = 1;
- }
- }
-
-
// Array created for passing options in params
if (isset($params['option_values']) && is_array($params['option_values'])) {
foreach ($params['option_values'] as $key => $value) {
function _civicrm_api3_custom_field_create_spec(&$params) {
$params['label']['api.required'] = 1;
$params['custom_group_id']['api.required'] = 1;
+ $params['option_type'] = array(
+ 'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate',
+ 'api.default' => 1,
+ 'type' => CRM_Utils_Type::T_BOOL,
+ );
}
/**
/**
* check with data type - Options with option_values
*/
+ function testCustomFieldCreateWithEmptyOptionGroup() {
+ $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
+ $params = array(
+ 'custom_group_id' => $customGroup['id'],
+ 'label' => 'Country',
+ 'html_type' => 'Select',
+ 'data_type' => 'String',
+ 'weight' => 4,
+ 'is_required' => 1,
+ 'is_searchable' => 0,
+ 'is_active' => 1,
+ 'version' => $this->_apiversion,
+ );
+
+ $customField = civicrm_api('custom_field', 'create', $params);
+ $this->assertAPISuccess($customField);
+ $this->assertNotNull($customField['id']);
+ $optionGroupID = civicrm_api('custom_field', 'getvalue', array(
+ 'version' => 3,
+ 'id' => $customField['id'],
+ 'return' => 'option_group_id',
+ ));
+
+ $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
+ $optionGroup = civicrm_api('option_group', 'getsingle', array(
+ 'version' => 3, 'id' => $optionGroupID));
+ $this->assertEquals($optionGroup['title'],'Country');
+ $optionValueCount = civicrm_api('option_value', 'getcount', array(
+ 'version' => 3, 'option_group_id' => $optionGroupID));
+ $this->assertEquals(0, $optionValueCount);
+ }
+
+
function testCustomFieldCreateWithOptionValues() {
$customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
$customField = civicrm_api('custom_field', 'create', $params);
- $this->assertEquals($customField['is_error'], 0);
+ $this->assertAPISuccess($customField);
$this->assertNotNull($customField['id']);
$getFieldsParams = array(
'options' => array('get_options' => 'custom_' . $customField['id']),
);
$customField = civicrm_api('custom_field', 'delete', $customOptionValueFields);
- $this->assertEquals($customField['is_error'], 0);
+ $this->assertAPISuccess($customField);
}
}