Allow to set a list of preferred countries

Created on 1 December 2015, over 8 years ago
Updated 2 February 2024, 5 months ago

Problem/Motivation

We should allow the Address widget (and form element) to support a list of preferred countries that would be grouped on top of the country selector.

This is important because sites frequently have the most visitors from a few countries. United States ends up being more important than Afghanistan, for example.

Proposed resolution

Add a widget setting to specify the list of preferred countries.
Also add support for a #preferred_countries array within the address form element.

Remaining tasks

  1. Nah, we dropped the first opt-group label, and "Other" seems generic enough for everything else.
  2. Fixed via patch #13.
  3. We need to get the test for preferred_countries working
  4. Add validation for #preferred_countries: ensure it's an array, that if #available_countries is defined that everything in #preferred_countries is available, that the values are valid country codes, etc?
  5. Review + commit

User interface changes

A new 'Preferred countries' multi-select setting for the address field widget, and a corresponding change to the settings summary.
If a list of preferred countries is defined, the country select form element will use optgroups to have a 'Preferred' list at the top, and an 'Other' list for all the non-preferred countries.

API changes

Addition of a #preferred_countries array in the 'address' form element type.

Data model changes

Change to the widget settings configuration schema to hold the preferred_countries list.

Original report by bojanz

Preferred countries would be grouped on top of the country selector.

This is important because sites frequently have the most visitors from a few countries. United States ends up being more important than Afghanistan, for example.

✨ Feature request
Status

RTBC

Version

2.0

Component

User interface

Created by

πŸ‡·πŸ‡ΈSerbia bojanz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡¦Ukraine dinazaur

    During the editing of the shipping method got the error

    TypeError: array_combine(): Argument #1 ($keys) must be of type array, null given in array_combine() (line 90 of modules/contrib/address/src/Element/Country.php).
    

    Had no time to investigate it. Here is a quick fix for the error.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States dww

    Fabulous, thanks for moving this forward!

    Unfortunately, I still can't commit this without tests (as tagged), which the latest patch does not contain.

    Also, the summary has some remaining tasks

  • πŸ‡ͺπŸ‡ΈSpain psf_ Huelva

    With patch in #46, I use the field in a render array I get error:

    Simple form code:

    /**
       * {@inheritdoc}
       */
      public function buildForm(array $form, FormStateInterface $form_state) {
    
    
        $form['address'] = [
          '#type' => 'address',
          '#title' => $this->t('Address'),
        ];
    
        $form['actions'] = [
          '#type' => 'actions',
        ];
        $form['actions']['submit'] = [
          '#type' => 'submit',
          '#value' => $this->t('Send'),
        ];
    
        return $form;
      }
    

    The error:

    Warning: Undefined array key "#preferred_countries" in Drupal\address\Element\Address::processAddress() (line 192 of modules/contrib/address/src/Element/Address.php).
    
    Drupal\address\Element\Address::processAddress(Array, Object, Array)
    
  • Status changed to Needs review 7 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 7 months ago
    34 pass
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Added 2 test cases for this: one for views exposed filter setting and the other for widget configuration and make sure selected countries are displayed as only options directly in select and not optgroup.
    Also fixed code standards, added missing default values for Address and Country element info and fixed schema for views filter.

  • πŸ‡·πŸ‡ΈSerbia bojanz

    Thank you for the improvements, sickness29!

    Since this is a major new feature and change in behavior, let's keep it limited to the 2.0.x branch. I'd like to keep the legacy 8.x-1.x branch as stress-free as possible.

  • πŸ‡ΊπŸ‡ΈUnited States dww

    Agreed, this can be 2.0.x material at this point, since it's a potentially more "disruptive" new feature.

    That said, I'm really excited about this, and will try to make time to review + test soon. Probably within the first week or two of December...

    Thanks!
    -Derek

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 7 months ago
    35 pass
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Reworked #49 to apply to 2.0.x

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 6 months ago
    10 pass, 22 fail
  • πŸ‡¦πŸ‡·Argentina tguerineau

    Hi,

    I've tested the preferred_countries-2626982-52.patch patch with the current 2.0.x branch and found that the patch doesn't apply due to code expected from the 8.x-1.x branch.

    After applying the rejected hunks manually and resolving duplications in AddressDefaultWidget.php (defaultSettings, settingsForm and settingsSummary methods), I've successfully tested the module with preferred countries field.

    The attached patch includes these modifications and works as expected. Please find my re-rolled patch attached for review.

    Reworked #52 to apply to 2.0.x.

  • Status changed to Needs work 6 months ago
  • The last submitted patch, 53: preferred_countries-2626982-53.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Status changed to Needs review 6 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 6 months ago
    45 pass
  • πŸ‡¦πŸ‡·Argentina tguerineau

    Hello,

    I am posting an updated version of the patch preferred_countries-2626982-53.patch β†’ . This new patch addresses the issues encountered with the previous submission (preferred_countries-2626982-52.patch).

    Please find the attached re-rolled patch for review and testing. I welcome any feedback or further testing.

  • Status changed to RTBC 5 months ago
  • πŸ‡³πŸ‡ΏNew Zealand dieuwe Auckland, NZ

    Thanks, this applies and runs well against the latest dev. Would be nice to get this committed after all these years.

Production build 0.69.0 2024