You are here

public function GeoPhpGeocodeFormatter::viewElements in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 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 198

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();
    if (array_key_exists($adapter, $adapters)) {
      foreach ($items as $delta => $item) {

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