Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 - Exclude disabled custom fields #23583

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Civi/Api4/Service/Spec/SpecGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private function addCustomFields($entity, RequestSpec $spec) {

$query = CustomField::get(FALSE)
->setSelect(['custom_group_id.name', 'custom_group_id.title', '*'])
->addWhere('is_active', '=', TRUE)
->addWhere('custom_group_id.is_multiple', '=', '0');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw do we need to do something about the multi record custom fields as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I've updated the PR and added a test for that as well.


// Contact custom groups are extra complicated because contact_type can be a value for extends
Expand Down Expand Up @@ -196,6 +197,7 @@ private function addCustomFields($entity, RequestSpec $spec) {
private function getCustomGroupFields($customGroup, RequestSpec $specification) {
$customFields = CustomField::get(FALSE)
->addWhere('custom_group_id.name', '=', $customGroup)
->addWhere('is_active', '=', TRUE)
->setSelect(['custom_group_id.name', 'custom_group_id.table_name', 'custom_group_id.title', '*'])
->execute();

Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ public function testWithSingleField(): void {
->execute()
->first();
$this->assertEquals(NULL, $contact['MyIndividualFields.FavColor']);

// Disable the field and it disappears from getFields and from the API output.
CustomField::update(FALSE)
->addWhere('custom_group_id:name', '=', 'MyIndividualFields')
->addWhere('name', '=', 'FavColor')
->addValue('is_active', FALSE)
->execute();

$getFields = Contact::getFields(FALSE)
->execute()->column('name');
$this->assertContains('first_name', $getFields);
$this->assertNotContains('MyIndividualFields.FavColor', $getFields);

$contact = Contact::get(FALSE)
->addSelect('MyIndividualFields.FavColor')
->addWhere('id', '=', $contactId)
->execute()
->first();
$this->assertArrayNotHasKey('MyIndividualFields.FavColor', $contact);
}

public function testWithTwoFields() {
Expand Down
12 changes: 11 additions & 1 deletion tests/phpunit/api/v4/Custom/CustomValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testCRUD() {
$this->assertEquals('secondary', $entity['searchable']);

// Retrieve and check the fields of CustomValue = Custom_$group
$fields = CustomValue::getFields($group)->setLoadOptions(TRUE)->setCheckPermissions(FALSE)->execute();
$fields = CustomValue::getFields($group, FALSE)->setLoadOptions(TRUE)->execute();
$expectedResult = [
[
'custom_group' => $group,
Expand Down Expand Up @@ -252,6 +252,16 @@ public function testCRUD() {
}
}

// Disable a field
CustomField::update(FALSE)
->addValue('is_active', FALSE)
->addWhere('id', '=', $multiField['id'])
->execute();

$result = CustomValue::get($group)->execute()->single();
$this->assertArrayHasKey($colorFieldName, $result);
$this->assertArrayNotHasKey($multiFieldName, $result);

// CASE 4: Test CustomValue::delete
// There is only record left whose id = 3, delete that record on basis of criteria id = 3
CustomValue::delete($group)->addWhere("id", "=", 3)->execute();
Expand Down