You are here

public function LeafletService::leafletRenderMap in Leaflet 8

Same name and namespace in other branches
  1. 2.1.x src/LeafletService.php \Drupal\leaflet\LeafletService::leafletRenderMap()
  2. 2.0.x src/LeafletService.php \Drupal\leaflet\LeafletService::leafletRenderMap()

Load all Leaflet required client files and return markup for a map.

Parameters

array $map: The map settings array.

array $features: The features array.

string $height: The height value string.

Return value

array The leaflet_map render array.

File

src/LeafletService.php, line 84

Class

LeafletService
Provides a LeafletService class.

Namespace

Drupal\leaflet

Code

public function leafletRenderMap(array $map, array $features = [], $height = '400px') {
  $map_id = isset($map['id']) ? $map['id'] : Html::getUniqueId('leaflet_map');
  $attached_libraries = [
    'leaflet/general',
    'leaflet/leaflet-drupal',
  ];

  // Add the Leaflet Fullscreen library, if requested.
  if (!empty($map['settings']['fullscreen_control'])) {
    $attached_libraries[] = 'leaflet/leaflet.fullscreen';
  }

  // Add the Leaflet Gesture Handling library, if requested.
  if (!empty($map['settings']['gestureHandling'])) {
    $attached_libraries[] = 'leaflet/leaflet.gesture_handling';
  }

  // Add the Leaflet Markecluster library and functionalities, if requested.
  if ($this->moduleHandler
    ->moduleExists('leaflet_markercluster') && isset($map['settings']['leaflet_markercluster']) && $map['settings']['leaflet_markercluster']['control']) {
    $attached_libraries[] = 'leaflet_markercluster/leaflet-markercluster';
    $attached_libraries[] = 'leaflet_markercluster/leaflet-markercluster-drupal';
  }

  // Add the Leaflet Geocoder library and functionalities, if requested,
  // and the user has access to Geocoder Api Enpoints.
  if (!empty($map['settings']['geocoder']['control'])) {
    $this
      ->setGeocoderControlSettings($map['settings']['geocoder'], $attached_libraries);
  }
  $settings[$map_id] = [
    'mapid' => $map_id,
    'map' => $map,
    // JS only works with arrays, make sure we have one with numeric keys.
    'features' => array_values($features),
  ];
  return [
    '#theme' => 'leaflet_map',
    '#map_id' => $map_id,
    '#height' => $height,
    '#map' => $map,
    '#attached' => [
      'library' => $attached_libraries,
      'drupalSettings' => [
        'leaflet' => $settings,
      ],
    ],
  ];
}