The countries_api_get_list function is great if you want everything in the whole list. However, I would envision that many, if not most, of the applications for this are going to want to build an options list for a form. That would mean a compact array of code and name. Here's how I'm using it now, but it would be nice if I could get the list this way with a simple function call that could be right on the form.

  $list = countries_api_get_list();
  $country_list = array();
  foreach ($list as $country) {
    $country_list[$country['iso2']] = $country['name'];
  }

  $form['country']['#options'] = $country_list;
CommentFileSizeAuthor
#1 countries_api.patch919 bytesnancydru

Comments

nancydru’s picture

Assigned: Unassigned » nancydru
Status: Active » Needs review
StatusFileSize
new919 bytes

Here's a patch.

lejon’s picture

I tried the patch in my userprofile form and got this in response:

Fatal error: Cannot redeclare countries_api_select() (previously declared in E:\wamp\www\drupal\sites\all\modules\cck\text.module(351) : eval()'d code:1) in E:\wamp\www\drupal\sites\all\modules\cck\text.module(351) : eval()'d code on line 14

No I can't re-open the 'manage fields' tab to delete it.

Any ideas?

Thanks.

lejon’s picture

I hacked by just deleting all instances of 'field_country_station' in my database through phpMyadmin. probably not very good, but it worked.

I'm trying to get the list so I can a drop-down list for users that I can link to the flag of the country see here:

http://stefangabos.blogspot.com/2006/08/zebra-php-framework-countries-li...

But using countries API and flags from famfamfam which have the correct ISO 2-letter prefix. Can you suggest how to do this?

Thanks!

nancydru’s picture

The patch was to go into this module.

BTW, I might suggest that you go to http://drupal.org/node/213783 and add your 2 cents worth. We have several modules that provide country lists, yet they all are woefully under-supported and few kept up-to-date. Let's encourage the core developers to consider this type of feature in core.

I also found:

http://drupal.org/project/ip_locator
http://drupal.org/node/45806
http://leon.info.tm/en/ip2nation-module

lejon’s picture

Is there a way I can add a form field that produces the country list from the IP2nation database and then adds the flag?

Thanks!

nancydru’s picture

I'm sure there is, but that question should be directed to that module, not here.

summit’s picture

Hi,

I am a little but confused about this module.
Is this module related to the location module please?
Thanks in advance for replying!
greetings,
Martijn

mrfelton’s picture

Status: Needs review » Closed (won't fix)

@Summit:
The countries_api module is not related to the location module, although it is certainly does provide functionality in the same domain. Countries_api puerly provides an API for manipulation of country related data. Currently, the only data that is handled by countries_api is 2 and 3 letter ISO codes as well as printable country names. It is mainly intended to be used to provide this information to other modules that require it in some way. As far as I am aware, the location module actually contains it's own database of country codes that it uses for it's country lookups, and I would suggest that a module like location would be a prime candidate for outsourcing this functionality to a dedicated module like countries_api.

@NancyDru:
Regarding your original patch. I think what is actually needed is some optimisation on the various existing methods that the countries_api module provides - it should let you choose the columns that you want returned, rather than always returning all the columns. I would say that the building of a select list is to specialised to be included in the api itself, and should there should probably be a separate cck widget providing a select list, that depends upon (an optimised) countries_api module for its data.

mrfelton’s picture

Version: 5.x-1.0 » 5.x-1.x-dev
Status: Closed (won't fix) » Needs review

@NancyDru:
After re-reading your original post, I'm re thinking my response as you actually make the point, that this is something that many other modules could be using (location for example) to generate their select lists, and this is not limited to the use case of a cck widget.

mrfelton’s picture

Status: Needs review » Fixed

And on further inspection, I see that @ngmaloney has included this functionality in his recent updates to the module with the addition countries_api_get_options_array() which is a function for returning an associative array useful for FAPI select elements. However, this doesn't give the option of choosing a 2 or 3 letter code for the array key - it just gives the 2 letter ISO code and printable name.

If you think the choice of key would be useful, could you open a separate ticket for it please?

nancydru’s picture

This sat for so long with no updates that I have moved on and forgotten this. I was looking at updating the country code stuff in the Helpers module to use this.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.