3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00
farmOS/modules/ui/map/farm_ui_map.module
Michael Stenta 33c47849b8 Remove logic for adding separate land/structure type layers.
This needs to be refactored/moved to the land/structure modules.
2021-04-08 15:37:23 -04:00

110 lines
3.1 KiB
PHP

<?php
/**
* @file
* The farmOS UI Map module.
*/
use Drupal\views\ViewExecutable;
/**
* Implements hook_farm_dashboard_panes().
*/
function farm_ui_map_farm_dashboard_panes() {
return [
'dashboard_map' => [
'block' => 'dashboard_map',
],
];
}
/**
* 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.
];
// 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;
}
}