View source
<?php
namespace Drupal\google_map_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
class GoogleMapFieldOpenLayersFormatter extends FormatterBase {
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',
];
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;
}
private function allowedTags() {
return [
'div',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'p',
'span',
'br',
'em',
'strong',
'a',
'img',
'video',
'ul',
'ol',
'li',
];
}
}