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
- Configure a Leaflet-formatted field's display settings with
zoomset, but leaveminZoom/maxZoomunset (e.g. save a field formatter config that predates those settings being introduced, or any formatter settings array where onlyzoomwas ever populated). - View any entity that renders that field (e.g. an Event node using the
defaultview mode'sfield_map_locationformatter). - Observe the two "Undefined array key" warnings, and note that
map_position.minZoom/map_position.maxZoomare0rather 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.jsonchange. - Confirm
composer installre-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/leafletproject 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).
| Comment | File | Size | Author |
|---|---|---|---|
| leaflet_fix_undefined_zoom_settings.patch | 1.13 KB | rmontero |
Issue fork leaflet-3613948
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
Comment #2
rmontero commentedComment #4
itamair commentedthx @rmontero ...
Comment #6
derekthatcher commentedFor 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);
}
Comment #8
hswong3i commentedComment #10
hswong3i commented#6 suggestion fix my rendering issue, create as MR https://git.drupalcode.org/project/leaflet/-/merge_requests/81
Comment #12
itamair commentedfrom 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...