You are here

public function AddressGeocodeFormatter::viewElements in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 modules/geocoder_address/src/Plugin/Field/FieldFormatter/AddressGeocodeFormatter.php \Drupal\geocoder_address\Plugin\Field\FieldFormatter\AddressGeocodeFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

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

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides GeocodeFormatterBase::viewElements

File

modules/geocoder_address/src/Plugin/Field/FieldFormatter/AddressGeocodeFormatter.php, line 115

Class

AddressGeocodeFormatter
Plugin implementation of the Geocode formatter.

Namespace

Drupal\geocoder_address\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  try {
    $dumper = $this->dumperPluginManager
      ->createInstance($this
      ->getSetting('dumper'));
  } catch (PluginException $e) {
    watchdog_exception('Geocode Address Dumper', $e);
  }
  $provider_plugins = $this
    ->getEnabledProviderPlugins();
  foreach ($items as $delta => $item) {
    $value = $item
      ->getValue();
    $address_string = $this->addressService
      ->addressArrayToGeoString($value);
    if ($address_collection = $this->geocoder
      ->geocode($address_string, array_keys($provider_plugins))) {
      $elements[$delta] = [
        '#plain_text' => $dumper
          ->dump($address_collection
          ->first()),
      ];
    }
  }
  return $elements;
}