There is no '- None -' option in the countries list.
You may not need to fill the country in certain cases. If it is, then you already have de standard field option to set the field to 'Obligatory'.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Alan D.’s picture

I've added one to the country element if it is not required. I'll hook this up latter to the field settings:

/**
 * Our process callback to expand the control.
 */
function countries_country_expand($element) {
  if (empty($element['#options'])) {
    if (empty($element['#filters'])) {
      include_once DRUPAL_ROOT . '/includes/locale.inc';
      $element['#options'] = country_get_list();
    }
    else {
      $element['#options'] = countries_get_countries('name');
    }
  }
  $element['#options'] = countries_filter($element['#options'], $element['#filters']);

  // If not required, add a none option. This can be overridden via setting
  // the $element['#empty_value'] parameter to NULL.
  if (empty($element['#required']) && !isset($element['#empty_value'])) {
    $element['#empty_value'] = '';
  }
  return $element;
}

Committed to master, and the issue is postponed to remind me to add field setting options latter.

Alan D.’s picture

Title: Option '- None -' not available. » Option '- None -' not available - add field settings.
Status: Active » Postponed
johnv’s picture

Status: Postponed » Needs work
FileSize
6.04 KB

Hi Alan, continuing on #1090416: Add 'checkboxes/radiobuttons' widget, where a checkboxes formatter is added, I propose some rework in your original code. The change above might not be necessary.
1. add widgets as in #1090416: Add 'checkboxes/radiobuttons' widget, these are defaults, and add the any/none-options by themselves.
2. this issue also inserts function countries_options_list($field);
3. we now have lost the continent filters in the new formatters; we can repair this by moving the settings from the widget to the field:

-function countries_field_widget_settings_form($field, $instance) {
+function countries_field_settings_form($field, $instance, $has_data) {

4. some more details are in the patch.
5. The 'size' option is still lost.
N.B. the patch may not apply cleanly, since I am working on 3 issues, and my not meet the commenting standards, but i have a very basic IDE.

johnv’s picture

I shouldn't do this too late in the evening. The patch does not save the field settings.
Let me know if you are interested in the change, and if I need to work on it a bit more.

webflo’s picture

Status: Needs work » Needs review
FileSize
1.64 KB

Hi i reworked this patch and introduced field settings rather than widget settings. This patch provides two core widget (selectbox and checkboxes/radios) as in #1090416: Add 'checkboxes/radiobuttons' widget and fixes the continent filter issue.

The size option is still available but not used in drupal core widgets.

johnv’s picture

Status: Needs review » Reviewed & tested by the community

I like this patch. You have kept the 'country select list', which is good since there is no upgrade path.
With admin/reports/fields people can find wher the countries-field is used, to change the widget

Alan D.’s picture

It looks like you both have been active. I really like the look of the use of hook_options_list(), I did not know of this hook!

If either would like access to git to push these changes into the dev version, contact me via my profile. I can not really review or test things out for the next two / three months as we are hitting the back roads of Peru / Bolivia and have very limited internet access.

Big thanks!

Alan

johnv’s picture

Alan, have a nice trip. Good to know that you're away. I'll do with the patches meanwhile.

johnv’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Reviewed & tested by the community » Fixed

This is fixed by adapting the default select/radio widgets.

Alan D.’s picture

Title: Option '- None -' not available - add field settings. » Convert custom widget to use the core field options widget.

I've just pushed through an update script that will convert the instances that use country widgets to options_list widgets and pull over the first found instance settings into the field settings (if not already set).

@webflo
The recursive continents filter has been simplified and renamed to _countries_checkbox_filter(). I discovered that the $element['#value'] is already filtered.

Status: Fixed » Closed (fixed)

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

Alan D.’s picture

Status: Closed (fixed) » Needs work

I think that I've found a minor issue with the code, but for the life of me I can find no documentation on this.

The existing code is:

/**
 * Implements hook_options_list().
 */
function countries_options_list($field) {
  $function = !empty($field['settings']['options_list_callback']) ? $field['settings']['options_list_callback'] : 'countries_country_expand';

  $filters = array(
    'continents' => array_filter($field['settings']['continents']),
    'enabled' => $field['settings']['enabled'],
  );

  if (!isset($field['#filters'])) {
    $field['#filters'] = $filters;
  }
  $field = $function($field);
  return $field['#options'];
}

But I think that the callback should be returning an options list if set. So I think that the valid usage is to not have this function to return the field and parse the options, but to return the results of that function directly. IE:

/**
 * Implements hook_options_list().
 */
function countries_options_list($field) {
  $function = !empty($field['settings']['options_list_callback']) ? $field['settings']['options_list_callback'] : 'countries_country_expand';

  $filters = array(
    'continents' => array_filter($field['settings']['continents']),
    'enabled' => $field['settings']['enabled'],
  );

  if (!isset($field['#filters'])) {
    $field['#filters'] = $filters;
  }
  if ($function == 'countries_country_expand') {
    $field = $function($field);
    return $field['#options'];
  }
  else {
    return $function($field);
  }
}

As I stated in the intro, I can not find the documentation on this and I have no idea how to test this either!

webflo’s picture

Status: Needs work » Needs review
FileSize
1.78 KB

Let the options_list_callback handle this instead of adding some special behavior for countries_country_expand in countries_options_list().

Alan D.’s picture

Looks good, but maybe this is superseded with the changes in #1292044: Code cleanup and filter changes?

Alan D.’s picture

Status: Needs review » Closed (duplicate)