You are here

protected function AddressDefaultFormatter::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 AddressDefaultFormatter::viewElement()
AddressDefaultFormatter::viewElements in src/Plugin/Field/FieldFormatter/AddressDefaultFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/AddressDefaultFormatter.php, line 143

Class

AddressDefaultFormatter
Plugin implementation of the 'address_default' 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 = [
    '#address_format' => $address_format,
    '#locale' => $address
      ->getLocale(),
  ];
  $element['country'] = [
    '#type' => 'html_tag',
    '#tag' => 'span',
    '#attributes' => [
      'class' => [
        'country',
      ],
    ],
    '#value' => Html::escape($countries[$country_code]),
    '#placeholder' => '%country',
  ];
  foreach ($address_format
    ->getUsedFields() as $field) {
    $property = FieldHelper::getPropertyName($field);
    $class = str_replace('_', '-', $property);
    $element[$property] = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#attributes' => [
        'class' => [
          $class,
        ],
      ],
      '#value' => Html::escape($values[$field]),
      '#placeholder' => '%' . $field,
    ];
  }
  return $element;
}