1

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?

0

How I understand the linked doc you need to configure the plain address formatter in a view mode to use address-plain.html.twig. Then you can output the node field as formatted in this twig:

   if ($node->bundle() == 'location') {
     $variables['address_state'] = $node->get('field_address')->view('custom_view_mode');
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.