I need to pull the administrative area component from an Address field and place the full text of it it in the template of another field on the content type. I’ve managed to accomplish this with the following code.
In preprocess:
function MYTHEME_preprocess_page_title(&$variables) { $node = Drupal::request()->attributes->get('node'); $variables['content_type'] = $node->bundle(); if ($node->bundle() == 'location') { $variables['address_state'] = $node->get('field_address')->getValue()[0]['administrative_area']; } }
And in the twig template:
{% if content_type == 'location' %} <h1{{ title_attributes }}>{% spaceless %}{{ title }}{% endspaceless %}{% spaceless %},{% endspaceless %} {{ address_state }}</h1> {% endif %}
This will produce, for example “Boston, MA”. But what I want is “Boston, Massachusetts”, using the full name of the administrative area rather than the code. According to Drupal Commerce Address docs, you can get the full name in a twig template using {{ administrative_area.name }}
. But I couldn’t get that to work. How can I render the full administrative area name in the template?