You are here

private function AddressDisplayFormatter::prepareAddressDisplay in Address Display 8

Prepare render array with address components.

Parameters

array $item: Address values.

Return value

array Render array.

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

File

src/Plugin/Field/FieldFormatter/AddressDisplayFormatter.php, line 239

Class

AddressDisplayFormatter
Plugin implementation of the 'Address Display' formatter.

Namespace

Drupal\address_display\Plugin\Field\FieldFormatter

Code

private function prepareAddressDisplay(array $item) {
  $config = $this
    ->getSetting('address_display');
  $countries = $this->countryRepository
    ->getList();
  $elements = [];

  // Skip hidden or empty items.
  foreach ($config as $key => $config_item) {
    if (!$config_item['display'] || empty($item[$key])) {
      unset($config[$key]);
    }
  }
  if (empty($config)) {
    return [];
  }

  // The key of the last displayed item.
  $last_key = array_keys($config)[count($config) - 1];
  foreach ($config as $key => $config_item) {
    if ($key == 'country_code') {
      $item[$key] = $countries[$item[$key]];
    }

    // Don't display the 'glue' separator for the last item.
    if ($key == $last_key) {
      $config_item['glue'] = '';
    }
    $elements[$key] = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#attributes' => [
        'class' => [
          'address-display-element',
          str_replace('_', '-', $key) . '-element',
        ],
      ],
      '#value' => $item[$key] . $config_item['glue'],
    ];
  }
  return $elements;
}