You are here

public function GeoPhpGeocodeFormatter::viewElements in Geocoder 8.3

Same name and namespace in other branches
  1. 8.2 modules/geocoder_geofield/src/Plugin/Field/FieldFormatter/GeoPhpGeocodeFormatter.php \Drupal\geocoder_geofield\Plugin\Field\FieldFormatter\GeoPhpGeocodeFormatter::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 FileGeocodeFormatter::viewElements

File

modules/geocoder_geofield/src/Plugin/Field/FieldFormatter/GeoPhpGeocodeFormatter.php, line 182

Class

GeoPhpGeocodeFormatter
Abstract implementation of the GeoPhp Wrapper formatter for File fields.

Namespace

Drupal\geocoder_geofield\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $adapters = $this->geoPhpWrapper
    ->getAdapterMap();
  $adapter = $this
    ->getSetting('adapter');
  try {

    /* @var \Drupal\geocoder_field\PreprocessorInterface $preprocessor */
    $preprocessor = $this->preprocessorManager
      ->createInstance('file');
    $preprocessor
      ->setField($items)
      ->preprocess();
    $providers = $this
      ->getEnabledGeocoderProviders();
    if (array_key_exists($adapter, $adapters)) {
      foreach ($items as $delta => $item) {

        /* @var \Geometry $collection */
        if ($collection = $this->geocoder
          ->geocode($item->value, $providers)) {
          $elements[$delta] = [
            '#markup' => $collection
              ->out($adapter),
          ];
        }
      }
    }
  } catch (\Exception $e) {
    watchdog_exception('geocoder', $e);
  }
  return $elements;
}