You are here

function countries_get_countries in Countries 7.2

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

Helper function to load countries. This includes all countries by default.

This now accepts a list of filters to provide an easy method of returning a filtered list of countries.

Parameters

string $property: A property of the country to return rather than the entire country object. Leave unset or pass in 'all' to have the country objects returned.

array $filters: An array of filters. See countries_filter() for details.

Return value

array An array of countries ordered by name or by the specified property.

11 calls to countries_get_countries()
CountriesCacheUnitTest::testCache in tests/countries.test
CountriesFunctionsUnitTest::testCountriesLookupListing in tests/countries.test
This browes the admin listing, making sure that the countries are correctly listed.
countries_allowed_values in ./countries.fields.inc
Returns a set of valid countries of a countries field.
countries_countries_alter in ./countries.module
Implements hook_countries_alter().
countries_country_lookup in ./countries.module
A helper function to find a country based on any country property.

... See full list

2 string references to 'countries_get_countries'
countries_clear_caches in ./countries.module
Helper function to clear various caches.
countries_i18n_i18n_object_info in modules/countries_i18n/countries_i18n.module
Implements hook_i18n_object_info().

File

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

Code

function countries_get_countries($property = 'all', $filters = array()) {

  // Since this contains quite some operations cache the results at least for
  // this request.
  $cache =& drupal_static(__FUNCTION__, array());
  $cid = md5($property . serialize($filters));
  if (isset($cache[$cid])) {
    return $cache[$cid];
  }

  // Special construct to store the country codes once we fetched all. This will
  // speed up loading the entities if a second call is made - even with other
  // parameters, this is because this way we can hit the entity cache.
  $country_codes_cid = __FUNCTION__ . ':entity_ids';
  $country_codes = FALSE;
  if (isset($cache[$country_codes_cid])) {
    $country_codes = $cache[$country_codes_cid];
  }
  $countries = entity_load_multiple_by_name('country', $country_codes);
  if (!isset($cache[$country_codes_cid])) {
    $cache[$country_codes_cid] = array_keys($countries);
  }
  $filtered_countries = countries_filter($countries, $filters);
  if ($property == 'all' || empty($property)) {
    return $filtered_countries;
  }
  $core_properties = countries_core_properties();
  if (!isset($core_properties[$property])) {
    $test = current($countries);
    if (!isset($test->{$property})) {
      throw new Exception(t('Invalid property lookup in countries_get_countries()'));
    }
  }
  $mapped_countries = array();
  foreach ($filtered_countries as $country) {
    if (isset($country->{$property})) {
      if ($property == 'name') {
        $mapped_countries[$country->iso2] = $country
          ->label();
      }
      else {
        $mapped_countries[$country->iso2] = $country->{$property};
      }
    }
  }
  uasort($mapped_countries, 'countries_sort');
  $cache[$cid] = $mapped_countries;
  return $mapped_countries;
}