Reuse map data from a related venue listing
- Thread started
- Last updated
What you’ll achieve
Copy venue coordinates into events or load them for event detail display.
My site
Use a related venue's public address and coordinates on an event listing. Choose whether to save those values or use them only to display the map.
Prepare a public venue and an event
Create the Venue and Event listing types, with a category for each. Use Venue only for locations whose address and coordinates may be shown publicly. Note its ID in Setup > Listing Types Manager. The examples use venue listing type ID 3; use your site's ID.
Create these listing custom fields in Setup > Custom Fields Manager:
| Label | Exact name | JReviews field type | Listing field group |
|---|---|---|---|
| Venue | jr_venue |
Related Listing | Event details |
| Address | jr_address |
Text | Location |
| Latitude | jr_latitude |
Decimal | Location |
| Longitude | jr_longitude |
Decimal | Location |
Create the Event details and Location listing field groups first, or reuse suitable existing listing groups. Select the group shown in the table in each field editor. In the Event listing type's Field Groups settings, associate both groups. Associate Location with Venue too. These are the same address and coordinate fields on both listing types.
For jr_venue, set Related Listings Form Filters > Listing type to Venue. Set Maximum options that can be selected to 1. Suggestions select existing Venue listings; do not create ordinary field options. Keep all four fields active, optional, and without dependencies. Give venue editors write access to Location, event submitters write access to all Event fields, and visitors read access to the displayed address and map data.
Publish a Venue listing named Lakefront Hall. Enter 600 E Grand Avenue, Chicago, IL in Address, 41.891700 in Latitude, and -87.607800 in Longitude. Coordinate values use a decimal point and can be negative. Publish an Event listing named Summer concert and select Lakefront Hall in its Venue field. If Lakefront Hall is listing 201, the event stores that listing ID in jr_venue. This ID is illustrative; select the venue record on your site.
Configure MapsPro to use jr_address, jr_latitude, and jr_longitude, and enable its map for Event. Enable Address output on the event detail page. With the save approach, leave the event's address and coordinates blank before saving. The callback supplies the three values above, and they remain stored on the event. With the display-only approach, the event's address and coordinates stay unchanged in storage while visitors see Lakefront Hall's address and map.
JReviews 6 Early Access
Add the PHP code to your overrides
Joomla. Add the PHP example to templates/jreviews_overrides/hooks.php.
WordPress. Add the PHP example to jreviews_overrides/hooks.php.
Start a new file with <?php followed by defined('MVC_FRAMEWORK') or die;. If the file exists, append the registration without repeating those lines. Keep the customization in overrides rather than editing JReviews core files.
Prepare the venue and event fields
Use the fields, field groups, and published Lakefront Hall venue prepared above. Replace listing type ID 3 in the helper with your public Venue listing type's ID. Install and enable the listing-detail map when using the display-only approach.
Load a published venue from the public venue listing type
Add this helper once. It accepts published venues in the configured public venue listing type and requires both coordinate fields. Keep every venue in that listing type suitable for public display. Publication alone does not make private address fields public.
function guide_public_event_venue(int $venueId)
{
if ($venueId < 1) {
return null;
}
$venue = \JReviews\App\Models\Listing::query()
->published()
->whereListingType(3)
->with(['field_data', 'listing_type.listing_field_groups.fields'])
->find($venueId);
if (!$venue || !is_numeric($venue->getFieldValue('jr_latitude')) || !is_numeric($venue->getFieldValue('jr_longitude'))) {
return null;
}
return $venue;
}
Choose whether to save the event coordinates
Enable Listing Form in Early Access for this option. Use this hook if event proximity searches need the venue coordinates stored on each event. It applies to frontend creates and updates. If an event has no approved venue, the submitted event data remains unchanged.
fwd_add_filter('jreviews:listing.data_before_save', function (array $data, $listing): array {
if (!fwd_request()->inFrontend()) {
return $data;
}
$venueIds = (array) ($data['fields']['jr_venue'] ?? []);
$venueId = (int) ($venueIds[0] ?? 0);
$venue = guide_public_event_venue($venueId);
if (!$venue) {
return $data;
}
foreach (['jr_address', 'jr_latitude', 'jr_longitude'] as $name) {
$field = $listing->getField($name);
if (!$field || fwd_auth()->user()->cannot('write', $field)) {
return $data;
}
}
foreach (['jr_address', 'jr_latitude', 'jr_longitude'] as $name) {
$data['fields'][$name] = $venue->getFieldValue($name, '');
}
return $data;
}, 20, 2);
Saving copies the current venue values. A later venue change does not update existing events until each event is saved again.
Or load the coordinates only for detail display
Enable Listing Detail Page in Early Access for this option. Use this hook instead when the event only needs a map on its detail page. It supplies the venue address and coordinates while displaying the event page. It does not save them on the event or enable searches for nearby events.
fwd_add_filter('jreviews:view_data:listings_detail', function (array $data): array {
$listing = $data['listing'] ?? null;
if (!$listing || !$listing->field_data) {
return $data;
}
$venueIds = (array) $listing->getFieldValue('jr_venue', []);
$venue = guide_public_event_venue((int) ($venueIds[0] ?? 0));
if (!$venue) {
return $data;
}
foreach (['jr_address', 'jr_latitude', 'jr_longitude'] as $name) {
$listing->field_data->setAttribute($name, $venue->getFieldValue($name, ''));
}
return $data;
}, 20, 1);
Choose one approach. Do not install both for the same workflow. A valid venue with coordinates makes its location available to MapsPro. A venue without coordinates and an event without a venue keep the event's original data. Zero coordinates are subject to MapsPro's normal coordinate checks.
Verify the map and stored values
Test an approved venue, a venue outside the public venue listing type, an unpublished venue, and a venue without coordinates. For the save approach, reopen the event and check the stored address and coordinates, then run the required proximity search. For display-only, inspect the map and confirm the event's stored coordinates did not change. Do not use this example for restricted venue location data without an additional authorization rule.
JReviews 5 and v6 Legacy
Use a related venue's address and coordinates for an event listing. Choose between saving the values on the event for proximity searches and showing the venue map only on the event detail page.
Add the code to your overrides
Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.
WordPress. Use jreviews_overrides/filters/filter_functions.php.
Start a new file with <?php followed by defined('MVC_FRAMEWORK') or die;. Add the examples within that PHP file without repeating its opening tag or guard.
Prepare the related venue field
Use the fields, associated field groups, and published Lakefront Hall venue prepared above. In your chosen callback, replace $publicVenueListingTypeId = 0 with your public Venue listing type's ID, such as 3 in this example. Keep the destination fields active on the event form and writable by the submitting groups. Keep them optional: normal form validation runs before the save callback copies the venue values. You may hide their visible controls with your theme, but do not remove them from the form or its active fields. Adapt all field names below to your configuration. The examples supply the selected venue's address and coordinates in the format MapsPro needs.
Store the venue data on the event
Choose this filter if events must have their own coordinates for proximity searches. It copies the venue address and coordinates during a frontend save.
Clickfwd\Hook\Filter::add('listing_save_pre', function($data, $params)
{
$venueId = (int) ($data['Field']['Listing']['jr_venue'][0] ?? 0);
$publicVenueListingTypeId = 0; // Set the dedicated public venue listing type ID.
if ($venueId <= 0 || $publicVenueListingTypeId <= 0) {
return $data;
}
$listingsRepo = new ListingsRepositoryComponent;
$controller = new \stdClass();
$listingsRepo->startup($controller);
$listingsRepo->getModel()->stopAfterFindModels = ['Media' => 1, 'Favorite' => 1, 'Community' => 1];
$venue = $listingsRepo
->published()
->whereListingTypeId($publicVenueListingTypeId)
->fields(['Field.jr_latitude AS `Geomaps.lat`',
'Field.jr_longitude AS `Geomaps.lon`',
'JreviewsCategory.marker_icon AS `Geomaps.marker_icon`'
])
->where('Listing.id = '. $venueId)->one();
if (isset($venue['Geomaps']['lat'], $venue['Geomaps']['lon'])) {
$data['Field']['Listing']['jr_address'] = $venue['Field']['pairs']['jr_address']['value'][0] ?? '';
$data['Field']['Listing']['jr_latitude'] = $venue['Geomaps']['lat'];
$data['Field']['Listing']['jr_longitude'] = $venue['Geomaps']['lon'];
}
return $data;
});
Load coordinates for detail display only
Use this alternative if the event only needs a map on its detail page. It shows the venue's address and map without saving those coordinates on the event. It does not enable event proximity searches.
Clickfwd\Hook\Filter::add('post_get_listing_detailpage_query', function($listing, $params)
{
$venueId = (int) ($listing['Field']['pairs']['jr_venue']['real_value'][0] ?? 0);
$publicVenueListingTypeId = 0; // Set the dedicated public venue listing type ID.
if ($venueId <= 0 || $publicVenueListingTypeId <= 0) {
return $listing;
}
$listingsRepo = new ListingsRepositoryComponent;
$controller = new \stdClass();
$listingsRepo->startup($controller);
$listingsRepo->getModel()->stopAfterFindModels = ['Media' => 1, 'Favorite' => 1, 'Community' => 1];
$venue = $listingsRepo
->published()
->whereListingTypeId($publicVenueListingTypeId)
->fields(['Field.jr_latitude AS `Geomaps.lat`',
'Field.jr_longitude AS `Geomaps.lon`',
'JreviewsCategory.marker_icon AS `Geomaps.marker_icon`'
])
->where('Listing.id = '. $venueId)->one();
if (isset($venue['Geomaps']['lat'], $venue['Geomaps']['lon'])) {
$listing['Geomaps'] = $venue['Geomaps'];
$listing['Field']['pairs']['jr_address'] = $venue['Field']['pairs']['jr_address'] ?? [];
$listing['Field']['pairs']['jr_latitude'] = $venue['Field']['pairs']['jr_latitude'];
$listing['Field']['pairs']['jr_longitude'] = $venue['Field']['pairs']['jr_longitude'];
}
return $listing;
});
Check the relationship and permissions
Choose one approach. Create a dedicated venue listing type containing only venues whose address and coordinates may be shown publicly to every visitor who can see an event. Set $publicVenueListingTypeId in your chosen callback to that listing type ID. Enter that ID directly in the code; do not take the public venue listing-type ID from a submitted form field. New published venues assigned to that listing type work without adding their IDs to the code. A value of 0 disables copying until you configure the ID.
The query accepts only published venues in your dedicated public venue listing type. Do not put restricted venues or private location data in that listing type: publication alone does not make every field public. If you cannot guarantee that every venue in the selected listing type has public location data, do not use this example until you have an appropriate approval or access check. The form's list of suggested venues is not an authorization check.
For the save approach, confirm that the venue has jr_address before copying it. Saving an event without a venue leaves its existing data unchanged. Changing the venue later does not update stored event coordinates until the event is saved again.
Test an event with a valid venue, an event without a venue, and a venue without coordinates. Confirm that the event shows the venue address and map. With display-only, change the venue address and coordinates, reload the event, and confirm that both change without rewriting the event. Check stored fields and any proximity search behavior required by your chosen approach.