City won't display in plain format is chosen for address fields

Created on 4 June 2025, about 1 month ago

When displaying a user's address in a user profile page, if the "Plain" format is selected for displaying the address, the locality (city) will not be displayed.

Enabled the Address module in Drupal 11. Create a new user and assign values to the address fields for that user. Observe that when the address is displayed within that user's profile, if the "Plain" address format is selected, the street, postal code, state/province and country will appear, but the locality (city) will not.

Ultimately, I don't know how to solve this, but I have come up with a work around for myself. I've added the following to mytheme.theme.
function mytheme_preprocess_user__compact(&$variables){
$variables['thecity'] = $variables['user']->field_address->locality;
}

Adding {{ thecity }} to user--compact.html.twig will show the locality in the user's compact profile.

I only need the city to show up in the compact display, but swapping the word full in place of compact in the above example would show it in the full user profile.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇨🇦Canada bcurry

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

Merge Requests

Comments & Activities

  • Issue created by @bcurry
  • 🇯🇵Japan liuyuanchao

    Maybe I can fix this.

  • 🇯🇵Japan liuyuanchao

    liuyuanchao changed the visibility of the branch 3528487-city-wont-display to hidden.

  • 🇯🇵Japan liuyuanchao

    I change the twig file to display the city in the case that `locality.code` not exists.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 535s
    #514824
  • 🇨🇦Canada bcurry

    Thanks for your insights. I tried the 2.1.x-dev version and it did not work for me. Although, full disclosure, after I installed 2.1.x-dev, the version does not appear under Address at: web/admin/modules . I later tried to install 2.0.4 and the version number is still not shown, so I don't know what version I'm actually running anymore.

    As a test, I also tried setting the country to Japan. Interestingly, if the country is Japan, you get a field for the City and a field for the Prefecture. In my case, it doesn't matter what City I type in. The user profile will always show the Prefecture twice and the City will not be shown. I assume it is showing the prefecture as the city in one of those cases.

    Most of my testing as been with US and Canadian cities.

    Something strange I've observed in the address-plain.html.twig is if I use {{ locality.code }} I don't get any errors, but I don't see the city. If I use: {{ locality }} I get the following error:

    InvalidArgumentException: "code" is an invalid render array key. Value should be an array but got a string. in Drupal\Core\Render\Element::children() (line 97 of /Applications/MAMP/htdocs/content/web/core/lib/Drupal/Core/Render/Element.php).

    It's counterintuitive to me that if I use ".code" in my twig file, there are no errors, if I take ".code" out of my twig file, I get an error citing "code".

  • 🇯🇵Japan liuyuanchao

    liuyuanchao changed the visibility of the branch 3528487-city-wont-display to active.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 458s
    #515627
  • 🇮🇳India divya.sejekan

    Could not reproduce the issue . I tried reproducing Japan. But i can see both city and Prefecture .Here i have used city - tokyo , Prefecture - Tokyo. Attached image for reference
    I have used D 11.1.8- , Address - 2.0.4
    Steps :
    1. Install Drupal , install module Address
    2. Added Field Address( An entity field containing a postal address) for People / Account Setting
    3. Created user

  • 🇯🇵Japan liuyuanchao

    Hi @divya.sejekan, Thank you for your testing.

    It seems like you didn't set the display format to plain.
    The default display format is not problem.

  • 🇮🇳India divya.sejekan

    Hi @liuyuanchao . Thank you for correcting me . Now im able to reproduce the issue by setting display to plain.

    After applying patch - 3528487-city-wont-display MR!70 . Issue is resolved , Able to see the city now.
    Tried with japan as well as other countries. Working as expected.
    Patch gets applied cleanly.

    Steps Followed :
    Steps :
    1. Install Drupal , install module Address
    2. Added Field Address( An entity field containing a postal address) for People / Account Setting . Set display - plain
    3. Created user

    RTBC ++ . Keeping in review for further code review

Production build 0.71.5 2024