Leaflet map formatter throws "Undefined array key" warnings for minZoom/maxZoom on event pages

Problem/Motivation

Event nodes (and any other content using the Leaflet map formatter) throw PHP warnings on render:

Warning: Undefined array key "minZoom" in Drupal\leaflet\Plugin\Field\FieldFormatter\LeafletDefaultFormatter->setExistingZoomSettings() (line 530 of modules/contrib/leaflet/src/Plugin/Field/FieldFormatter/LeafletDefaultFormatter.php).
Warning: Undefined array key "maxZoom" in Drupal\leaflet\Plugin\Field\FieldFormatter\LeafletDefaultFormatter->setExistingZoomSettings() (line 531 of modules/contrib/leaflet/src/Plugin/Field/FieldFormatter/LeafletDefaultFormatter.php).

setExistingZoomSettings() computes zoom bounds with:

$settings['map_position']['minZoom'] = (int) $settings['minZoom'] ?? 3;
$settings['map_position']['maxZoom'] = (int) $settings['maxZoom'] ?? 16;

(int) binds tighter than ??, so this actually evaluates as ((int) $settings['minZoom']) ?? 3. When a field formatter's settings define zoom but not minZoom/maxZoom (a perfectly valid, common configuration — e.g. this site's Event field_map_location display only sets zoom: 12), PHP throws the "Undefined array key" warning on the array access, then casts the resulting NULL to 0 — which is never falsy enough for ?? to fall through to its fallback.

So this bug is two problems in one: a noisy warning on every render, and a silent functional regression where minZoom/maxZoom resolve to 0 instead of the intended defaults (3/16).

Steps to reproduce

  1. Configure a Leaflet-formatted field's display settings with zoom set, but leave minZoom/maxZoom unset (e.g. save a field formatter config that predates those settings being introduced, or any formatter settings array where only zoom was ever populated).
  2. View any entity that renders that field (e.g. an Event node using the default view mode's field_map_location formatter).
  3. Observe the two "Undefined array key" warnings, and note that map_position.minZoom/map_position.maxZoom are 0 rather than the documented defaults of 3 and 16.

Minimal reproduction outside Drupal:

$settings = ['zoom' => 12];
$minZoom = (int) $settings['minZoom'] ?? 3; // warning; $minZoom === 0, not 3

Proposed resolution

Parenthesize the null-coalesce so it runs before the cast, per PHP operator precedence:

$settings['map_position']['zoom'] = (int) ($settings['zoom'] ?? 10);
$settings['map_position']['minZoom'] = (int) ($settings['minZoom'] ?? 3);
$settings['map_position']['maxZoom'] = (int) ($settings['maxZoom'] ?? 16);

This removes the warning and restores the intended fallback behavior. Applied locally as patches/leaflet_fix_undefined_zoom_settings.patch, registered against drupal/leaflet in composer.json.

Remaining tasks

  • Commit the patch file and composer.json change.
  • Confirm composer install re-applies the patch cleanly in CI/build.
  • Deploy and verify the warnings are gone on the Event default view mode in a live environment.
  • Optionally report/submit this fix upstream to the drupal/leaflet project on drupal.org so it doesn't need to be carried as a local patch indefinitely.

User interface changes

None. The PHP warnings were not user-facing on this site (system.logging:error_level is set to hide), so no visible UI change — this is a backend correctness/log-noise fix. Sites with error_level set to display errors on screen would no longer see the warning text appear on map-formatted field renders.

API changes

None. The fix only corrects the internal computation inside setExistingZoomSettings(); no public method signatures, hooks, or plugin interfaces are affected.

Data model changes

None. No field storage, configuration schema, or entity structure is altered. Existing field formatter settings (with or without minZoom/maxZoom keys) continue to be read as-is; only the resolved in-memory default now matches the documented fallback (3/16 instead of 0).

Issue fork leaflet-3613948

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

rmontero created an issue. See original summary.

rmontero’s picture

Issue summary: View changes

  • itamair committed 5c8d2a72 on 10.4.x
    fix: #3613948 Leaflet map formatter throws "Undefined array key"...
itamair’s picture

Status: Active » Fixed

thx @rmontero ...

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.

derekthatcher’s picture

For me this fix has prevented reading field display settings.

I think we need to check for existing nested values in $settings['map_position'] before checking for root keys or falling back to defaults.

changing to this resolves for me:

protected function setExistingZoomSettings(): void {
$settings = $this->getSettings();
$settings['map_position']['zoom'] = (int) ($settings['map_position']['zoom'] ?? $settings['zoom'] ?? 10);
$settings['map_position']['minZoom'] = (int) ($settings['map_position']['minZoom'] ?? $settings['minZoom'] ?? 3);
$settings['map_position']['maxZoom'] = (int) ($settings['map_position']['maxZoom'] ?? $settings['maxZoom'] ?? 16);
$this->setSettings($settings);
}

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

hswong3i’s picture

Status: Fixed » Needs review

hswong3i’s picture

#6 suggestion fix my rendering issue, create as MR https://git.drupalcode.org/project/leaflet/-/merge_requests/81

  • itamair committed 84db3db5 on 10.4.x
    LeafletDefaultFormatter:
    setExistingZoomSettings() removed, as related...
itamair’s picture

Status: Needs review » Fixed

from better insights and analysis the code related to this issue (method setExistingZoomSettings() in the LeafletDefaultFormatter formatter)
relates to very old use case and code branch, to tmp preserve formatter settings zoom backword compatibility.

The setExistingZoomSettings() in the LeafletDefaultFormatter formatter is definitely not needed anymore and has been removed in this last commit: https://git.drupalcode.org/project/leaflet/-/commit/84db3db5cdc57915d07e...

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.