You are here

public function GoogleMapFieldOpenLayersFormatter::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/GoogleMapFieldOpenLayersFormatter.php, line 24

Class

GoogleMapFieldOpenLayersFormatter
Plugin implementation of the 'google_map_field' OpenLayers formatter.

Namespace

Drupal\google_map_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $element = [
      '#theme' => 'olmap_field',
      '#name' => $item->name,
      '#lat' => $item->lat,
      '#lon' => $item->lon,
      '#zoom' => $item->zoom,
      '#show_marker' => $item->marker === "1" ? "true" : "false",
      '#show_controls' => $item->controls === "1" ? "true" : "false",
      '#width' => $item->width ? $item->width : '320px',
      '#height' => $item->height ? $item->height : '200px',
      '#default_marker' => '/' . drupal_get_path('module', 'google_map_field') . '/images/pin.png',
    ];

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