Hiding Country from an Address Field’s Output in Drupal 8
There are two field formatters available on an Address field in Drupal 8:
The Plain formatter uses a Twig template file but it would take some work to override with the proper markup. The Default formatter doesn’t use a Twig template file so you cannot simply override via Twig.
If your goal is to simply hide theĀ country value from the output you can use hook_preprocess_field to alter the country value. As it turns out, unsetting the country field doesn’t help, but setting the value to an empty string does work.
Here’s an example (in a module; you can put in your .theme file if you’d prefer the setting to be at the theme level).
1 2 3 4 5 6 7 8 |
/** * Implements hook_preprocess_HOOK(). */ function mymodule_preprocess_field(&$vars) { if ($vars['field_name'] == 'field_person_address') { $vars['items'][0]['content']['country']['#value'] = ''; } } |