Allow some countries to be the first options in the list

Created on 29 November 2024, 9 months ago

Problem/Motivation

Country lists are long and can be difficult to work with.

It can be an improvement if a couple of often selected countries are shown as the first options in the select list.

For example in a Dutch website that ships products to neighbouring countries Belgium and Germany, it could list these countries first.

Proposed resolution

- provide a UI to select countries that should be listed first
- sort countries in the widget showing the selected countries first

User interface changes

- sorting of the options

Feature request
Status

Active

Version

2.1

Component

User interface

Created by

🇳🇱Netherlands batigolix Utrecht

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @batigolix
  • 🇳🇱Netherlands batigolix Utrecht

    With the help of Claude I created a first draft. The priority works for a normal select box, but not for radio buttons yet

  • 🇳🇱Netherlands batigolix Utrecht
  • 🇵🇹Portugal jrochate

    Works as expected, but when is on first use, there are some warnings about undefined variables.

    - Edit a country field just after installing the patch:

    Warning: Undefined array key "priority_countries" in Drupal\country\Plugin\Field\FieldType\CountryItem->fieldSettingsForm() (line 142 of modules/contrib/country/src/Plugin/Field/FieldType/CountryItem.php) 
    
    Warning: Undefined array key "show_priority_separator" in Drupal\country\Plugin\Field\FieldType\CountryItem->fieldSettingsForm() (line 152 of modules/contrib/country/src/Plugin/Field/FieldType/CountryItem.php) 
    
    Warning: Undefined array key "priority_separator_text" in Drupal\country\Plugin\Field\FieldType\CountryItem->fieldSettingsForm() (line 159 of modules/contrib/country/src/Plugin/Field/FieldType/CountryItem.php) 
    
    Warning: Undefined array key "priority_countries" in Drupal\country\Plugin\Field\FieldType\CountryItem->fieldSettingsForm() (line 142 of modules/contrib/country/src/Plugin/Field/FieldType/CountryItem.php) 
    
    Warning: Undefined array key "show_priority_separator" in Drupal\country\Plugin\Field\FieldType\CountryItem->fieldSettingsForm() (line 152 of modules/contrib/country/src/Plugin/Field/FieldType/CountryItem.php) 
    
    Warning: Undefined array key "priority_separator_text" in Drupal\country\Plugin\Field\FieldType\CountryItem->fieldSettingsForm() (line 159 of modules/contrib/country/src/Plugin/Field/FieldType/CountryItem.php) 

    - After saving that specific country field definitions, the variables are set, so no more warnings.

  • 🇳🇱Netherlands batigolix Utrecht

    I updated the merge request

  • 🇵🇹Portugal jrochate

    Thanks. Looks good now.

  • 🇳🇱Netherlands batigolix Utrecht

    Thank you for reviewing this. I still have some doubts whether this new feature makes sense. Especially whether it should. be combined with the selected countries field.

    I will leave this open for a little while to get more feedback.

  • 🇵🇹Portugal jrochate

    Sure, no prob. I think is make a lot of sense, because it's normal to have a "most used" countries, but you don't want to loose the opportunity to know the rest of them.

    For example: select all european countries first, and then the rest of the world if your use case is primarily targeted to Europe.
    Or your own country first, because it will be 90% of the income users.

    Thank you for your efforts on this.

  • 🇳🇱Netherlands batigolix Utrecht

    I took a new approach and I created a dedicated field widget for the priority countries.

  • 🇳🇱Netherlands arjenk

    Works nice and I like the widget approach. Two suggestions:

    1. The indentation between the priority countries and the regular countries list seems a bit off. The priority countries appear to have less indentation compared to the regular list.

    2. Consider renaming the widget from "Select with priorities" to "Select list with priorities" to be more consistent with the naming convention of the original "Select list" widget.

  • 🇵🇹Portugal jrochate

    I also think the widget approach works better.

    I would like to add a request:

    - respect the default value defined on the field

    I mean, when there is no default value and the field is not mandatory, the fact that we use the widget to get priority countries shouldn't make the selection list pre-select the first priority country.
    This is happening now, and it looks like a bug.

    For me, the expected behaviour is only auto-fill the select box when the field has a pre-defined country selected, no mater has priority countries or not.

  • 🇵🇹Portugal jrochate

    @batigolix what about not having a default country selected, even if we have one or more countries in priority?

    There should be a default country selected only if we choose the default value on field config.

    Do you agree?

  • 🇳🇱Netherlands arjenk

    I agree with jrochate, the 'select list' widget shows initially: '- None -'.

    The 'select list with priorities' show by default the first country, a bit confusing.

    indentation is fixed now, naming of the widgets is consistent now,

    one minor code question:

        return [
          'priority_countries' => [],
          'show_priority_separator' => FALSE,
          'priority_separator_text' => '--- Other Countries ---',
          'priority_group_title' => '--- Most chosen countries ---',
        ] + parent::defaultSettings();
    

    I expected the use of $this->t() here?

  • 🇳🇱Netherlands batigolix Utrecht

    Thanks for the feedback. I made the requested changes. Please review again

  • Confirming requested/added functionality is maintained with inclusion of newest changes.

  • Status changed to Needs work about 1 month ago
  • 🇮🇳India divya.sejekan

    Patch gets applied successfully. But patch has white space.
    After patch is applied , i added a country field in CT , The country dropdown is better now , easy to use.
    Using drupal - 10.3.14
    - But i cannot see the indentation done , like the above screenshots
    Please verify
    Observation

    1. Pre-selected country UI (limited) - UI is fine
    Steps :
    - Add country field in CT
    - Fill Allowed number of values as limited
    - In field settings , select Set defaut value
    2. pre-selected multiple countries (unlimited) - Ui Not as described - Need to fix this
    - Add country field in CT
    - In field settings , select Selectable
    3. Box is shrinked for Selectable country option
    - Add country field in CT
    - In field setting - Selectable countries - select few countries and save
    4. Pre-selected country UI (limited-2) - UI is like old implementation
    Steps :
    - Add country field in CT
    - In field settings , select Set defaut value , Fill Allowed number of values as limited 2 values

  • 🇳🇱Netherlands batigolix Utrecht

    Check the README.md (included in the merge request) that explains how to create a separator between the prioritized and normal countries. This will enable the indentation.

    As an extension of a default Drupal select field, there is no setting for the size of the field. It adapts to to the longest available value.

    Please explain the issue with the whitespace in the patch (you mean in the merge request?) . The code passes the gitlab CI checks for code styling, so it should be good. But please explain in more details where it can be improved.

  • 🇳🇱Netherlands arjenk

    There are trailing whitespaces, but it is in the README.md file, so we can safely ignore this, as Drupal does not check md files with phpcs.

    Played around with the UI, and it works nice, could not find issues.

    minor thing: getEmptyLabel() can be simplified (removing duplicate code) by moving the

        if (!$this->required) {
          return $this->t('- None -');
        }
    

    to the beginning.

  • 🇳🇱Netherlands batigolix Utrecht
  • 🇳🇱Netherlands batigolix Utrecht
Production build 0.71.5 2024