You are here

function countries_filter in Countries 8

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

A helper function to filter country lists.

If the country code is not found in the database, the country will be not be return by this function. This is preferably called using:


countries_get_countries('name', array('enabled' => COUNTRIES_ENABLED)).

Parameters

array $countries: An array indexed by ISO2 country value. The actual value held in the array does not matter to this function.

array $filters: This allows you to get a full country list that is filtered. Currently, the defined filters includes:

enabled - Limits to results by country status. Valid options include:

countries - An array of ISO2 codes of countries to include. continents - An array of continent codes. Only countries assigned to these continents will be returned.

Return value

array The original array filtered using the supplied country filters.

4 calls to countries_filter()
countries_example in modules/countries_example/countries_example.module
Menu callback; example countries form.
countries_get_countries in ./countries.module
Helper function to load countries. This includes all countries by default.
views_handler_filter_countries_list::get_value_options in views/views_handler_filter_countries_list.inc
_countries_country_expand in ./countries.elements.inc
Our process callback to expand the country FAPI element.

File

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

Code

function countries_filter($countries, $filters = array()) {
  if (!empty($filters)) {
    $target_countries = array();
    foreach (countries_get_countries() as $country) {
      $include = TRUE;
      if (isset($filters['enabled'])) {
        $include &= $filters['enabled'] == COUNTRIES_ALL || $filters['enabled'] == COUNTRIES_ENABLED && $country->enabled || $filters['enabled'] == COUNTRIES_DISABLED && !$country->enabled;
      }
      if (!empty($filters['countries'])) {
        $include &= in_array($country->iso2, $filters['countries']);
      }
      if (!empty($filters['continents'])) {
        $include &= in_array($country->continent, $filters['continents']);
      }
      if ($include) {
        $target_countries[$country->iso2] = TRUE;
      }
    }
    $countries = array_intersect_key($countries, $target_countries);
  }
  return $countries;
}