You are here

function leaflet_build_map in Leaflet 7

Attach Leaflet-required client files and return renderable array for a map.

Parameters

array $map: Map definition as returned my leaflet_map_get_info();

array $features: Associative array of map features.

string $height: The height of the map.

Return value

array A renderable array.

3 calls to leaflet_build_map()
leaflet_field_formatter_view in ./leaflet.formatters.inc
Implements hook_field_formatter_view().
leaflet_render_map in ./leaflet.module
DEPRECATED. Use leaflet_build_map() instead.
leaflet_views_plugin_style::render in leaflet_views/leaflet_views_plugin_style.inc
Renders view.

File

./leaflet.module, line 115

Code

function leaflet_build_map($map, $features = array(), $height = '400px') {

  // [#2777321] With two maps on 1 page, drupal_html_id() sometimes fails to
  // return a unique id. Make it so.
  $map_id = drupal_html_id('leaflet_map') . '-' . rand();
  $build = array(
    '#theme' => 'html_tag',
    '#tag' => 'div',
    '#value' => '',
    '#attributes' => array(
      'id' => $map_id,
      'style' => 'height: ' . $height,
    ),
  );

  // Allow map definitions to provide a default icon:
  if (isset($map['icon']['iconUrl'])) {
    foreach ($features as &$feature) {
      if (!isset($feature['icon'])) {
        $feature['icon'] = $map['icon'];
      }
    }
  }
  $settings = array(
    'mapId' => $map_id,
    'map' => $map,
    'features' => $features,
  );
  drupal_alter('leaflet_map_prebuild', $settings);
  $build['#attached']['js'][] = array(
    'data' => array(
      'leaflet' => array(
        $settings,
      ),
    ),
    'type' => 'setting',
  );
  $build['#attached']['css'][] = array(
    'data' => drupal_get_path('module', 'leaflet') . '/leaflet_extras.css',
  );

  // Load the leaflet library, which includes integration files.
  // libraries_load('leaflet');
  // See [2460643]
  $build['#attached']['libraries_load'][] = array(
    'leaflet',
  );

  // Let other modules properly attach libraries as well [#2567387]
  drupal_alter('leaflet_build_map', $build);
  return $build;
}