I added a customized country select list to my profile page.
Countries are mostly written like 'Germany' or 'United Kingdom', but there are some, like 'Brazil, Federative Republic of', so it can be found under 'B'.

I have entered all countries into the text field, each on a single row. Drupal splits this data on every row AND on every comma.

I want

Burundi, Republic of
Cambodia, Kingdom of
Cameroon, United Republic of

but I get

Burundi
Republic of
Cambodia
Kingdom of
Cameroon
United Republic of

How can I change this behaviour? Yes, I know, but using some other symbols instead of a comma is the last option.

Thanks

Comments

captaindrunk’s picture

the question really is how are you generating or displaying the listed countries? select drop-down? multi-select?

Below is a snippet of what I use, it merely selects the name and numerical id of a country from a list (ISO based, includes comma'd names like you are describing) and orders it according to the country id.

  $country_formname="country";
  $country_options[] = t('Select Country');
  $country_result = db_query("SELECT name, world_countryid FROM countryid ORDER BY world_countryid");
  while ($world_country = db_fetch_object($country_result)) {
   $country_options[$world_country->name] = $world_country->name;
  }
  $form['country'] = array(
        '#type' => 'select',
        '#title' => t('Please select the country you live in'),
        '#default_value' => '',
        '#options' => $country_options,
        '#description' => 'Select the country you live in',
        '#multiple' => $multiple = FALSE,
        '#required' => $required = TRUE,
        );

Of note, because the comma is used in SQL queries, be careful how you insert your country names so it doesn't treat it as a field delimiter (although it would typically error out)

suit4’s picture

I used the profile.module and added a filed with type 'list selection'. Then I added the data in 'selection options'.
I didn't customize anything there and there is no special view to this page, just a modified profile.

I don't have any idea, where to put your code and build up a profile submission form by hand.

I THink, this is more an issue in the profile.module.

It splits the text field content by newline, by linefeed an by comma.

...
      case 'selection':
        $options = $field->required ? array() : array('--');
        $lines = split(",[\n\r]", $field->options);
        foreach ($lines as $line) {
          if ($line = trim($line)) {
            $options[$line] = $line;
          }
        }
...

For now, i removed the comma on the split command.

I'm going to file an issue for the profile.module. I think, splitting by comma AND newline is too much or, at least should be mentioned.
It would be nice to have this configurable.

Thanks, anyway.

Maybe you like to tell me, how to use your code?

captaindrunk’s picture

the code sample I provided is part of the forms API, in order to use it you would need to build a custom form.

suit4’s picture

Thanks for the code,
but I'll stick with my way for now.

Actually I got it working with my workaround.
Now I can use those country list entries to send a query to yahoo maps and get a geocode for the country and city (if set) :)

cboshuizen’s picture

I see this didn't get fixed. I hacked the code the same as you.

Chris