There two issues.

The first is very simple, missed the variable in address\templates\address-plain.html.twig.
Need to add {{ locality.name }}

The second issue related if in "Manage fields" was unchecked "Administrative area" so in this case "Administrative area" always empty and in address\src\Plugin\Field\FieldFormatter\AddressPlainFormatter.php works break in foreach.
Some errors in AddressPlainFormatter.php

I submit a patch.
Many thanks for a beautiful module.

Comments

Ruslan P created an issue. See original summary.

ruslan piskarov’s picture

dww’s picture

Status: Patch (to be ported) » Needs review

Thanks for the issue and the patch! This is a more appropriate status, indicating that you have some code that needs review. There's nothing to be "ported" yet, which usually implies porting to a different version/branch. However, address currently still only has a single 8.x-1.x branch.

Anyway, I'll hopefully take a look and try this in the next few days. Maybe someone else will get to it before then.

Cheers,
-Derek

jonraedeke’s picture

Status: Needs review » Reviewed & tested by the community

Patch works well. Thanks!

ruslan piskarov’s picture

Thank you @jonraedeke.

bojanz’s picture

Status: Reviewed & tested by the community » Needs work

This patch is fixing a bug, but there is no failing test to indicate what the bug is (the break/continue part, it seems).

The template intentionally doesn't have locality.name, because it has locality.code
The same logic is used for administrative_area as well.

dww’s picture

Tagging this that it needs a test demonstrating the bug. We should have a test-only patch that adds a new test that fails, and a full patch with the same test and the fix from #2 that passes.

Also, adding this to the list of possible issues for the 8.x-1.8 release.

Cheers,
-Derek

siavash’s picture

Thanks for the patch!

I was getting this issue on one of the listings I had:
Notice: Undefined index: locality in .../docroot/modules/contrib/address/src/Plugin/Field/FieldFormatter/AddressPlainFormatter.php on line 196

Updating the code to use !empty() instead of !== NULL fixed my issue:

$parents[] = ($index && $index > 0 && !empty($original_values[$subdivision_fields[$index - 1]]) )

ruslan piskarov’s picture

ruslan piskarov’s picture

Thank you @Siavash.
Updated the patch with your fix.
Unfototunatly, currency I am busy and can't provide a patch with tests and test-only. Will come back later.

akshayadhav’s picture

Status: Needs work » Reviewed & tested by the community

#9 works well. I could use {{ locality.name }} to override the plain address format twig.

mmenavas’s picture

It looks like I'm not the only one who is happy with the patch. What's keeping it from being merged?

ruslan piskarov’s picture

I use this patch for 6 year for all projects. Will be nice if it will be merged.

bojanz’s picture

Status: Reviewed & tested by the community » Needs work

My entire feedback from #6 4 years ago is unchanged:

The template intentionally doesn't have locality.name, because it has locality.code
The same logic is used for administrative_area as well.

So right now this issue looks like a "won't fix" unless we can prove an actual problem with a failing test.

marcusx’s picture

StatusFileSize
new27.15 KB

For "Italien" Addresses there is no locality.code. The locality name is just a string in 'locaity'. And what does code in this context mean? I understand "code" for the country is the ISO shortcut. But why would the city name e.g. "Berlin" for a German address be in `locality.code` and not `locality.name` ?

See:
Locality Italy

sickness29’s picture

Assigned: Unassigned » sickness29
sickness29’s picture

Status: Needs work » Needs review
StatusFileSize
new6.06 KB

Added only tests per @dww comment that check plain address formatter ,so this should fail.
There are 2 tests: one to check if all address data present in plain formatter output and the other checks if all data is shown when some property is optional and empty (apart from country_code which determines empty field).

sickness29’s picture

Assigned: sickness29 » Unassigned
StatusFileSize
new8.03 KB

Now I apply same tests from #17 with fixes from #9 into one patch.
This should now pass.

bojanz’s picture

Status: Needs review » Needs work

For "Italien" Addresses there is no locality.code. The locality name is just a string in 'locaity'. And what does code in this context mean? I understand "code" for the country is the ISO shortcut. But why would the city name e.g. "Berlin" for a German address be in `locality.code` and not `locality.name` ?

The template file that you're modifying tells you this:

 * if a subdivision (dependent_locality, locality, administrative_area) was
 * entered, the array will always have a code. If it's a predefined subdivision,
 * it will also have a name. The code is always preferred.

This is because if a subdivision is predefined, it has two names, "name" which is supposed to be used in the UI when selecting a value, and "code" which is supposed to be shown when rendering an address. Sometimes this "code" matches the "name", sometimes it matches an ISO code, depending on the rules for that country. The use of "code" to mean user-facing label is unfortunate, but we inherited that decision from Google 10 years ago and we can't change it.

We can extend the comment in the template file to explain what I just explained here.

@sickness29
Thank you for illustrating a case in which the current code fails, something I've been asking for since 2019.

However, we already have a AddressPlainFormatterTest in Drupal\Tests\address\Kernel\Formatter, can we add a new test case there instead of introducing an independent test?

Furthermore, the twig file changes don't seem correct, the twig file now looks like this:

  {% if locality.name %}
    {{ locality.name }} <br>
  {% endif %}
  {% if dependent_locality.code %}
    {{ dependent_locality.code }} <br>
  {% endif %}
  {% if locality.code or postal_code or administrative_area.code %}
    {{ locality.code }} {{ postal_code }} {{ administrative_area.code }} <br>
  {% endif %}

So we output the locality, then the dependent_locality, then the locality again?

I still think this patch should not make any changes to the twig file itself, nor should it switch "code" for "name" in the formatter plugin.

sickness29’s picture

Status: Needs work » Needs review
StatusFileSize
new4.52 KB

Hi @bojanz
I have added changes to Kernel\Formatter\AddressPlainFormatterTest to illustrate the issue in test failure.
When only country and admin area were selected, there is no admin area in output, however it should be as module allows optional values.
I will add this same test with fix as next comment

sickness29’s picture

bojanz’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests +Needs reroll

Fix looks great now. Needs a reroll though, #3144823: The country is rendered in the wrong language when the entity is translatable changed the tests.

  • bojanz committed 32a09e4b on 8.x-1.x authored by sickness29
    Issue #2881391 by sickness29, Ruslan Piskarov, bojanz: The locality.name...
bojanz’s picture

Version: 8.x-1.x-dev » 2.0.x-dev

Committed #21 to 8.x-1.x, changing version to focus on the 2.0.x reroll.

bojanz’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new5.65 KB

Rerolled.

  • bojanz committed 38289a64 on 2.0.x
    Issue #2881391 by sickness29, Ruslan Piskarov, bojanz: The locality.name...
bojanz’s picture

Status: Needs review » Fixed

And committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.