From 1b2ce0b07566d7c3bf427aa860f4130fe1433488 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Sat, 20 Jan 2024 09:58:13 -0500 Subject: [PATCH] Add a locations map behavior and refactor dashboard map logic to use it. --- .../MapRenderEventSubscriber.php | 8 ++-- .../MapRenderEventSubscriber.php | 8 ++-- .../farm_map.map_behavior.locations.yml | 11 ++++++ .../install/farm_map.map_type.dashboard.yml | 3 +- .../core/ui/map/farm_ui_map.post_update.php | 39 +++++++++++++++++++ .../MapRenderEventSubscriber.php | 10 ++--- 6 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 modules/core/ui/map/config/install/farm_map.map_behavior.locations.yml create mode 100644 modules/core/ui/map/farm_ui_map.post_update.php diff --git a/modules/asset/land/src/EventSubscriber/MapRenderEventSubscriber.php b/modules/asset/land/src/EventSubscriber/MapRenderEventSubscriber.php index 536b9083f..82065c94e 100644 --- a/modules/asset/land/src/EventSubscriber/MapRenderEventSubscriber.php +++ b/modules/asset/land/src/EventSubscriber/MapRenderEventSubscriber.php @@ -60,11 +60,9 @@ class MapRenderEventSubscriber implements EventSubscriberInterface { */ public function onMapRender(MapRenderEvent $event) { - // Get the map ID. - $map_id = $event->getmapType()->id(); - - // Add land type layers to dashboard map. - if ($map_id == 'dashboard') { + // If the "locations" behavior is added to the map, add layers for each + // land type. + if (in_array('locations', $event->getMapBehaviors())) { $layers = []; // Define the parent group. diff --git a/modules/asset/structure/src/EventSubscriber/MapRenderEventSubscriber.php b/modules/asset/structure/src/EventSubscriber/MapRenderEventSubscriber.php index d0a776ba2..09665253f 100644 --- a/modules/asset/structure/src/EventSubscriber/MapRenderEventSubscriber.php +++ b/modules/asset/structure/src/EventSubscriber/MapRenderEventSubscriber.php @@ -60,11 +60,9 @@ class MapRenderEventSubscriber implements EventSubscriberInterface { */ public function onMapRender(MapRenderEvent $event) { - // Get the map ID. - $map_id = $event->getmapType()->id(); - - // Add structure type layers to dashboard map. - if ($map_id == 'dashboard') { + // If the "locations" behavior is added to the map, add layers for each + // structure type. + if (in_array('locations', $event->getMapBehaviors())) { $layers = []; // Define the parent group. diff --git a/modules/core/ui/map/config/install/farm_map.map_behavior.locations.yml b/modules/core/ui/map/config/install/farm_map.map_behavior.locations.yml new file mode 100644 index 000000000..6911befd6 --- /dev/null +++ b/modules/core/ui/map/config/install/farm_map.map_behavior.locations.yml @@ -0,0 +1,11 @@ +langcode: en +status: true +dependencies: + enforced: + module: + - farm_ui_map +id: locations +label: Location asset layers +description: 'Displays location asset geometries in layers by asset type.' +library: '' +settings: { } diff --git a/modules/core/ui/map/config/install/farm_map.map_type.dashboard.yml b/modules/core/ui/map/config/install/farm_map.map_type.dashboard.yml index 4348b58ba..46a88d062 100644 --- a/modules/core/ui/map/config/install/farm_map.map_type.dashboard.yml +++ b/modules/core/ui/map/config/install/farm_map.map_type.dashboard.yml @@ -7,5 +7,6 @@ dependencies: id: dashboard label: Dashboard description: 'The farmOS dashboard map.' -behaviors: { } +behaviors: + - locations options: { } diff --git a/modules/core/ui/map/farm_ui_map.post_update.php b/modules/core/ui/map/farm_ui_map.post_update.php new file mode 100644 index 000000000..a8f4fed5c --- /dev/null +++ b/modules/core/ui/map/farm_ui_map.post_update.php @@ -0,0 +1,39 @@ + 'locations', + 'label' => 'Location asset layers', + 'description' => 'Displays location asset geometries in layers by asset type.', + 'library' => '', + 'settings' => [], + 'dependencies' => [ + 'enforced' => [ + 'module' => [ + 'farm_ui_map', + ], + ], + ], + ]); + $behavior->save(); + + // Add the locations behavior to the dashboard map type. + $dashboard = MapType::load('dashboard'); + $behaviors = $dashboard->getMapBehaviors(); + $behaviors[] = 'locations'; + $dashboard->set('behaviors', $behaviors); + $dashboard->save(); +} diff --git a/modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php b/modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php index ec070e7f7..0bb03d42c 100644 --- a/modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php +++ b/modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php @@ -61,11 +61,8 @@ class MapRenderEventSubscriber implements EventSubscriberInterface { */ public function onMapRender(MapRenderEvent $event) { - // Get the map ID. - $map_id = $event->getmapType()->id(); - // Add behaviors/settings to default and geofield maps. - if (in_array($map_id, ['default', 'geofield'])) { + if (in_array($event->getmapType()->id(), ['default', 'geofield'])) { // Add "All locations" layers. $event->addBehavior('asset_type_layers'); @@ -86,8 +83,9 @@ class MapRenderEventSubscriber implements EventSubscriberInterface { } } - // Add asset layers to dashboard map. - elseif ($map_id == 'dashboard') { + // If the "locations" behavior is added to the map, add layers for each + // location asset type. + if (in_array('locations', $event->getMapBehaviors())) { $layers = [];