2021-04-03 23:05:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* The farmOS UI Map module.
|
|
|
|
*/
|
|
|
|
|
2021-06-04 21:14:28 +02:00
|
|
|
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
2021-06-09 05:34:07 +02:00
|
|
|
use Drupal\Core\Link;
|
2021-06-04 21:14:28 +02:00
|
|
|
|
2021-04-05 16:24:56 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_farm_dashboard_panes().
|
|
|
|
*/
|
|
|
|
function farm_ui_map_farm_dashboard_panes() {
|
|
|
|
return [
|
|
|
|
'dashboard_map' => [
|
|
|
|
'block' => 'dashboard_map',
|
2021-06-06 04:41:25 +02:00
|
|
|
'region' => 'top',
|
2021-04-05 16:24:56 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-04-08 20:40:46 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_module_implements_alter().
|
|
|
|
*/
|
|
|
|
function farm_ui_map_module_implements_alter(&$implementations, $hook) {
|
|
|
|
|
|
|
|
// Ensure that this module's hook_views_pre_render() runs first.
|
|
|
|
if ($hook == 'views_pre_render') {
|
|
|
|
$module = 'farm_ui_map';
|
|
|
|
$group = $implementations[$module];
|
|
|
|
unset($implementations[$module]);
|
|
|
|
$implementations = array_merge([$module => $group], $implementations);
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 21:14:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_ENTITY_TYPE_view().
|
|
|
|
*/
|
|
|
|
function farm_ui_map_asset_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
|
|
|
|
/** @var \Drupal\farm_entity\Plugin\Asset\AssetType\AssetTypeInterface $entity */
|
|
|
|
|
|
|
|
// Bail if not the map_popup view mode.
|
|
|
|
if ($view_mode !== 'map_popup') {
|
|
|
|
return $build;
|
|
|
|
}
|
|
|
|
|
2021-06-08 23:57:15 +02:00
|
|
|
// The default view mode is used if a map_popup view mode is not provided.
|
|
|
|
// Alter the default view mode to only include common fields.
|
2021-06-09 14:21:28 +02:00
|
|
|
$view_mode_options = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle('asset', $entity->bundle());
|
|
|
|
if (!array_key_exists($view_mode, $view_mode_options)) {
|
2021-06-09 14:21:09 +02:00
|
|
|
$common_fields = ['name', 'type', 'flag', 'notes', 'location'];
|
2021-06-08 23:57:15 +02:00
|
|
|
$build = array_filter($build, function ($key) use ($common_fields) {
|
|
|
|
return strpos($key, '#') === 0 || in_array($key, $common_fields);
|
|
|
|
}, ARRAY_FILTER_USE_KEY);
|
|
|
|
}
|
|
|
|
|
2021-06-09 05:34:07 +02:00
|
|
|
// Build links.
|
|
|
|
$links = [];
|
|
|
|
|
2021-06-09 05:34:34 +02:00
|
|
|
// Build link to view assets located here.
|
|
|
|
// Only show this link on location assets.
|
|
|
|
if (\Drupal::service('asset.location')->isLocation($entity)) {
|
|
|
|
$links[] = Link::createFromRoute(t('View assets in this location'), 'view.farm_asset.page_location', ['asset' => $entity->id()])->toString();
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:14:28 +02:00
|
|
|
// Build link to view logs referencing the asset.
|
2021-06-09 05:34:07 +02:00
|
|
|
$links[] = Link::createFromRoute(t('View logs'), 'view.farm_log.page_asset', ['asset' => $entity->id(), 'log_type' => 'all'])->toString();
|
|
|
|
|
|
|
|
// Render links in a list.
|
|
|
|
$build['links'] = [
|
|
|
|
'#markup' => '<p>' . implode(' | ', $links) . '</p>',
|
2021-06-04 21:14:28 +02:00
|
|
|
'#weight' => -100,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $build;
|
|
|
|
}
|