diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index dd410fd6d638..da87cfbc49ca 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1580,7 +1580,30 @@ protected function getTransformedFieldValue(string $fieldName, $importedValue) { } $comparisonValue = $this->getComparisonValue($importedValue); - return $options[$comparisonValue] ?? 'invalid_import_value'; + $resolvedValue = $options[$comparisonValue] ?? 'invalid_import_value'; + if (in_array($fieldName, ['state_province_id', 'county_id'], TRUE) && $resolvedValue === 'invalid_import_value') { + if ($fieldName === 'state_province_id') { + $stateID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_state_province WHERE name = %1', [1 => [$comparisonValue, 'String']]); + if (!$stateID) { + $stateID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_state_province WHERE abbreviation = %1', [1 => [$comparisonValue, 'String']]); + } + if ($stateID) { + $this->importableFieldsMetadata['state_province_id']['options'][$comparisonValue] = $stateID; + return $stateID; + } + } + if ($fieldName === 'county_id') { + $countyID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_county WHERE name = %1', [1 => [$comparisonValue, 'String']]); + if (!$countyID) { + $countyID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_county WHERE abbreviation = %1', [1 => [$comparisonValue, 'String']]); + } + if ($countyID) { + $this->importableFieldsMetadata['county_id']['options'][$comparisonValue] = $countyID; + return $countyID; + } + } + } + return $resolvedValue; } // @todo - make this generic - for fields where getOptions doesn't fetch // getOptions does not retrieve these fields with high potential results diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_country_state.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_country_state.csv new file mode 100644 index 000000000000..7cc5d2c2d2fe --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_country_state.csv @@ -0,0 +1,2 @@ +First Name,Last Name,State,Country +Bob,Smith,Alberta,Canada diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 7acc64fc00cd..30f1c62549c8 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -325,6 +325,28 @@ public function testImportMasterAddress(): void { $this->assertEquals('out yonder', $newAddress['street_address']); } + /** + * @throws \CRM_Core_Exception + */ + public function testImportNonDefaultCountryState(): void { + \Civi::settings()->set('defaultContactCountry', 1228); + $csv = 'individual_country_state.csv'; + $mapper = [ + ['first_name'], + ['last_name'], + ['state_province', 'Primary'], + ['country', 'Primary'], + ]; + $this->validateCSV($csv, $mapper); + $this->importCSV($csv, $mapper); + $address = Address::get(FALSE) + ->addWhere('country_id.name', '=', 'Canada') + ->addWhere('state_province_id.name', '=', 'Alberta') + ->addSelect('contact_id.display_name') + ->execute()->single(); + $this->assertEquals('Bob Smith', $address['contact_id.display_name']); + } + /** * Test updating an existing contact with external_identifier match but * subtype mismatch.