3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Add land/structure type layers to land/structure asset Views.

This commit is contained in:
Michael Stenta 2021-04-08 14:41:18 -04:00
parent a27225e383
commit c7ddaab912
2 changed files with 108 additions and 0 deletions

View file

@ -5,6 +5,9 @@
* Land asset module.
*/
use Drupal\farm_land\Entity\FarmLandType;
use Drupal\views\ViewExecutable;
/**
* Allowed values callback function for the land type field.
*
@ -20,3 +23,54 @@ function farm_land_type_field_allowed_values() {
}
return $allowed_values;
}
/**
* Implements hook_views_pre_render().
*/
function farm_land_views_pre_render(ViewExecutable $view) {
// Add land type map layers to the land assets map.
if ($view->id() == 'farm_asset' && $view->current_display == 'page_type' && !empty($view->args[0]) && $view->args[0] == 'land') {
// If the asset_map has not been added, bail.
if (empty($view->attachment_before['asset_map'])) {
return;
}
$map = &$view->attachment_before['asset_map'];
// Load all land types.
$land_types = FarmLandType::loadMultiple();
// Get exposed filters.
$exposed_filters = $view->getExposedInput();
// If the land type exposed filter is already in use remove land types
// that are not included.
if (!empty($exposed_filters['land_type_value'])) {
$land_types = array_filter($land_types, function ($key) use ($exposed_filters) {
return in_array($key, $exposed_filters['land_type_value']);
}, ARRAY_FILTER_USE_KEY);
}
// Create a layer for each land type.
$asset_layers = [];
foreach ($land_types as $land_type) {
$asset_layers['land_' . $land_type->id()] = [
'group' => t('Land types'),
'label' => $land_type->label(),
'asset_type' => 'land',
'filters' => $exposed_filters + ['land_type_value[]' => $land_type->id()],
// @todo Color each differently.
// This was previously provided with hook_farm_area_type_info.
'color' => 'orange',
'zoom' => TRUE,
];
}
// Add layers to the map settings.
$map['#map_settings']['asset_type_layers'] = array_merge($map['#map_settings']['asset_type_layers'], $asset_layers);
// Remove the land asset layer.
unset($map['#map_settings']['asset_type_layers']['full_land']);
}
}

View file

@ -5,6 +5,9 @@
* Structure asset module.
*/
use Drupal\farm_structure\Entity\FarmStructureType;
use Drupal\views\ViewExecutable;
/**
* Allowed values callback function for the structure type field.
*
@ -20,3 +23,54 @@ function farm_structure_type_field_allowed_values() {
}
return $allowed_values;
}
/**
* Implements hook_views_pre_render().
*/
function farm_structure_views_pre_render(ViewExecutable $view) {
// Add structure type map layers to the structure assets map.
if ($view->id() == 'farm_asset' && $view->current_display == 'page_type' && !empty($view->args[0]) && $view->args[0] == 'structure') {
// If the asset_map has not been added, bail.
if (empty($view->attachment_before['asset_map'])) {
return;
}
$map = &$view->attachment_before['asset_map'];
// Load all structure types.
$structure_types = FarmStructureType::loadMultiple();
// Get exposed filters.
$exposed_filters = $view->getExposedInput();
// If the structure type exposed filter is already in use remove structure
// types that are not included.
if (!empty($exposed_filters['structure_type_value'])) {
$structure_types = array_filter($structure_types, function ($key) use ($exposed_filters) {
return in_array($key, $exposed_filters['structure_type_value']);
}, ARRAY_FILTER_USE_KEY);
}
// Create a layer for each structure type.
$asset_layers = [];
foreach ($structure_types as $structure_type) {
$asset_layers['structure_' . $structure_type->id()] = [
'group' => t('Structure types'),
'label' => $structure_type->label(),
'asset_type' => 'structure',
'filters' => $exposed_filters + ['structure_type_value[]' => $structure_type->id()],
// @todo Color each differently.
// This was previously provided with hook_farm_area_type_info.
'color' => 'orange',
'zoom' => TRUE,
];
}
// Add layers to the map settings.
$map['#map_settings']['asset_type_layers'] = array_merge($map['#map_settings']['asset_type_layers'], $asset_layers);
// Remove the structure asset layer.
unset($map['#map_settings']['asset_type_layers']['full_structure']);
}
}