diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 44c4e958c0f8..9f18bfde45b2 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -45,36 +45,13 @@ public static function create(&$params) { $group->title = $params['title']; } - $extends = CRM_Utils_Array::value('extends', $params, []); - $extendsEntity = $extends[0] ?? NULL; - - $participantEntities = [ - 'ParticipantRole', - 'ParticipantEventName', - 'ParticipantEventType', - ]; - - if (in_array($extendsEntity, $participantEntities)) { - $group->extends = 'Participant'; - } - else { - $group->extends = $extendsEntity; - } - - $group->extends_entity_column_id = 'null'; - if (in_array($extendsEntity, $participantEntities) - ) { - $group->extends_entity_column_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name'); - } - - // this is format when form get submit. - $extendsChildType = $extends[1] ?? NULL; + $extendsChildType = NULL; // lets allow user to pass direct child type value, CRM-6893 if (!empty($params['extends_entity_column_value'])) { $extendsChildType = $params['extends_entity_column_value']; } if (!CRM_Utils_System::isNull($extendsChildType)) { - $registeredSubTypes = self::getSubTypes()[$extendsEntity]; + $registeredSubTypes = self::getSubTypes()[$params['extends']]; if (is_array($extendsChildType)) { foreach ($extendsChildType as $childType) { if (!array_key_exists($childType, $registeredSubTypes) && !in_array($childType, $registeredSubTypes, TRUE)) { @@ -88,8 +65,8 @@ public static function create(&$params) { } $extendsChildType = [$extendsChildType]; } - $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $extendsChildType); - if (CRM_Utils_Array::value(0, $extends) == 'Relationship') { + $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, $extendsChildType); + if ($params['extends'] == 'Relationship') { $extendsChildType = str_replace(['_a_b', '_b_a'], [ '', '', @@ -121,6 +98,8 @@ public static function create(&$params) { 'is_active', 'is_multiple', 'icon', + 'extends_entity_column_id', + 'extends', ]; $current_db_version = CRM_Core_BAO_Domain::version(); $is_public_version = version_compare($current_db_version, '4.7.19', '>='); @@ -214,7 +193,7 @@ public static function create(&$params) { $params['id'], 'table_name' ); - CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($extendsEntity)); + CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($params['extends'])); } $transaction->commit(); diff --git a/CRM/Custom/Form/Group.php b/CRM/Custom/Form/Group.php index 2b1cf9158eeb..ddf49e1c3a80 100644 --- a/CRM/Custom/Form/Group.php +++ b/CRM/Custom/Form/Group.php @@ -413,7 +413,8 @@ public function postProcess() { $params['created_date'] = date('YmdHis'); } - $group = CRM_Core_BAO_CustomGroup::create($params); + $result = civicrm_api3('CustomGroup', 'create', $params); + $group = $result['values'][$result['id']]; // reset the cache Civi::cache('fields')->flush(); @@ -421,14 +422,14 @@ public function postProcess() { CRM_Core_BAO_Cache::resetCaches(); if ($this->_action & CRM_Core_Action::UPDATE) { - CRM_Core_Session::setStatus(ts('Your custom field set \'%1 \' has been saved.', [1 => $group->title]), ts('Saved'), 'success'); + CRM_Core_Session::setStatus(ts('Your custom field set \'%1 \' has been saved.', [1 => $group['title']]), ts('Saved'), 'success'); } else { // Jump directly to adding a field if popups are disabled $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add'; - $url = CRM_Utils_System::url("civicrm/admin/custom/group/field$action", 'reset=1&new=1&gid=' . $group->id . '&action=' . ($action ? 'add' : 'browse')); + $url = CRM_Utils_System::url("civicrm/admin/custom/group/field$action", 'reset=1&new=1&gid=' . $group['id'] . '&action=' . ($action ? 'add' : 'browse')); CRM_Core_Session::setStatus(ts("Your custom field set '%1' has been added. You can add custom fields now.", - [1 => $group->title] + [1 => $group['title']] ), ts('Saved'), 'success'); $session = CRM_Core_Session::singleton(); $session->replaceUserContext($url); diff --git a/api/v3/CustomGroup.php b/api/v3/CustomGroup.php index 3c5519fc706f..f45c9ca3028b 100644 --- a/api/v3/CustomGroup.php +++ b/api/v3/CustomGroup.php @@ -40,6 +40,29 @@ function civicrm_api3_custom_group_create($params) { return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact')."); } + if (!isset($params['extends_entity_column_value']) && isset($params['extends'][1])) { + $extendsEntity = $params['extends'][0] ?? NULL; + $participantEntities = [ + 'ParticipantRole', + 'ParticipantEventName', + 'ParticipantEventType', + ]; + $params['extends_entity_column_id'] = 'null'; + if (in_array($extendsEntity, $participantEntities) + ) { + $params['extends_entity_column_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name'); + } + $params['extends_entity_column_value'] = $params['extends'][1]; + if (in_array($extendsEntity, $participantEntities)) { + $params['extends'] = 'Participant'; + } + else { + $params['extends'] = $extendsEntity; + } + } + elseif (isset($params['extends']) && (!isset($params['extends'][1]) || empty($params['extends'][1]))) { + $params['extends'] = $params['extends'][0]; + } if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) { // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']);