Problem/Motivation

The submodule leaflet_markercluster unconditionally overrides Drupal.Leaflet.prototype.add_features in modules/leaflet_markercluster/leaflet_markercluster.drupal.js. The Leaflet library attaches leaflet_markercluster/leaflet-markercluster-drupal per block whenever at least one map on the page has map_settings.leaflet_markercluster.control = true. Because the override lives on the prototype and has no per-instance guard, every Leaflet map on that page is routed through L.MarkerClusterGroup, ignoring each map's own leaflet_markercluster.control value. Result: clustering is either off on all maps or on for all maps — it cannot be configured per block/view/formatter.

Steps to reproduce

1. Create a page (or node) that renders two Leaflet maps/blocks (e.g. two leaflet_map view blocks or two node fields using the Leaflet formatter).
2. In block/formatter A) enable "Marker Cluster" (leaflet_markercluster.control = true) with e.g. disableClusteringAtZoom: 8.
3. In block/formatter B) disable "Marker Cluster" (leaflet_markercluster.control = false).
4. Verify drupalSettings.leaflet contains both maps with the expected control values (true / false).
5. Load the page. Expected: map A clusters, map B renders individual markers. Actual: both maps cluster. If A is false and B is true, both cluster as well. Only when both are false (library not attached) does clustering stay off on both.

Proposed resolution

Keep a reference to the original add_features before overriding, and inside the new implementation delegate back to the original when the current map instance does not have clustering enabled:

(function ($, Drupal) {
  var original_add_features = Drupal.Leaflet.prototype.add_features;

  Drupal.Leaflet.prototype.add_features = function (features, initial) {
    if (!this.map_settings
      || !this.map_settings.leaflet_markercluster
      || !this.map_settings.leaflet_markercluster.control) {
      return original_add_features.call(this, features, initial);
    }
    // existing clustering implementation ...
  };
})(jQuery, Drupal);

This makes clustering opt-in per map instance without changing the public API.

Remaining tasks

* Review and commit the JS fix in leaflet_markercluster.drupal.js.

User interface changes

None.

API changes

None. The signature of Drupal.Leaflet.prototype.add_features(features, initial) is unchanged. Only behaviour changes: when map_settings.leaflet_markercluster.control is falsy for a given instance, the base leaflet implementation is used instead of the clustering one.

Data model changes

None. No schema, config schema, or stored settings are changed. Existing leaflet_markercluster settings (control, options, excluded, include_path) are honoured as documented.

Issue fork leaflet-3621802

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

internetter created an issue. See original summary.

internetter’s picture

Status: Active » Needs review
Issue tags: +markercluster

I added the needed code changes as MR.

itamair made their first commit to this issue’s fork.

itamair’s picture

Status: Needs review » Fixed

thanks @internetter, nice catch!
I tested it and you solution looks effective and neat.
I just made a tiny adjustment (Use const for original add_features reference).
Merging into 10.4.x branch, will be part of the next incoming Leaflet module release.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.