mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Split map logic out of farm_ui_views to a new farm_ui_map module.
This commit is contained in:
parent
f57cebd68f
commit
746e189664
|
@ -5,6 +5,7 @@ package: farmOS UI
|
|||
core_version_requirement: ^9
|
||||
dependencies:
|
||||
- farm:farm_ui_action
|
||||
- farm:farm_ui_map
|
||||
- farm:farm_ui_menu
|
||||
- farm:farm_ui_theme
|
||||
- farm:farm_ui_user
|
||||
|
|
|
@ -3,7 +3,7 @@ status: true
|
|||
dependencies:
|
||||
enforced:
|
||||
module:
|
||||
- farm_ui_views
|
||||
- farm_ui_map
|
||||
id: asset_list
|
||||
label: Asset list
|
||||
description: 'The map displayed on lists of assets.'
|
9
modules/ui/map/farm_ui_map.info.yml
Normal file
9
modules/ui/map/farm_ui_map.info.yml
Normal file
|
@ -0,0 +1,9 @@
|
|||
name: farmOS UI Map
|
||||
description: Provides default maps of farmOS entities.
|
||||
type: module
|
||||
package: farmOS UI
|
||||
core_version_requirement: ^9
|
||||
dependencies:
|
||||
- farm:farm_location
|
||||
- farm:farm_map
|
||||
- farm:farm_ui_views
|
133
modules/ui/map/farm_ui_map.module
Normal file
133
modules/ui/map/farm_ui_map.module
Normal file
|
@ -0,0 +1,133 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* The farmOS UI Map module.
|
||||
*/
|
||||
|
||||
use Drupal\views\ViewExecutable;
|
||||
|
||||
/**
|
||||
* Implements hook_views_pre_render().
|
||||
*/
|
||||
function farm_ui_map_views_pre_render(ViewExecutable $view) {
|
||||
|
||||
// Render a map attachment above views of assets.
|
||||
if ($view->id() == 'farm_asset' && in_array($view->current_display, ['page', 'page_type'])) {
|
||||
|
||||
// Get all asset bundles.
|
||||
$asset_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($view->getBaseEntityType()->id());
|
||||
|
||||
// Start array of filtered bundles.
|
||||
$filtered_bundles = $asset_bundles;
|
||||
|
||||
// Start array of asset layers to add.
|
||||
$asset_layers = [
|
||||
'full' => [],
|
||||
'cluster' => [],
|
||||
];
|
||||
|
||||
// Save the translated cluster string.
|
||||
$cluster_string = ' (' . t('clusters') . ')';
|
||||
|
||||
// Save the group labels.
|
||||
$layer_group = $view->getBaseEntityType()->getCollectionLabel();
|
||||
$cluster_group = $layer_group . $cluster_string;
|
||||
|
||||
// Add multiple asset layers to the page of all assets.
|
||||
if ($view->current_display == 'page') {
|
||||
|
||||
// Limit to filtered asset types.
|
||||
if (!empty($exposed_filters['type'])) {
|
||||
$filtered_bundles = array_intersect_key($asset_bundles, array_flip($exposed_filters['type']));
|
||||
}
|
||||
}
|
||||
|
||||
// If this is the page_type display, only map this asset type.
|
||||
if ($view->current_display == 'page_type' && !empty($view->args[0])) {
|
||||
|
||||
// Don't use a separate group for cluster layers.
|
||||
$cluster_group = $layer_group;
|
||||
|
||||
$bundle = $view->args[0];
|
||||
$filtered_bundles = [
|
||||
$bundle => $asset_bundles[$bundle],
|
||||
];
|
||||
}
|
||||
|
||||
// Add an asset type map layer for each filtered bundle.
|
||||
foreach ($filtered_bundles as $bundle => $bundle_info) {
|
||||
|
||||
// Get exposed filters.
|
||||
$exposed_filters = $view->getExposedInput();
|
||||
|
||||
// Add a cluster layer for all asset types.
|
||||
$asset_layers['cluster']['cluster_' . $bundle] = [
|
||||
'group' => $cluster_group,
|
||||
'label' => $bundle_info['label'] . $cluster_string,
|
||||
'cluster' => TRUE,
|
||||
'asset_type' => $bundle,
|
||||
'filters' => $exposed_filters,
|
||||
// @todo Color each asset type cluster differently.
|
||||
];
|
||||
|
||||
// Create individual layers for land types and structure types.
|
||||
// @todo Abstract this to detect types other than land/structure?
|
||||
if (in_array($bundle, ['land', 'structure'])) {
|
||||
|
||||
// Get fields for the bundle.
|
||||
$entity_manager = \Drupal::service('entity_field.manager');
|
||||
$fields = $entity_manager->getFieldStorageDefinitions('asset', $bundle);
|
||||
$type_field_name = $bundle . '_type';
|
||||
|
||||
// Create a group for the asset bundle.
|
||||
$bundle_group = t('@bundle assets', ['@bundle' => $bundle_info['label']]);
|
||||
|
||||
// Get valid options for the bundle_type field.
|
||||
$options = options_allowed_values($fields[$type_field_name]);
|
||||
foreach ($options as $option => $label) {
|
||||
|
||||
// Add an exposed filter for the bundle_type field.
|
||||
$exposed_filters[$type_field_name . '[]'] = $option;
|
||||
|
||||
// Add layer for the bundle type.
|
||||
$asset_layers['full'][$bundle . '_' . $option] = [
|
||||
'group' => $bundle_group,
|
||||
'label' => $label,
|
||||
'asset_type' => $bundle,
|
||||
'filters' => $exposed_filters,
|
||||
// @todo Color each asset type differently.
|
||||
// This was previously provided with hook_farm_area_type_info.
|
||||
'color' => 'orange',
|
||||
'zoom' => TRUE,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
// Add full geometry layers for the other asset types.
|
||||
$asset_layers['full']['full_' . $bundle] = [
|
||||
'group' => $layer_group,
|
||||
'label' => $bundle_info['label'],
|
||||
'asset_type' => $bundle,
|
||||
'filters' => $exposed_filters,
|
||||
// @todo Color each asset type differently.
|
||||
// This was previously provided with hook_farm_area_type_info.
|
||||
'color' => 'orange',
|
||||
'zoom' => TRUE,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Build the map render array.
|
||||
$map = [
|
||||
'#type' => 'farm_map',
|
||||
'#map_type' => 'asset_list',
|
||||
];
|
||||
$all_layers = array_merge($asset_layers['cluster'], $asset_layers['full']);
|
||||
$map['#map_settings']['asset_type_layers'] = $all_layers;
|
||||
|
||||
// Render the map.
|
||||
$view->attachment_before['asset_map'] = $map;
|
||||
}
|
||||
}
|
|
@ -12,7 +12,6 @@ dependencies:
|
|||
- farm:farm_entity
|
||||
- farm:farm_location
|
||||
- farm:farm_ui_menu
|
||||
- farm:farm_map
|
||||
- farm:quantity
|
||||
- fraction:fraction
|
||||
- log:log
|
||||
|
|
|
@ -183,131 +183,13 @@ function farm_ui_views_views_pre_render(ViewExecutable $view) {
|
|||
return;
|
||||
}
|
||||
|
||||
// Get all asset bundles.
|
||||
$asset_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($view->getBaseEntityType()->id());
|
||||
|
||||
// If this is the "By type" display and a bundle argument is specified, load
|
||||
// the bundle label and set the title.
|
||||
if ($view->current_display == 'page_type' && !empty($view->args[0])) {
|
||||
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($view->getBaseEntityType()->id());
|
||||
$bundle = $view->args[0];
|
||||
if (!empty($asset_bundles[$bundle])) {
|
||||
$view->setTitle($asset_bundles[$bundle]['label'] . ' ' . $view->getBaseEntityType()->getPluralLabel());
|
||||
if (!empty($bundles[$bundle])) {
|
||||
$view->setTitle($bundles[$bundle]['label'] . ' ' . $view->getBaseEntityType()->getPluralLabel());
|
||||
}
|
||||
}
|
||||
|
||||
// Render a map attachment above views of assets.
|
||||
if ($view->id() == 'farm_asset' && in_array($view->current_display, ['page', 'page_type'])) {
|
||||
|
||||
// Start array of filtered bundles.
|
||||
$filtered_bundles = $asset_bundles;
|
||||
|
||||
// Start array of asset layers to add.
|
||||
$asset_layers = [
|
||||
'full' => [],
|
||||
'cluster' => [],
|
||||
];
|
||||
|
||||
// Save the translated cluster string.
|
||||
$cluster_string = ' (' . t('clusters') . ')';
|
||||
|
||||
// Save the group labels.
|
||||
$layer_group = $view->getBaseEntityType()->getCollectionLabel();
|
||||
$cluster_group = $layer_group . $cluster_string;
|
||||
|
||||
// Add multiple asset layers to the page of all assets.
|
||||
if ($view->current_display == 'page') {
|
||||
|
||||
// Limit to filtered asset types.
|
||||
if (!empty($exposed_filters['type'])) {
|
||||
$filtered_bundles = array_intersect_key($asset_bundles, array_flip($exposed_filters['type']));
|
||||
}
|
||||
}
|
||||
|
||||
// If this is the page_type display, only map this asset type.
|
||||
if ($view->current_display == 'page_type' && !empty($view->args[0])) {
|
||||
|
||||
// Don't use a separate group for cluster layers.
|
||||
$cluster_group = $layer_group;
|
||||
|
||||
$bundle = $view->args[0];
|
||||
$filtered_bundles = [
|
||||
$bundle => $asset_bundles[$bundle],
|
||||
];
|
||||
}
|
||||
|
||||
// Add an asset type map layer for each filtered bundle.
|
||||
foreach ($filtered_bundles as $bundle => $bundle_info) {
|
||||
|
||||
// Get exposed filters.
|
||||
$exposed_filters = $view->getExposedInput();
|
||||
|
||||
// Add a cluster layer for all asset types.
|
||||
$asset_layers['cluster']['cluster_' . $bundle] = [
|
||||
'group' => $cluster_group,
|
||||
'label' => $bundle_info['label'] . $cluster_string,
|
||||
'cluster' => TRUE,
|
||||
'asset_type' => $bundle,
|
||||
'filters' => $exposed_filters,
|
||||
// @todo Color each asset type cluster differently.
|
||||
];
|
||||
|
||||
// Create individual layers for land types and structure types.
|
||||
// @todo Abstract this to detect types other than land/structure?
|
||||
if (in_array($bundle, ['land', 'structure'])) {
|
||||
|
||||
// Get fields for the bundle.
|
||||
$entity_manager = \Drupal::service('entity_field.manager');
|
||||
$fields = $entity_manager->getFieldStorageDefinitions('asset', $bundle);
|
||||
$type_field_name = $bundle . '_type';
|
||||
|
||||
// Create a group for the asset bundle.
|
||||
$bundle_group = t('@bundle assets', ['@bundle' => $bundle_info['label']]);
|
||||
|
||||
// Get valid options for the bundle_type field.
|
||||
$options = options_allowed_values($fields[$type_field_name]);
|
||||
foreach ($options as $option => $label) {
|
||||
|
||||
// Add an exposed filter for the bundle_type field.
|
||||
$exposed_filters[$type_field_name . '[]'] = $option;
|
||||
|
||||
// Add layer for the bundle type.
|
||||
$asset_layers['full'][$bundle . '_' . $option] = [
|
||||
'group' => $bundle_group,
|
||||
'label' => $label,
|
||||
'asset_type' => $bundle,
|
||||
'filters' => $exposed_filters,
|
||||
// @todo Color each asset type differently.
|
||||
// This was previously provided with hook_farm_area_type_info.
|
||||
'color' => 'orange',
|
||||
'zoom' => TRUE,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
// Add full geometry layers for the other asset types.
|
||||
$asset_layers['full']['full_' . $bundle] = [
|
||||
'group' => $layer_group,
|
||||
'label' => $bundle_info['label'],
|
||||
'asset_type' => $bundle,
|
||||
'filters' => $exposed_filters,
|
||||
// @todo Color each asset type differently.
|
||||
// This was previously provided with hook_farm_area_type_info.
|
||||
'color' => 'orange',
|
||||
'zoom' => TRUE,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Build the map render array.
|
||||
$map = [
|
||||
'#type' => 'farm_map',
|
||||
'#map_type' => 'asset_list',
|
||||
];
|
||||
$all_layers = array_merge($asset_layers['cluster'], $asset_layers['full']);
|
||||
$map['#map_settings']['asset_type_layers'] = $all_layers;
|
||||
|
||||
// Render the map.
|
||||
$view->attachment_before['asset_map'] = $map;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue