I would like to entirely replace the country list, but unfortunately I could not find any help in the hook_countries_alter(&$countries) documentation :(
I have a list of countries saved as "country" content type and I would like to use this list to override the iso.inc list but I have problems with multilingualism.
Any hint? Here is my code:

<?php
function country_countries_alter(&$countries){
  $countries = array();
  $query = db_select("node", "n")
    ->fields("n", array("nid"))
    ->condition("type", "country");
  $results = $query->execute();
  foreach ($results as $result) {
    $node = node_load($result->nid);
    $node_wrapper = entity_metadata_wrapper('node',$node);
    $countries[$node_wrapper->field_country_iso2->value()] = t($node_wrapper->title->value());
  };
}
?>

What am I missing?

Thanks a lot!

Comments

Jaypan’s picture

What am I missing?

A clear description of the problem. Actually, a description of the problem altogether, clear or otherwise.

Think the Drupal forums suck? Add your agreement here: https://www.drupal.org/node/2641072

denix’s picture

Hi Jaypan,

after posting the question and going to sleep, I solved the problem.
The issue was that when I override the list of countries with my countries I was retrieving all the country names in all languages. I solved the issue adding a condition to the query. Here is the final code:

<?php
function country_countries_alter(&$countries){
  $countries = array();
  $query = db_select("node", "n")
    ->fields("n", array("nid"))
    ->condition("type", "country")
    ->condition(db_or()->condition('language', $language->language, '=')
    ->condition('language', LANGUAGE_NONE));
  $results = $query->execute();
  foreach ($results as $result) {
    $node = node_load($result->nid);
    $node_wrapper = entity_metadata_wrapper('node',$node);
    $countries[$node_wrapper->field_country_iso2->value()] = t($node_wrapper->title->value());
    asort($countries);
  };
}
?>

in case somebody else need to do the same thing.