diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 568e92a6336f..44c4e958c0f8 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -74,7 +74,21 @@ public static function create(&$params) { $extendsChildType = $params['extends_entity_column_value']; } if (!CRM_Utils_System::isNull($extendsChildType)) { - $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, $extendsChildType); + $registeredSubTypes = self::getSubTypes()[$extendsEntity]; + if (is_array($extendsChildType)) { + foreach ($extendsChildType as $childType) { + if (!array_key_exists($childType, $registeredSubTypes) && !in_array($childType, $registeredSubTypes, TRUE)) { + throw new CRM_Core_Exception('Supplied Sub type is not valid for the specified entitiy'); + } + } + } + else { + if (!array_key_exists($extendsChildType, $registeredSubTypes) && !in_array($extendsChildType, $registeredSubTypes, TRUE)) { + throw new CRM_Core_Exception('Supplied Sub type is not valid for the specified entitiy'); + } + $extendsChildType = [$extendsChildType]; + } + $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $extendsChildType); if (CRM_Utils_Array::value(0, $extends) == 'Relationship') { $extendsChildType = str_replace(['_a_b', '_b_a'], [ '', @@ -2200,4 +2214,39 @@ private static function buildGroupTree($entityType, $toReturn, $subTypes, $query return [$multipleFieldGroups, $groupTree]; } + public static function getSubTypes(): array { + $sel2 = []; + $activityType = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE); + + $eventType = CRM_Core_OptionGroup::values('event_type'); + $grantType = CRM_Core_OptionGroup::values('grant_type'); + $campaignTypes = CRM_Campaign_PseudoConstant::campaignType(); + $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE); + $participantRole = CRM_Core_OptionGroup::values('participant_role'); + + asort($activityType); + asort($eventType); + asort($grantType); + asort($membershipType); + asort($participantRole); + + $sel2['Event'] = $eventType; + $sel2['Grant'] = $grantType; + $sel2['Activity'] = $activityType; + $sel2['Campaign'] = $campaignTypes; + $sel2['Membership'] = $membershipType; + $sel2['ParticipantRole'] = $participantRole; + $sel2['ParticipantEventName'] = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); + $sel2['ParticipantEventType'] = $eventType; + $sel2['Contribution'] = CRM_Contribute_PseudoConstant::financialType(); + $sel2['Relationship'] = CRM_Custom_Form_Group::getRelationshipTypes(); + + $sel2['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL); + $sel2['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL); + $sel2['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL); + + CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($sel2); + return $sel2; + } + } diff --git a/CRM/Custom/Form/Group.php b/CRM/Custom/Form/Group.php index ebb516f79939..2b1cf9158eeb 100644 --- a/CRM/Custom/Form/Group.php +++ b/CRM/Custom/Form/Group.php @@ -176,38 +176,8 @@ public function buildQuickForm() { $this->assign('contactTypes', json_encode($contactTypes)); $sel1 = ["" => ts("- select -")] + CRM_Core_SelectValues::customGroupExtends(); - $sel2 = []; - $activityType = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE); - - $eventType = CRM_Core_OptionGroup::values('event_type'); - $grantType = CRM_Core_OptionGroup::values('grant_type'); - $campaignTypes = CRM_Campaign_PseudoConstant::campaignType(); - $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE); - $participantRole = CRM_Core_OptionGroup::values('participant_role'); - ksort($sel1); - asort($activityType); - asort($eventType); - asort($grantType); - asort($membershipType); - asort($participantRole); - - $sel2['Event'] = $eventType; - $sel2['Grant'] = $grantType; - $sel2['Activity'] = $activityType; - $sel2['Campaign'] = $campaignTypes; - $sel2['Membership'] = $membershipType; - $sel2['ParticipantRole'] = $participantRole; - $sel2['ParticipantEventName'] = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); - $sel2['ParticipantEventType'] = $eventType; - $sel2['Contribution'] = CRM_Contribute_PseudoConstant::financialType(); - $sel2['Relationship'] = self::getRelationshipTypes(); - - $sel2['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL); - $sel2['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL); - $sel2['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL); - - CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($sel2); + $sel2 = CRM_Core_BAO_CustomGroup::getSubTypes(); foreach ($sel2 as $main => $sub) { if (!empty($sel2[$main])) { diff --git a/tests/phpunit/api/v3/CustomGroupTest.php b/tests/phpunit/api/v3/CustomGroupTest.php index e689a66bf401..36e5f6f759c9 100644 --- a/tests/phpunit/api/v3/CustomGroupTest.php +++ b/tests/phpunit/api/v3/CustomGroupTest.php @@ -175,8 +175,7 @@ public function testCustomGroupExtendsMultipleCreate() { 'is_active' => 1, ]; - $result = $this->callAPIFailure('custom_group', 'create', $params, - 'implode(): Invalid arguments passed'); + $result = $this->callAPIFailure('custom_group', 'create', $params, 'Supplied Sub type is not valid for the specified entitiy'); } /**