public function YamapsDefaultFormatter::viewElements in Yandex.Maps 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/ YamapsDefaultFormatter.php, line 26
Class
- YamapsDefaultFormatter
- Plugin implementation of the 'yamaps' formatter.
Namespace
Drupal\yamaps\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$map_behaviors = [];
if ($this
->getSetting('enable_zoom')) {
$map_behaviors[] = 'scrollZoom';
$map_behaviors[] = 'dblClickZoom';
}
if ($this
->getSetting('enable_drag')) {
$map_behaviors[] = 'drag';
}
foreach ($items as $delta => $item) {
$map_id = Html::getUniqueId($items
->getName() . '-' . $delta);
// Map information.
$default_js = yamaps_format_values_to_js($item);
// Map initialization parameters.
$map = [
'init' => [
'center' => $default_js['coords']['center'] ?? NULL,
'zoom' => $default_js['coords']['zoom'] ?? NULL,
'type' => 'yandex#map',
'behaviors' => $map_behaviors,
],
'display_options' => [
'display_type' => 'map',
'width' => $this
->getSetting('width'),
'height' => $this
->getSetting('height'),
],
'controls' => 1,
'placemarks' => $this
->getFieldSetting('enable_placemarks') ? $default_js['placemarks'] : [],
'lines' => $this
->getFieldSetting('enable_lines') ? $default_js['lines'] : [],
'polygons' => $this
->getFieldSetting('enable_polygons') ? $default_js['polygons'] : [],
'edit' => FALSE,
];
$element = [
'#theme' => 'yamaps_field_formatter',
'#width' => $this
->getSetting('width'),
'#height' => $this
->getSetting('height'),
'#map_id' => $map_id,
];
$elements['#attached']['drupalSettings']['yamaps'][$map_id] = $map;
$elements[$delta] = $element;
}
$elements['#attached']['library'][] = 'yamaps/yandex-map-api';
if ($this
->getFieldSetting('enable_placemarks')) {
$elements['#attached']['library'][] = 'yamaps/yamaps-placemark';
}
if ($this
->getFieldSetting('enable_lines')) {
$elements['#attached']['library'][] = 'yamaps/yamaps-line';
}
if ($this
->getFieldSetting('enable_polygons')) {
$elements['#attached']['library'][] = 'yamaps/yamaps-polygon';
}
$elements['#attached']['library'][] = 'yamaps/yamaps-map';
return $elements;
}