Skip to content

Commit

Permalink
Merge pull request #25994 from colemanw/groupParents
Browse files Browse the repository at this point in the history
SearchKit - Support the `Group.parents` field
  • Loading branch information
colemanw authored Apr 17, 2023
2 parents 50e195f + 77b92cf commit 4fe4df2
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 14 deletions.
15 changes: 13 additions & 2 deletions CRM/Contact/DAO/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/Group.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:d9453f2a948783a20abc574cc0ba860a)
* (GenCodeChecksum:65f55d4c1449a2daf78f584395974789)
*/

/**
Expand Down Expand Up @@ -555,6 +555,9 @@ public static function &fields() {
'bao' => 'CRM_Contact_BAO_Group',
'localizable' => 0,
'serialize' => self::SERIALIZE_SEPARATOR_BOOKEND,
'html' => [
'type' => 'Select',
],
'pseudoconstant' => [
'optionGroupName' => 'group_type',
'optionEditPath' => 'civicrm/admin/options/group_type',
Expand Down Expand Up @@ -618,8 +621,16 @@ public static function &fields() {
'bao' => 'CRM_Contact_BAO_Group',
'localizable' => 0,
'serialize' => self::SERIALIZE_COMMA,
'html' => [
'type' => 'EntityRef',
'label' => ts("Parent Group(s)"),
],
'pseudoconstant' => [
'callback' => 'CRM_Core_PseudoConstant::allGroup',
'table' => 'civicrm_group',
'keyColumn' => 'id',
'labelColumn' => 'title',
'nameColumn' => 'name',
'prefetch' => 'FALSE',
],
'add' => '2.1',
],
Expand Down
5 changes: 0 additions & 5 deletions CRM/Core/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,6 @@ public static function &countryIsoCode($id = FALSE) {
* array reference of all groups.
*/
public static function allGroup($groupType = NULL, $excludeHidden = TRUE) {
if ($groupType === 'validate') {
// validate gets passed through from getoptions. Handle in the deprecated
// fn rather than change the new pattern.
$groupType = NULL;
}
$condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden);
$groupKey = ($groupType ? $groupType : 'null') . !empty($excludeHidden);

Expand Down
4 changes: 4 additions & 0 deletions Civi/Api4/Service/Spec/SpecFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public static function arrayToField(array $data, $entity) {
if ($fkAPIName || $fkClassName) {
$field->setFkEntity($fkAPIName ?: CoreUtil::getApiNameFromBAO($fkClassName));
}
// For pseudo-fk fields like `civicrm_group.parents`
elseif (($data['html']['type'] ?? NULL) === 'EntityRef' && !empty($data['pseudoconstant']['table'])) {
$field->setFkEntity(CoreUtil::getApiNameFromTableName($data['pseudoconstant']['table']));
}

return $field;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/search_kit/Civi/Search/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private static function addImplicitFKFields(array $schema):array {
foreach ($schema as &$entity) {
if ($entity['searchable'] !== 'bridge') {
foreach (array_reverse($entity['fields'] ?? [], TRUE) as $index => $field) {
if (!empty($field['fk_entity']) && !$field['options'] && !empty($schema[$field['fk_entity']]['label_field'])) {
if (!empty($field['fk_entity']) && !$field['options'] && !$field['suffixes'] && !empty($schema[$field['fk_entity']]['label_field'])) {
$isCustom = strpos($field['name'], '.');
// Custom fields: append "Contact ID" etc. to original field label
if ($isCustom) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@
_.each(fields, function(field) {
var item = {
// Use options suffix if available.
id: prefix + field.name + (field.options && _.includes(field.suffixes || [], suffix.replace(':', '')) ? suffix : ''),
id: prefix + field.name + (_.includes(field.suffixes || [], suffix.replace(':', '')) ? suffix : ''),
text: field.label,
description: field.description
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class="form-control"
ng-model="$ctrl.value"
crm-autocomplete="$ctrl.getFkEntity()"
crm-autocomplete-params="{fieldName: $ctrl.field.entity + '.' + $ctrl.field.name}"
crm-autocomplete-params="{fieldName: $ctrl.field.entity + '.' + $ctrl.field.name, key: $ctrl.optionKey || null}"
auto-open="true"
multi="$ctrl.isMulti()"
static-options="$ctrl.getAutocompleteStaticOptions()"
Expand Down
9 changes: 6 additions & 3 deletions tests/phpunit/api/v3/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,16 @@ public function testGroupCreateWithTypeAndParent() {

/**
* Test that an array of valid values works for group_type field.
* FIXME: Api4
* @var int $version
* @dataProvider versionThreeAndFour
*/
public function testGroupTypeWithPseudoconstantArray() {
public function testGroupTypeWithPseudoconstantArray($version) {
$this->_apiversion = $version;
$groupType = $version == 3 ? 'group_type' : 'group_type:name';
$params = [
'name' => 'Test Group 2',
'title' => 'Test Group 2',
'group_type' => ['Mailing List', 'Access Control'],
$groupType => ['Mailing List', 'Access Control'],
'sequential' => 1,
];
$group = $this->callAPISuccess('Group', 'create', $params);
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/api/v4/Entity/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,29 @@ public function testCreate() {
->execute();
}

public function testGetParents() {
$parent1 = Group::create(FALSE)
->addValue('title', uniqid())
->execute()->single();
$parent2 = Group::create(FALSE)
->addValue('title', uniqid())
->execute()->single();
$child = Group::create(FALSE)
->addValue('title', uniqid())
->addValue('parents', [$parent1['id'], $parent2['id']])
->execute()->single();

$get = Group::get(FALSE)
->addWhere('id', '=', $child['id'])
->addSelect('parents')
->execute()->single();
$this->assertEquals([$parent1['id'], $parent2['id']], $get['parents']);

$get = Group::get(FALSE)
->addWhere('id', '=', $child['id'])
->addSelect('parents:label')
->execute()->single();
$this->assertEquals([$parent1['title'], $parent2['title']], $get['parents:label']);
}

}
13 changes: 12 additions & 1 deletion xml/schema/Contact/Group.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@
<pseudoconstant>
<optionGroupName>group_type</optionGroupName>
</pseudoconstant>
<html>
<type>Select</type>
</html>
<serialize>SEPARATOR_BOOKEND</serialize>
<add>1.9</add>
</field>
Expand Down Expand Up @@ -184,8 +187,16 @@
<add>2.1</add>
<serialize>COMMA</serialize>
<pseudoconstant>
<callback>CRM_Core_PseudoConstant::allGroup</callback>
<table>civicrm_group</table>
<keyColumn>id</keyColumn>
<nameColumn>name</nameColumn>
<labelColumn>title</labelColumn>
<prefetch>FALSE</prefetch>
</pseudoconstant>
<html>
<type>EntityRef</type>
<label>Parent Group(s)</label>
</html>
</field>
<field>
<name>children</name>
Expand Down

0 comments on commit 4fe4df2

Please sign in to comment.