3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00
farmOS/modules/ui/views/farm_ui_views.module
2020-12-06 16:33:29 -05:00

85 lines
2.7 KiB
PHP

<?php
/**
* @file
* The farmOS UI Views module.
*/
use Drupal\views\ViewExecutable;
/**
* Implements hook_farm_dashboard_panes().
*/
function farm_ui_views_farm_dashboard_panes() {
return [
'upcoming_tasks' => [
'view' => 'farm_log',
'view_display_id' => 'block_upcoming',
'group' => 'logs',
'weight' => 10,
],
'late_tasks' => [
'view' => 'farm_log',
'view_display_id' => 'block_late',
'group' => 'logs',
'weight' => 11,
],
];
}
/**
* Implements hook_views_pre_view().
*/
function farm_ui_views_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
// We only want to alter the Views we provide.
if (!in_array($view->id(), ['farm_asset', 'farm_log', 'farm_plan'])) {
return;
}
// If this is the "By type" display and a type is specified, remove the type
// field and filter handlers.
if ($display_id == 'page_type' && !empty($args[0])) {
$view->removeHandler($display_id, 'field', 'type');
$view->removeHandler($display_id, 'filter', 'type');
}
// If this is the "Upcoming" or "Late" Logs block display, add a "more" link
// that points to the default page display with appropriate filters.
if ($view->id() == 'farm_log' && in_array($display_id, ['block_upcoming', 'block_late'])) {
$view->display_handler->setOption('use_more', TRUE);
$view->display_handler->setOption('use_more_always', TRUE);
$view->display_handler->setOption('link_display', 'custom_url');
$today = date('Y-m-d', \Drupal::time()->getRequestTime());
if ($display_id == 'block_upcoming') {
$view->display_handler->setOption('use_more_text', t('View all upcoming logs'));
$view->display_handler->setOption('link_url', 'logs?status[]=pending&start=' . $today);
}
elseif ($display_id == 'block_late') {
$view->display_handler->setOption('use_more_text', t('View all late logs'));
$view->display_handler->setOption('link_url', 'logs?status[]=pending&end=' . $today);
}
}
}
/**
* Implements hook_views_pre_render().
*/
function farm_ui_views_views_pre_render(ViewExecutable $view) {
// We only want to alter the Views we provide.
if (!in_array($view->id(), ['farm_asset', 'farm_log', 'farm_plan'])) {
return;
}
// 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($bundles[$bundle])) {
$view->setTitle($bundles[$bundle]['label'] . ' ' . $view->getBaseEntityType()->getPluralLabel());
}
}
}