use Civi\Api4\Event\SchemaMapBuildEvent;
use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;
use Civi\Api4\Service\Schema\Joinable\Joinable;
+use Civi\Api4\Utils\CoreUtil;
use Civi\Core\Service\AutoService;
use Civi\Core\CiviEventDispatcherInterface;
use CRM_Core_DAO_AllCoreTables as AllCoreTables;
}
if ($fieldData->data_type === 'EntityReference' && isset($fieldData->fk_entity)) {
- $targetTable = AllCoreTables::getTableForEntityName($fieldData->fk_entity);
+ $targetTable = self::getTableName($fieldData->fk_entity);
$joinable = new Joinable($targetTable, 'id', $fieldData->name);
$customTable->addTableLink($fieldData->column_name, $joinable);
}
}
}
+ /**
+ * @param string $entityName
+ * @return string
+ */
+ private static function getTableName(string $entityName) {
+ if (CoreUtil::isContact($entityName)) {
+ return 'civicrm_contact';
+ }
+ return AllCoreTables::getTableForEntityName($entityName);
+ }
+
}
use Civi\Api4\Contact;
use Civi\Api4\CustomGroup;
use Civi\Api4\CustomField;
+use Civi\Api4\Individual;
+use Civi\Api4\Organization;
/**
* @group headless
'fk_entity' => 'Activity',
'filter' => "subject=$subject",
])->execute()->single();
+ // Spec should only exist for Individuals
+ $spec = Organization::getFields(FALSE)
+ ->addWhere('name', '=', 'EntityRefFields.TestActivityReference')
+ ->execute()->first();
+ $this->assertNull($spec);
// Check metadata
$spec = Contact::getFields(FALSE)
->addWhere('name', '=', 'EntityRefFields.TestActivityReference')
$this->assertGreaterThan(2, $result->countFetched());
}
+ /**
+ * Ensure custom fields of type EntityReference correctly apply filters
+ */
+ public function testEntityReferenceCustomFieldByContactType(): void {
+ CustomGroup::create()->setValues([
+ 'title' => 'EntityRefFields',
+ 'extends' => 'Individual',
+ ])->execute();
+ CustomField::create()->setValues([
+ 'label' => 'TestOrgRef',
+ 'custom_group_id.name' => 'EntityRefFields',
+ 'html_type' => 'Autocomplete-Select',
+ 'data_type' => 'EntityReference',
+ 'fk_entity' => 'Organization',
+ ])->execute()->single();
+ // Check metadata
+ $spec = Individual::getFields(FALSE)
+ ->addWhere('name', '=', 'EntityRefFields.TestOrgRef')
+ ->execute()->single();
+ $this->assertNull($spec['suffixes']);
+ $this->assertEquals('EntityRef', $spec['input_type']);
+ $this->assertEquals('Organization', $spec['fk_entity']);
+ // Check results
+ $contacts = $this->saveTestRecords('Contact', [
+ 'records' => [
+ ['contact_type' => 'Organization'],
+ ['contact_type' => 'Individual'],
+ ['contact_type' => 'Household'],
+ ],
+ ])->indexBy('contact_type')->column('id');
+ // Autocomplete by id
+ $result = (array) Organization::autocomplete(FALSE)
+ ->setFieldName("Contact.EntityRefFields.TestOrgRef")
+ ->setInput((string) $contacts['Organization'])
+ ->execute();
+ $this->assertCount(1, $result);
+ // Autocomplete by id
+ $result = (array) Organization::autocomplete(FALSE)
+ ->setFieldName("Contact.EntityRefFields.TestOrgRef")
+ ->setInput((string) $contacts['Individual'])
+ ->execute();
+ $this->assertCount(0, $result);
+ // Autocomplete by id
+ $result = (array) Organization::autocomplete(FALSE)
+ ->setFieldName("Contact.EntityRefFields.TestOrgRef")
+ ->setInput((string) $contacts['Household'])
+ ->execute();
+ $this->assertCount(0, $result);
+ // No field specified
+ $result = Contact::autocomplete(FALSE)
+ ->execute();
+ $this->assertGreaterThan(2, $result->countFetched());
+ }
+
}