Show map on /locations #779

This commit is contained in:
Michael Stenta 2024-02-02 19:53:31 -05:00
commit 9058e39f7c
14 changed files with 139 additions and 21 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Add a Product asset type and Product type taxonomy #787](https://github.com/farmOS/farmOS/pull/787) - [Add a Product asset type and Product type taxonomy #787](https://github.com/farmOS/farmOS/pull/787)
- [Inventory quick form #766](https://github.com/farmOS/farmOS/pull/766) - [Inventory quick form #766](https://github.com/farmOS/farmOS/pull/766)
- [Add UI for creating instances of quick forms #785](https://github.com/farmOS/farmOS/pull/785) - [Add UI for creating instances of quick forms #785](https://github.com/farmOS/farmOS/pull/785)
- [Show map on /locations #779](https://github.com/farmOS/farmOS/pull/779)
### Changed ### Changed

View File

@ -60,11 +60,9 @@ class MapRenderEventSubscriber implements EventSubscriberInterface {
*/ */
public function onMapRender(MapRenderEvent $event) { public function onMapRender(MapRenderEvent $event) {
// Get the map ID. // If the "locations" behavior is added to the map, add layers for each
$map_id = $event->getmapType()->id(); // land type.
if (in_array('locations', $event->getMapBehaviors())) {
// Add land type layers to dashboard map.
if ($map_id == 'dashboard') {
$layers = []; $layers = [];
// Define the parent group. // Define the parent group.

View File

@ -60,11 +60,9 @@ class MapRenderEventSubscriber implements EventSubscriberInterface {
*/ */
public function onMapRender(MapRenderEvent $event) { public function onMapRender(MapRenderEvent $event) {
// Get the map ID. // If the "locations" behavior is added to the map, add layers for each
$map_id = $event->getmapType()->id(); // structure type.
if (in_array('locations', $event->getMapBehaviors())) {
// Add structure type layers to dashboard map.
if ($map_id == 'dashboard') {
$layers = []; $layers = [];
// Define the parent group. // Define the parent group.

View File

@ -72,6 +72,23 @@ class MapRenderEvent extends Event {
return $this->mapType; return $this->mapType;
} }
/**
* Getter method for map behaviors.
*
* This returns a merged list of map behaviors from both the map type
* configuration and the map element's #behaviors property.
*
* @return string[]
* An array of map behavior IDs.
*/
public function getMapBehaviors() {
$behaviors = $this->getMapType()->getMapBehaviors();
if (!empty($this->element['#behaviors'])) {
$behaviors = array_merge($behaviors, $this->element['#behaviors']);
}
return $behaviors;
}
/** /**
* Add behavior to the map. * Add behavior to the map.
* *
@ -92,8 +109,10 @@ class MapRenderEvent extends Event {
/** @var \Drupal\farm_map\Entity\MapBehaviorInterface $behavior */ /** @var \Drupal\farm_map\Entity\MapBehaviorInterface $behavior */
$behavior = $this->entityTypeManager->getStorage('map_behavior')->load($behavior_name); $behavior = $this->entityTypeManager->getStorage('map_behavior')->load($behavior_name);
// Attach the library. // If the behavior has a library, attach it.
$this->element['#attached']['library'][] = $behavior->getLibrary(); if (!empty($behavior->getLibrary())) {
$this->element['#attached']['library'][] = $behavior->getLibrary();
}
// Add behavior settings if supplied. // Add behavior settings if supplied.
if (!empty($settings)) { if (!empty($settings)) {

View File

@ -123,7 +123,7 @@ class MapBlock extends BlockBase implements ContainerFactoryPluginInterface {
return [ return [
'#type' => 'farm_map', '#type' => 'farm_map',
'#map_type' => $this->configuration['map_type'] ?? 'default', '#map_type' => $this->configuration['map_type'] ?? 'default',
'#behaviors' => array_keys($this->configuration['map_behaviors']) ?? [], '#behaviors' => $this->configuration['map_behaviors'] ?? [],
]; ];
} }

View File

@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_ui_location
id: locations
label: Locations
description: 'The farmOS locations map.'
behaviors:
- locations
options: { }

View File

@ -6,5 +6,6 @@ core_version_requirement: ^10
dependencies: dependencies:
- farm:farm_entity - farm:farm_entity
- farm:farm_location - farm:farm_location
- farm:farm_ui_map
- farm:farm_ui_menu - farm:farm_ui_menu
- inspire_tree:inspire_tree - inspire_tree:inspire_tree

View File

@ -0,0 +1,33 @@
<?php
/**
* @file
* Post update functions for farm_ui_location module.
*/
use Drupal\farm_map\Entity\MapType;
/**
* Add farmOS locations map type.
*/
function farm_ui_location_post_update_add_locations_map_type(&$sandbox = NULL) {
// Create locations map type.
$map_type = MapType::create([
'id' => 'locations',
'label' => 'Locations',
'description' => 'The farmOS locations map.',
'behaviors' => [
'location',
],
'options' => [],
'dependencies' => [
'enforced' => [
'module' => [
'farm_ui_location',
],
],
],
]);
$map_type->save();
}

View File

@ -112,6 +112,14 @@ class LocationHierarchyForm extends FormBase {
*/ */
public function buildForm(array $form, FormStateInterface $form_state, AssetInterface $asset = NULL) { public function buildForm(array $form, FormStateInterface $form_state, AssetInterface $asset = NULL) {
// If no asset was specified, show a map of all locations.
if (is_null($asset)) {
$form['map'] = [
'#type' => 'farm_map',
'#map_type' => 'locations',
];
}
// Add a DIV for the JavaScript content. // Add a DIV for the JavaScript content.
$form['content'] = [ $form['content'] = [
'#type' => 'html_tag', '#type' => 'html_tag',

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 id: dashboard
label: Dashboard label: Dashboard
description: 'The farmOS dashboard map.' description: 'The farmOS dashboard map.'
behaviors: { } behaviors:
- locations
options: { } options: { }

View File

@ -7,5 +7,4 @@ dependencies:
- farm:asset - farm:asset
- farm:farm_location - farm:farm_location
- farm:farm_map - farm:farm_map
- farm:farm_ui_dashboard
- farm:farm_ui_views - farm:farm_ui_views

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) { public function onMapRender(MapRenderEvent $event) {
// Get the map ID.
$map_id = $event->getmapType()->id();
// Add behaviors/settings to default and geofield maps. // 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. // Add "All locations" layers.
$event->addBehavior('asset_type_layers'); $event->addBehavior('asset_type_layers');
@ -86,8 +83,9 @@ class MapRenderEventSubscriber implements EventSubscriberInterface {
} }
} }
// Add asset layers to dashboard map. // If the "locations" behavior is added to the map, add layers for each
elseif ($map_id == 'dashboard') { // location asset type.
if (in_array('locations', $event->getMapBehaviors())) {
$layers = []; $layers = [];