2020-10-16 03:10:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2020-11-27 16:33:30 +01:00
|
|
|
* The farmOS UI Views module.
|
2020-10-16 03:10:07 +02:00
|
|
|
*/
|
|
|
|
|
2020-12-05 12:42:41 +01:00
|
|
|
use Drupal\views\ViewExecutable;
|
|
|
|
|
2020-10-16 03:10:07 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_farm_dashboard_panes().
|
|
|
|
*/
|
2020-11-27 16:33:30 +01:00
|
|
|
function farm_ui_views_farm_dashboard_panes() {
|
2020-12-06 22:14:30 +01:00
|
|
|
$panes = [];
|
|
|
|
|
2020-12-06 22:15:03 +01:00
|
|
|
// 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,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-12-06 22:14:30 +01:00
|
|
|
// 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,
|
2020-10-16 03:10:07 +02:00
|
|
|
];
|
2020-12-06 22:14:30 +01:00
|
|
|
|
|
|
|
return $panes;
|
2020-10-16 03:10:07 +02:00
|
|
|
}
|
2020-12-05 12:42:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'])) {
|
2020-12-05 12:42:41 +01:00
|
|
|
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');
|
|
|
|
}
|
2020-12-06 18:19:56 +01:00
|
|
|
|
2020-12-06 22:05:18 +01:00
|
|
|
// 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'])) {
|
2020-12-06 18:19:56 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 12:42:41 +01:00
|
|
|
}
|
2020-12-05 12:43:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'])) {
|
2020-12-05 12:43:18 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|