You are here

public function SimpleGMapFormatter::viewElements in Simple Google Maps 3.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/SimpleGMapFormatter.php \Drupal\simple_gmap\Plugin\Field\FieldFormatter\SimpleGMapFormatter::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 FormatterInterface::viewElements

File

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

Class

SimpleGMapFormatter
Plugin implementation of the 'simple_gmap' formatter.

Namespace

Drupal\simple_gmap\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  $settings = $this
    ->getSettings();
  $include_text = $settings['include_text'];
  $zoom_level = (int) $settings['zoom_level'];

  // For some reason, static gmaps accepts a different value for map type.
  $static_map_types = [
    'm' => 'roadmap',
    'k' => 'satellite',
    'h' => 'hybrid',
    'p' => 'terrain',
  ];
  $map_type = $settings['map_type'];

  // Figure out a language code to use. Google cannot recognize 'und'.
  if ($settings['langcode'] == 'page') {
    $lang_to_use = $langcode;
  }
  else {
    $lang_to_use = [
      '#plain_text' => $settings['langcode'],
    ];
  }
  foreach ($items as $delta => $item) {
    $url_value = urlencode($item->value);
    $address_value = $item->value;
    $address = $include_text ? $address_value : '';
    $text_for_link = $settings['link_text'] == 'use_address' ? $address_value : $settings['link_text'];
    $link_text = [
      '#plain_text' => $text_for_link,
    ];
    $iframe_title = $settings['iframe_title'];
    $iframe_title_value = [
      '#plain_text' => str_replace('[address]', $address_value, $iframe_title),
    ];
    $element[$delta] = [
      '#theme' => 'simple_gmap_output',
      '#include_map' => $settings['include_map'],
      '#include_static_map' => $settings['include_static_map'],
      '#include_link' => $settings['include_link'],
      '#include_text' => $settings['include_text'],
      '#width' => [
        '#plain_text' => $settings['iframe_width'],
      ],
      '#height' => [
        '#plain_text' => $settings['iframe_height'],
      ],
      '#iframe_title' => $iframe_title_value,
      '#static_scale' => (int) $settings['static_scale'],
      '#url_suffix' => $url_value,
      '#zoom' => $zoom_level,
      '#link_text' => $link_text,
      '#address_text' => $address,
      '#map_type' => $map_type,
      '#langcode' => $lang_to_use,
      '#static_map_type' => $static_map_types[$map_type],
      '#apikey' => $settings['apikey'],
    ];
  }
  return $element;
}