public function createCustomGroup($params = []) {
$params = array_merge([
'title' => 'Custom Group',
- 'extends' => [$this->entity],
+ 'extends' => [$this->entity ?? 'Contact'],
'weight' => 5,
'style' => 'Inline',
'max_multiple' => 0,
], $params);
- $this->ids['CustomGroup'][$params['title']] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
- return $this->ids['CustomGroup'][$params['title']];
+ $identifier = $params['name'] ?? $params['title'];
+ $this->ids['CustomGroup'][$identifier] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
+ return $this->ids['CustomGroup'][$identifier];
+ }
+
+ /**
+ * Get the table_name for the specified custom group.
+ *
+ * @param string $identifier
+ *
+ * @return string
+ */
+ public function getCustomGroupTable($identifier = 'Custom Group') {
+ return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids['CustomGroup'][$identifier], 'return' => 'table_name']);
+ }
+
+ /**
+ * Get the the column name for the identified custom field.
+ *
+ * @param string $key
+ * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
+ *
+ * @return string
+ */
+ protected function getCustomFieldColumnName($key) {
+ return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
}
/**
*
* @throws \CRM_Core_Exception
*/
- public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = '') {
- if ($customFieldType !== 'text') {
+ public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
+ $supported = ['text', 'select', 'date', 'int'];
+ 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');
}
$groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
+ $groupParams['name'] = $identifier ?? 'Custom Group';
$this->createCustomGroup($groupParams);
- $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+ switch ($customFieldType) {
+ case 'text':
+ $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
+ break;
+
+ case 'select':
+ $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
+ break;
+
+ case 'int':
+ $customField = $this->createIntCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
+ break;
+
+ case 'date':
+ $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
+ break;
+ }
$this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
}
$customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
$ids['text'] = $customField['id'];
- $optionValue[] = [
- 'label' => 'Red',
- 'value' => 'R',
- 'weight' => 1,
- 'is_active' => 1,
- ];
- $optionValue[] = [
- 'label' => 'Yellow',
- 'value' => 'Y',
- 'weight' => 2,
- 'is_active' => 1,
- ];
- $optionValue[] = [
- 'label' => 'Green',
- 'value' => 'G',
- 'weight' => 3,
- 'is_active' => 1,
- ];
-
- $params = [
- 'label' => 'Pick Color',
- 'html_type' => 'Select',
- 'data_type' => 'String',
- 'weight' => 2,
- 'is_required' => 1,
- 'is_searchable' => 0,
- 'is_active' => 1,
- 'option_values' => $optionValue,
- 'custom_group_id' => $customGroupID,
- ];
-
- $customField = $this->callAPISuccess('custom_field', 'create', $params);
+ $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
$ids['select_string'] = $customField['id'];
- $params = [
- 'custom_group_id' => $customGroupID,
- 'name' => 'test_date',
- 'label' => 'test_date',
- 'html_type' => 'Select Date',
- 'data_type' => 'Date',
- 'default_value' => '20090711',
- 'weight' => 3,
- 'time_format' => 1,
- ];
+ $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]);
+ $ids['select_date'] = $customField['id'];
- $customField = $this->callAPISuccess('custom_field', 'create', $params);
+ $customField = $this->createIntCustomField(['custom_group_id' => $customGroupID]);
+ $ids['int'] = $customField['id'];
- $ids['select_date'] = $customField['id'];
$params = [
'custom_group_id' => $customGroupID,
'name' => 'test_link',
$ids['file'] = $fileField['id'];
$ids['country'] = $this->customFieldCreate([
'custom_group_id' => $customGroupID,
- 'data_type' => 'Int',
+ 'data_type' => 'Country',
'html_type' => 'Select Country',
'default_value' => '',
'label' => 'Country',
* @return string
*/
protected function getCustomFieldName($key) {
- $linkField = 'custom_' . $this->getCustomFieldID($key);
- return $linkField;
+ return 'custom_' . $this->getCustomFieldID($key);
}
/**
* @return string
*/
protected function getCustomFieldID($key) {
- $linkField = $this->ids['CustomField'][$key];
- return $linkField;
+ return $this->ids['CustomField'][$key];
+ }
+
+ /**
+ * Create a custom text fields.
+ *
+ * @param array $params
+ * Parameter overrides, must include custom_group_id.
+ *
+ * @return array
+ */
+ protected function createIntCustomField($params = []) {
+ $params = array_merge([
+ 'label' => 'Enter integer here',
+ 'html_type' => 'Text',
+ 'data_type' => 'Int',
+ 'default_value' => '4',
+ 'weight' => 1,
+ 'is_required' => 1,
+ 'sequential' => 1,
+ 'is_searchable' => 1,
+ 'is_search_range' => 1,
+ ], $params);
+
+ return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
}
/**
'is_required' => 1,
'sequential' => 1,
'is_searchable' => 1,
+ 'text_length' => 300,
], $params);
return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
}
+ /**
+ * Create custom select field.
+ *
+ * @param array $params
+ * Parameter overrides, must include custom_group_id.
+ *
+ * @return array
+ */
+ protected function createSelectCustomField(array $params): array {
+ $optionValue = [
+ [
+ 'label' => 'Red',
+ 'value' => 'R',
+ 'weight' => 1,
+ 'is_active' => 1,
+ ],
+ [
+ 'label' => 'Yellow',
+ 'value' => 'Y',
+ 'weight' => 2,
+ 'is_active' => 1,
+ ],
+ [
+ 'label' => 'Green',
+ 'value' => 'G',
+ 'weight' => 3,
+ 'is_active' => 1,
+ ],
+ ];
+
+ $params = array_merge([
+ 'label' => 'Pick Color',
+ 'html_type' => 'Select',
+ 'data_type' => 'String',
+ 'weight' => 2,
+ 'is_required' => 1,
+ 'is_searchable' => 0,
+ 'is_active' => 1,
+ 'option_values' => $optionValue,
+ ], $params);
+
+ $customField = $this->callAPISuccess('custom_field', 'create', $params);
+ return $customField['values'][$customField['id']];
+ }
+
+ /**
+ * Create a custom field of type date.
+ *
+ * @param array $params
+ *
+ * @return array
+ */
+ protected function createDateCustomField($params): array {
+ $params = array_merge([
+ 'name' => 'test_date',
+ 'label' => 'test_date',
+ 'html_type' => 'Select Date',
+ 'data_type' => 'Date',
+ 'default_value' => '20090711',
+ 'weight' => 3,
+ 'is_searchable' => 1,
+ 'is_search_range' => 1,
+ 'time_format' => 1,
+ ], $params);
+
+ $customField = $this->callAPISuccess('custom_field', 'create', $params);
+ return $customField['values'][$customField['id']];
+ }
+
}