Change format of the address

Created on 21 October 2024, about 1 month ago

Problem/Motivation

I've configure Address Suggestion with Mapbox successfully, however the display of the field (plain text) is not correct. It's placing the street number after the street address. eg. Johnston Street, 34 event though the autocomplete on the field is formatting the address correctly for our country (Australia) eg. 34 Johnston Street

How can we adjust the format of the output to the address in plain text? And how can we hide things like Country?

Steps to reproduce

1. Create an address field on a content type
2. Configure form display with Address Suggestion using Mapbox
3. Change the display to Default or Plain Text
4. Create a node, search for an address and select it.

Expected result: Street address formatted the same as what is seen in the autocomplete for the Address field eg. 34 Johnston Street

Actual result: The format changes to put the street number at the end after a comma eg. Johnston Street, 34

Things I've checked: Mapbox dashboard and options, Manage Field, Manage Form Display, Manage Display

Proposed resolution

Allow changes to the format of addresses and allow to hide country. Can this be done with a custom template in the custom theme? Or can we get config fields in the Drupal admin UI?

Remaining tasks

User interface changes

Manage Display for address field have fields to show, hide parts of the address and change the format of the outputted string.

API changes

Data model changes

Feature request
Status

Active

Version

1.0

Component

User interface

Created by

🇦🇺Australia Spanners Sydney

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

Comments & Activities

  • Issue created by @Spanners
  • 🇫🇷France lazzyvn paris

    Some country have format Street number street name.
    You can debug in modules/contrib/address_suggestion/src/Plugin/AddressProvider/MapboxGeocoding.php

    $countryFormatSpecial = ['FR', 'CA', 'IE', 'IN', 'IL', 'HK', 'MY', 'OM',  'NZ', 'PH', 'SA', 'SE', 'SG', 'LK', 'TH', 'UK', 'US', 'VN', ];
    

    I think your site config with another country in list not AU

  • 🇦🇺Australia Spanners Sydney

    I tried updating the array with `AU` but it didn't help.

    $countryFormatSpecial = ['AU', 'FR', 'CA', 'IE', 'IN', 'IL', 'HK', 'MY', 'OM',
          'NZ', 'PH', 'SA', 'SE', 'SG', 'LK', 'TH', 'UK', 'US', 'VN',
        ];

    I actually updated it for all the providers, in case there was an inheritance, but no good either.

    I also tried changing the output by adding text at the start of both options in the if else.

    foreach ($content["features"] as $key => $feature) {
          $results[$key]['street_name'] = $feature["text"];
          if (!empty($feature["address"])) {
            if (!empty($country) && in_array($country, $countryFormatSpecial)) {
              $results[$key]['street_name'] = 'Thisisedited' . $feature["address"] . ' ' . $results[$key]['street_name'];
            }
            else {
              $results[$key]['street_name'] .= 'Thisisedited' . isset($feature["address"]) ? ', ' . $feature["address"] : '';
            }
          }

    But after clearing cache and running cron, resaving the node, trying creating a new node, there was no change to the output.

    Double checked that Australia was set in Regional settings and also changed to France, but no change.

    Any other ideas?

  • 🇫🇷France lazzyvn paris

    I think it supports number street format with address field not text field, because in address field we can select country and text field has no hope. Can't interface template, but you can write your own plugin widget
    example

    
    /**
     * @AddressProvider(
     *   id = "mapbox_geocoding_au",
     *   label = @Translation("Mapbox Geocoding"),
     *   api = "https://api.mapbox.com/geocoding/v5/mapbox.places/",
     * )
    
    class MapboxGeocodingAU extends MapboxGeocoding{
    public function processQuery($string, $settings) {
    // call parent processQuery and rewrite $result label with your format
    }
    }
    
Production build 0.71.5 2024