You are here

function countries_filter in Countries 7.2

Same name and namespace in other branches
  1. 8 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
Child classes should be used to override this function and set the 'value options', unless 'options callback' is defined as a valid function or static public method to generate these values.
_countries_country_expand in ./countries.elements.inc
Our process callback to expand the country FAPI element.

File

./countries.module, line 559
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;
}