- $record[$fieldName] = \CRM_Core_DAO::getFieldValue($fkDao, $value, 'id', $fkField);
+ // Constrain search to the domain of the current entity
+ $domainConstraint = NULL;
+ if (isset($fkDao::getSupportedFields()['domain_id'])) {
+ if (!empty($record['domain_id'])) {
+ $domainConstraint = $record['domain_id'] === 'current_domain' ? \CRM_Core_Config::domainID() : $record['domain_id'];
+ }
+ elseif (!empty($record['id']) && isset($this->entityFields()['domain_id'])) {
+ $domainConstraint = \CRM_Core_DAO::getFieldValue($this->getBaoName(), $record['id'], 'domain_id');
+ }
+ }
+ if ($domainConstraint) {
+ $fkSearch = new $fkDao();
+ $fkSearch->domain_id = $domainConstraint;
+ $fkSearch->$fkField = $value;
+ $fkSearch->find(TRUE);
+ $record[$fieldName] = $fkSearch->id;
+ }
+ // Simple lookup without all the fuss about domains
+ else {
+ $record[$fieldName] = \CRM_Core_DAO::getFieldValue($fkDao, $value, 'id', $fkField);
+ }