Integrate Address with Search API

Created on 5 October 2016, over 7 years ago
Updated 27 March 2024, 3 months ago

Similar to its Drupal 7 version #1269608: Integrate Addressfield with Search API β†’

The Address module stores country and state information in the database as abbreviations such as NY for New York and US for United States. Unfortunately, this is how the Search API indexes the information meaning that the an aggregated fulltext search filter will not return results for 'United States' and 'New York' but only for US and NY. The Search API faceted search blocks for the Addressfield's country field does show the full name of the country but only the abbreviated letters for the state.

The Search API does have an API hook to alter the values of the items being indexed, function hook_search_api_index_items_alter(array &$items, SearchApiIndex $index), however, I can not find a way to change the values of the country and state abbreviations to the full name using any Address module's functions.

✨ Feature request
Status

RTBC

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States chriscalip Chicago, IL

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    This looks great, I'm not sure how viable it is to add integration tests for this and a unit test doesn't add much value, because it's only the integration with search api that matters.

    RTBC based on that.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 7 months ago
    32 pass
  • πŸ‡ΊπŸ‡ΈUnited States recrit

    After applying patch address-integrate_address_searchapi-2812659-74.patch, I started to notice the logs being littered with the following warning:

    Warning: Undefined array key "value" in Drupal\address\Plugin\search_api\processor\AddressAbbreviationsToFullForm->triangulateCodeToFulltext() (line 198...
    

    Cause:
    This is caused by the new code in patch 74 to account for the country field:

    $current = $result = $field_value[$target] ?? $field_value['value'];
    

    An address field can have optional properties so $field_value[$target] may actually be NULL for an address field (not a country field).

    Fix:
    The attached patch changes the line to the following to account for NULL values.

    $current = $result = $field_value[$target] ?? ($field_value['value'] ?? NULL);
    if (!isset($result)) {
      return $result;
     }
    

    The patch also updates the "preprocessIndexField" method to account for NULL to avoid string operations on a NULL value which causes deprecation notices in PHP 8.1

  • πŸ‡¦πŸ‡ΉAustria maxilein

    Does 2.0 work with search api?

  • πŸ‡·πŸ‡ΈSerbia bojanz

    Changing version to 2.x-dev, this will have to go into the 2.1.0 release, 8.x-1.x is now closed.

  • πŸ‡ΊπŸ‡¦Ukraine proweb.ua

    address 2.0.1
    #77 works

Production build 0.69.0 2024