Add a locations map behavior and refactor dashboard map logic to use it.

This commit is contained in:
Michael Stenta 2024-01-20 09:58:13 -05:00
parent 433747d0ff
commit 1b2ce0b075
6 changed files with 62 additions and 17 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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: { }

View File

@ -7,5 +7,6 @@ dependencies:
id: dashboard
label: Dashboard
description: 'The farmOS dashboard map.'
behaviors: { }
behaviors:
- locations
options: { }

View File

@ -0,0 +1,39 @@
<?php
/**
* @file
* Post update functions for farm_ui_map module.
*/
use Drupal\farm_map\Entity\MapBehavior;
use Drupal\farm_map\Entity\MapType;
/**
* Create farmOS-map locations behavior, add it to dashboard map.
*/
function farm_ui_map_post_update_locations_behavior(&$sandbox = NULL) {
// Create locations behavior.
$behavior = MapBehavior::create([
'id' => '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();
}

View File

@ -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 = [];