You are here

function countries_country_lookup in Countries 7.2

Same name and namespace in other branches
  1. 8 countries.module \countries_country_lookup()
  2. 7 countries.module \countries_country_lookup()

A helper function to find a country based on any country property.

Parameters

string $value: Search keyword to find a country for.

string $property: The country property to search with. If empty, the code will attempt to guess what property the user is looking for. This is not recommended!

Return value

object The loaded country or FALSE if no match was found.

4 calls to countries_country_lookup()
CountriesFunctionsUnitTest::testCountriesLookupListing in tests/countries.test
This browes the admin listing, making sure that the countries are correctly listed.
countries_country_code_context in plugins/arguments/country_code.inc
Discover if this argument gives us the term we crave.
views_handler_argument_countries_country::title_query in views/views_handler_argument_countries_country.inc
Override the behavior of title_query() based on the user selected options.
_countries_feeds_set_target in ./countries.feeds.inc
Callback defined via countries_feeds_processor_targets_alter().

File

./countries.module, line 845
Defines the field and entity information for countries.

Code

function countries_country_lookup($value, $property = NULL) {
  if (empty($value)) {
    return FALSE;
  }

  // For lazy coders / sources, try to discover the $key from the $value.
  if (empty($property)) {
    $property = _countries_estimate_property_type($value);
  }

  // Helper function to maximise the lookup chances.
  switch ($property) {
    case 'iso2':
    case 'iso3':
      $value = drupal_strtoupper($value);
      break;
    case 'numcode':
      $value = intval($value);
      break;
    default:
      $value = trim($value);
      break;
  }
  foreach (countries_get_countries($property) as $iso2 => $country_value) {
    if ($value == $country_value) {
      return country_load($iso2);
    }
  }

  // Try the official name too when doing name searches.
  if ($property == 'name') {
    foreach (countries_get_countries('official_name') as $iso2 => $country_value) {
      if ($value == $country_value) {
        return country_load($iso2);
      }
    }

    // This is a fallback to try and find a match if none are found. This
    // should match common shorthand forms such as USA or US.
    $property = _countries_estimate_property_type($value);
    if ($property != 'name') {
      return countries_country_lookup($value, $property);
    }
  }
  return FALSE;
}