You are here

public function GeofieldGoogleStaticMapFormatter::viewElements in Geofield Map 8.2

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 FormatterInterface::viewElements

File

modules/geofield_map_extras/src/Plugin/Field/FieldFormatter/GeofieldGoogleStaticMapFormatter.php, line 292

Class

GeofieldGoogleStaticMapFormatter
Plugin implementation of the 'geofield_static_google_map' formatter.

Namespace

Drupal\geofield_map_extras\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $locations = [];
  $settings = $this
    ->getSettings();
  $language = $langcode !== Language::LANGCODE_NOT_SPECIFIED ? $langcode : 'en';
  foreach ($items as $item) {
    if ($item
      ->isEmpty()) {
      continue;
    }
    $value = $item
      ->getValue();
    if ($value['geo_type'] !== 'Point') {
      continue;
    }
    $locations[] = urlencode($value['latlon']);
  }
  $elements = [];

  // Return a single item.
  $elements[0] = [
    '#theme' => 'geofield_static_google_map',
    '#width' => $settings['width'],
    '#height' => $settings['height'],
    '#scale' => $settings['scale'],
    '#locations' => $locations,
    '#zoom' => $settings['zoom'],
    '#langcode' => $language,
    '#static_map_type' => $settings['static_map_type'],
    '#apikey' => (string) $this
      ->getGmapApiKey(),
    '#marker_color' => $settings['marker_color'],
    '#marker_size' => $settings['marker_size'],
  ];
  return $elements;
}