You are here

protected function ZoneDefaultFormatter::viewElement in Address 8

Builds a renderable array for a single zone item.

Parameters

\CommerceGuys\Addressing\Zone\Zone $zone: The zone.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array.

1 call to ZoneDefaultFormatter::viewElement()
ZoneDefaultFormatter::viewElements in src/Plugin/Field/FieldFormatter/ZoneDefaultFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/ZoneDefaultFormatter.php, line 135

Class

ZoneDefaultFormatter
Plugin implementation of the 'address_zone_default' formatter.

Namespace

Drupal\address\Plugin\Field\FieldFormatter

Code

protected function viewElement(Zone $zone, $langcode) {
  $countries = $this->countryRepository
    ->getList();
  $element = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'zone',
      ],
    ],
  ];
  if ($label = $zone
    ->getLabel()) {
    $element['label'] = [
      '#type' => 'item',
      '#attributes' => [
        'class' => [
          'label',
        ],
      ],
      '#plain_text' => $label,
    ];
  }
  foreach ($zone
    ->getTerritories() as $index => $territory) {
    $country_code = $territory
      ->getCountryCode();
    $address_format = $this->addressFormatRepository
      ->get($country_code);
    $labels = LabelHelper::getFieldLabels($address_format);
    $element['territories'][$index] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'zone-territory',
        ],
      ],
    ];
    $element['territories'][$index]['country'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Country'),
      '#attributes' => [
        'class' => [
          'country',
        ],
      ],
      '#plain_text' => $countries[$country_code],
    ];
    if ($administrative_area = $territory
      ->getAdministrativeArea()) {
      $administrative_areas = $this->subdivisionRepository
        ->getList([
        $country_code,
      ]);
      $administrative_area_name = $administrative_area;
      if (isset($administrative_areas[$administrative_area])) {
        $administrative_area_name = $administrative_areas[$administrative_area];
      }
      $element['territories'][$index]['administrative_area'] = [
        '#type' => 'item',
        '#title' => $labels[AddressField::ADMINISTRATIVE_AREA],
        '#attributes' => [
          'class' => [
            'administrative-area',
          ],
        ],
        '#plain_text' => $administrative_area_name,
      ];
    }
    if ($locality = $territory
      ->getLocality()) {
      $localities = $this->subdivisionRepository
        ->getList([
        $country_code,
        $administrative_area,
      ]);
      $locality_name = $locality;
      if (isset($localities[$locality])) {
        $locality_name = $localities[$locality];
      }
      $element['territories'][$index]['locality'] = [
        '#type' => 'item',
        '#title' => $labels[AddressField::LOCALITY],
        '#attributes' => [
          'class' => [
            'locality',
          ],
        ],
        '#plain_text' => $locality_name,
      ];
    }
    if ($dependent_locality = $territory
      ->getDependentLocality()) {
      $dependent_localities = $this->subdivisionRepository
        ->getList([
        $country_code,
        $administrative_area,
        $locality,
      ]);
      $dependent_locality_name = $dependent_locality;
      if (isset($dependent_localities[$dependent_locality])) {
        $dependent_locality_name = $dependent_localities[$dependent_locality];
      }
      $element['territories'][$index]['dependent_locality'] = [
        '#type' => 'item',
        '#title' => $labels[AddressField::DEPENDENT_LOCALITY],
        '#attributes' => [
          'class' => [
            'dependent-locality',
          ],
        ],
        '#plain_text' => $dependent_locality_name,
      ];
    }
    if ($included_postal_codes = $territory
      ->getIncludedPostalCodes()) {
      $element['territories'][$index]['included_postal_codes'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Included postal codes'),
        '#attributes' => [
          'class' => [
            'included-postal-codes',
          ],
        ],
        '#plain_text' => $included_postal_codes,
      ];
    }
    if ($excluded_postal_codes = $territory
      ->getExcludedPostalCodes()) {
      $element['territories'][$index]['excluded_postal_codes'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Excluded postal codes'),
        '#attributes' => [
          'class' => [
            'excluded-postal-codes',
          ],
        ],
        '#plain_text' => $excluded_postal_codes,
      ];
    }
  }
  return $element;
}