Hello,
I would have custom code that changes $settings['mapId'] in the hook_leaflet_map_prebuild_alter function. Since I am changing this map id, I would like this change to be reflected in the $build['#attributes]['id'], so the map will be rendered fine.

My use case is the following

I am rendering the full map in an ajax form element. Submit button and other elements trigger some ajax commands that build the map again (rendering the map again). Since the leaflet_build_map uses drupal_html_id function, every call to the form the "leaflet-map" id changes to "leaflet-map-1" (which is good for most cases).
I have written a function like:

function my_module_leaflet_map_prebuild_alter(&$settings) {
  $settings['mapId'] = 'leaflet-map';
}

But $build['#attributes]['id'] has been already set (at the beggining of the leaflet_build_map function) and it is never updated again, so the map doesn´t show up (because the id is leaflet-map--2, or something like that).

I've just added a patch for it, that changes from this:

  $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',
  );

to this:

  $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',
  );

  // Update $build attributes id with any mapId change (could be useful for ajax form calls)
  $build['#attributes']['id'] = $settings['mapId'];

I don't really know if this is the way to go (in form api and leaflet api), but some feedback here would be great.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TuWebO’s picture