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

98 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/**
* @file
2020-11-27 16:33:30 +01:00
* The farmOS UI Views module.
*/
use Drupal\views\ViewExecutable;
/**
* Implements hook_farm_dashboard_panes().
*/
2020-11-27 16:33:30 +01:00
function farm_ui_views_farm_dashboard_panes() {
$panes = [];
// If the plan module is enabled, add active plans pane.
if (\Drupal::service('module_handler')->moduleExists('plan')) {
$panes['active_plans'] = [
'view' => 'farm_plan',
'view_display_id' => 'block_active',
'group' => 'plans',
'weight' => 0,
];
}
// Add upcoming and late logs panes.
$panes['upcoming_tasks'] = [
'view' => 'farm_log',
'view_display_id' => 'block_upcoming',
'group' => 'logs',
'weight' => 10,
];
$panes['late_tasks'] = [
'view' => 'farm_log',
'view_display_id' => 'block_late',
'group' => 'logs',
'weight' => 11,
];
return $panes;
}
/**
* 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.
2021-03-08 15:52:40 +01:00
if (!in_array($view->id(), ['farm_asset', 'farm_log', 'farm_plan', 'farm_quantity'])) {
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.
2021-03-08 15:52:40 +01:00
if (!in_array($view->id(), ['farm_asset', 'farm_log', 'farm_plan', 'farm_quantity'])) {
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());
}
}
}