You are here

public function GoogleMapFieldDefaultFormatter::viewElements in Google Map Field 8

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/GoogleMapFieldDefaultFormatter.php, line 24

Class

GoogleMapFieldDefaultFormatter
Plugin implementation of the 'google_map_field' formatter.

Namespace

Drupal\google_map_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $element = [
      '#theme' => 'google_map_field',
      '#name' => $item->name,
      '#lat' => $item->lat,
      '#lon' => $item->lon,
      '#zoom' => $item->zoom,
      '#type' => $item->type,
      '#show_marker' => $item->marker === "1" ? "true" : "false",
      '#marker_icon' => $item->marker_icon,
      '#traffic' => $item->traffic === "1" ? "true" : "false",
      '#show_controls' => $item->controls === "1" ? "true" : "false",
      '#width' => $item->width ? $item->width : '320px',
      '#height' => $item->height ? $item->height : '200px',
    ];

    // Handle markup for InfoWindow popup.
    if (!empty($item->infowindow)) {
      $element['#infowindow'] = [
        '#markup' => $item->infowindow,
        '#allowed_tags' => $this
          ->allowedTags(),
      ];
    }
    $element['#attached']['library'][] = 'google_map_field/google-map-field-renderer';
    $element['#attached']['library'][] = 'google_map_field/google-map-apis';
    $elements[$delta] = $element;
  }
  return $elements;
}