Views sorting by admin area name

Created on 3 December 2021, over 2 years ago
Updated 27 December 2023, 6 months ago

Problem/Motivation

Currently, when you have a list of entities with an address field, there is no way to sort them by the administrative area name in views.

If you try to sort results by admin area they are ordered alphabetically by their ISO code value which does not match the alphabetical order of their name/label causing results to sorted in an apparently random order.

Proposed resolution

Implement an Admin Area views sort similar to the Country one that was introduced in #2783699: Views sorting by country name β†’

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ctrlADel North Carolina, USA

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.

  • πŸ‡ΊπŸ‡ΈUnited States cindytwilliams Nashville, TN

    Patch #4 works well for me. It adds a "sort by" option in the view, which allows us to sort the results by either the name or the code.

  • Status changed to Needs work 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dww

    Generally +1 to this. A few things needed before we can commit it:

    1. Unlike #2783699: Views sorting by country name β†’ , this patch has no tests. πŸ˜… Since y'all mostly copied the Views plugin from that issue, check out commit 9a8756aeb, tests/src/Functional/Views/CountrySortTest.php and tests/modules/address_test/config/optional/views.view.address_test_sort_country.yml for inspiration.
    2. Minor doc nit: + * Constructs a new Country object. that's not the class being constructed. I hate our boilerplate standards for constructors, but as long as those are still the standard, we need to be accurate with the docs.

    Thanks!
    -Derek

  • Status changed to Needs review 8 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 8 months ago
    34 pass
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Same code from #4 with new browser test for administrative area sort handler. Also fixed constructor phpdoc.

  • Status changed to Needs work 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dww

    Thanks! That was fast. πŸ˜… However:

    +    $this->assertSession()->pageTextContains('1-AL');
    +    $this->assertSession()->pageTextContains('2-AS');
    +    $this->assertSession()->pageTextContains('3-CT');
    +    $this->assertSession()->pageTextContains('4-CO');
    +    $this->assertSession()->pageTextContains('5-MA');
    +    $this->assertSession()->pageTextContains('6-ME');
    +    $this->assertSession()->pageTextContains('7-MD');
    

    That maps to this list:

    1. Alabama
    2. American Samoa
    3. Connecticut
    4. Colorado
    5. Massachusetts
    6. Maine
    7. Maryland

    That's buggy. Colorado vs. Connecticut are out of order, as is Massachusetts (which should be last).

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

    p.s. I'd love to see these full names as at least comments in the test code so we can reason about such things without having to pull up vendor/commerceguys/addressing/resources/subdivision/US.json to compare.

    p.p.s. IA (Iowa) would be another good state to include in this test, since the code would sort first among other "I" states, but the name should be last (after Illinois and Indiana).

  • Assigned to sickness29
  • Issue was unassigned.
  • Status changed to Needs review 8 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 8 months ago
    34 pass
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Updated patch with fix for solution from 4 which put all administrative areas for all countries in same sql expression and it bugged because some countries had administrative areas with same names. Fixed that by adding country check as well so administrative area's "weight" only applied when country and area match.
    Also updated test to contain full admin area names and added Iowa with Illinois, replaced AL/AS with AZ/AR which makes more sense for testing purposes.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update 7 months ago
    33 pass, 2 fail
  • Status changed to Needs work 7 months ago
  • πŸ‡·πŸ‡ΈSerbia bojanz

    2.0.x test is failing with:
    ```
    1) Drupal\Tests\address\Functional\Views\AdministrativeAreaSortTest::testSortAdministrativeAreaName
    Exception: Deprecated function: ctype_upper(): Argument of type int will be interpreted as string in the future
    Drupal\address\Plugin\views\sort\AdministrativeArea->query()() (Line: 117)
    ```

    Please check how ctype_upper() is getting a non-string administrative area code passed in.

  • 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
    40 pass
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Hi @bojanz
    with update of commerceguys/addressing we now have many admin area codes which are numeral, and ctype_upper was used to check for format before passing it into mysql case.
    I have added a check for string there and it should be good now

  • πŸ‡·πŸ‡ΈSerbia bojanz
    +        $subdivisions = $this->subdivisionRepository->getList([$country_code], $this->langcode);
    +        foreach (array_keys($subdivisions) as $administrative_area_code) {
    +          // Administrative_area codes format check.
    +          if (is_string($administrative_area_code) && ctype_upper($administrative_area_code)) {
    +            $when[] = "WHEN $country_field = '$country_code' AND $field_name = '$administrative_area_code' THEN " . $i++;
    +          }
    +        }
    

    So if an administrative area code is numeric (e.g. "82"), we will just skip it, and the handler won't work for that country?
    Can't we just not pass it to ctype_upper() in that case, and add it as-is to the query?

Production build 0.69.0 2024