simplemap icon indicating copy to clipboard operation
simplemap copied to clipboard

Fixed an issue querying address field from non-primary site

Open NathanReul opened this issue 5 months ago • 0 comments

Problem

Location queries using the map field fail on non-primary sites in multi-site Craft CMS installations. This is due to a strict JOIN condition that matches elements_sites.siteId to ownerSiteId, preventing access to map data saved on different sites.

Root Cause

In MapService::modifyElementsQuery(), the JOIN condition includes:

[[elements_sites.siteId]] = [[alias.ownerSiteId]]  

This breaks location queries when:

  • Map data is saved on Site A (ownerSiteId = 1)
  • A query runs on Site B (siteId = 2)
    → No results are returned since 1 ≠ 2.

Solution

  • Removed the strict site ID match from the JOIN condition.
  • Preserved integrity by matching:
    • Element ID: [[elements.id]] = [[alias.ownerId]]
    • Field ID: [[alias.fieldId]] = field->id
    • Excludes deleted elements: `[[elements.dateDeleted]] IS NULL`

Files Changed

  • src/services/MapService.php: Modified the JOIN condition in modifyElementsQuery()

Impact

This fix eliminates the need for complex workarounds in multi-site templates and makes the plugin's location query functionality work as expected across all sites in a Craft CMS multi-site setup.

NathanReul avatar Jun 12 '25 13:06 NathanReul