You are here

protected function AddressPlainFormatter::viewElement in Address 8

Builds a renderable array for a single address item.

Parameters

\Drupal\address\AddressInterface $address: The address.

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

Return value

array A renderable array.

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

File

src/Plugin/Field/FieldFormatter/AddressPlainFormatter.php, line 127

Class

AddressPlainFormatter
Plugin implementation of the 'address_plain' formatter.

Namespace

Drupal\address\Plugin\Field\FieldFormatter

Code

protected function viewElement(AddressInterface $address, $langcode) {
  $country_code = $address
    ->getCountryCode();
  $countries = $this->countryRepository
    ->getList();
  $address_format = $this->addressFormatRepository
    ->get($country_code);
  $values = $this
    ->getValues($address, $address_format);
  $element = [
    '#theme' => 'address_plain',
    '#given_name' => $values['givenName'],
    '#additional_name' => $values['additionalName'],
    '#family_name' => $values['familyName'],
    '#organization' => $values['organization'],
    '#address_line1' => $values['addressLine1'],
    '#address_line2' => $values['addressLine2'],
    '#postal_code' => $values['postalCode'],
    '#sorting_code' => $values['sortingCode'],
    '#administrative_area' => $values['administrativeArea'],
    '#locality' => $values['locality'],
    '#dependent_locality' => $values['dependentLocality'],
    '#country' => [
      'code' => $country_code,
      'name' => $countries[$country_code],
    ],
    '#address' => $address,
    '#view_mode' => $this->viewMode,
    '#cache' => [
      'contexts' => [
        'languages:' . LanguageInterface::TYPE_INTERFACE,
      ],
    ],
  ];
  return $element;
}