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

Issue #3188586: Add Views of logs that reference assets

This commit is contained in:
Michael Stenta 2021-05-20 18:39:46 -04:00
commit e26a30e8d7
7 changed files with 274 additions and 8 deletions

View file

@ -1321,7 +1321,7 @@ display:
display_plugin: block
id: block_late
display_title: 'Late (block)'
position: 7
position: 6
display_options:
display_extenders: { }
display_description: ''
@ -2215,7 +2215,7 @@ display:
display_plugin: rest_export
id: csv
display_title: 'CSV export (rest)'
position: 8
position: 7
display_options:
display_extenders: { }
path: logs.csv
@ -2328,6 +2328,114 @@ display:
- user
- user.permissions
tags: { }
page_asset:
display_plugin: page
id: page_asset
display_title: 'Asset reference (page)'
position: 3
display_options:
display_extenders: { }
display_description: ''
path: asset/%asset/logs/%log_type
arguments:
asset_target_id:
id: asset_target_id
table: log__asset
field: asset_target_id
relationship: none
group_type: group
admin_label: ''
default_action: 'not found'
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: true
validate:
type: 'entity:asset'
fail: 'not found'
validate_options:
operation: view
multiple: 0
bundles: { }
access: false
break_phrase: false
not: false
entity_type: log
entity_field: asset
plugin_id: numeric
type:
id: type
table: log_field_data
field: type
relationship: none
group_type: group
admin_label: ''
default_action: ignore
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: all
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: true
validate:
type: 'entity:log_type'
fail: 'not found'
validate_options:
operation: view
multiple: 0
access: false
bundles: { }
glossary: false
limit: 0
case: none
path_case: none
transform_dash: false
break_phrase: false
entity_type: log
entity_field: type
plugin_id: string
defaults:
arguments: false
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- user
- user.permissions
tags: { }
page_type:
display_plugin: page
id: page_type
@ -2401,7 +2509,7 @@ display:
display_plugin: page
id: page_user
display_title: 'Assigned (page)'
position: 3
position: 4
display_options:
display_extenders: { }
display_description: ''

View file

@ -0,0 +1,15 @@
farm.asset.logs:
title: 'Logs'
route_name: view.farm_log.page_asset
route_parameters:
log_type: 'all'
base_route: entity.asset.canonical
weight: 50
farm.asset.logs.all:
title: 'All'
route_name: view.farm_log.page_asset
route_parameters:
log_type: 'all'
parent_id: farm.asset.logs
farm.asset.logs.type:
deriver: Drupal\farm_ui_views\Plugin\Derivative\FarmLogViewsTaskLink

View file

@ -91,8 +91,15 @@ function farm_ui_views_views_pre_view(ViewExecutable $view, $display_id, array &
return;
}
// Only alter the "By type" display if a type is provided.
// If this is a "By type" display, alter the fields and filters.
$bundle = FALSE;
if ($display_id == 'page_type' && !empty($args[0])) {
$bundle = $args[0];
}
elseif ($view->id() == 'farm_log' && $display_id == 'page_asset' && !empty($args[1]) && $args[1] != 'all') {
$bundle = $args[1];
}
if (!empty($bundle)) {
// Remove the type field and filter handlers.
$view->removeHandler($display_id, 'field', 'type');
@ -101,8 +108,8 @@ function farm_ui_views_views_pre_view(ViewExecutable $view, $display_id, array &
// If the entity type has a bundle_plugin manager, add all of its
// bundle fields and filters to the page_type view.
if (\Drupal::entityTypeManager()->hasHandler($view->getBaseEntityType()->id(), 'bundle_plugin')) {
farm_ui_views_add_bundle_handlers($view, 'page_type', $view->args[0], 'field');
farm_ui_views_add_bundle_handlers($view, 'page_type', $view->args[0], 'filter');
farm_ui_views_add_bundle_handlers($view, $display_id, $bundle, 'field');
farm_ui_views_add_bundle_handlers($view, $display_id, $bundle, 'filter');
}
}
@ -298,11 +305,17 @@ function farm_ui_views_views_pre_render(ViewExecutable $view) {
return;
}
// If this is the "By type" display and a bundle argument is specified, load
// If this is a "By type" display and a bundle argument is specified, load
// the bundle label and set the title.
$bundle = FALSE;
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];
}
elseif ($view->id() == 'farm_log' && $view->current_display == 'page_asset' && !empty($view->args[1]) && $view->args[1] != 'all') {
$bundle = $view->args[1];
}
if (!empty($bundle)) {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($view->getBaseEntityType()->id());
if (!empty($bundles[$bundle])) {
$view->setTitle($bundles[$bundle]['label'] . ' ' . $view->getBaseEntityType()->getPluralLabel());
}

View file

@ -0,0 +1,10 @@
services:
farm_ui_views.route_subscriber:
class: Drupal\farm_ui_views\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
farm_ui_views.asset_logs_access:
class: Drupal\farm_ui_views\Access\FarmAssetLogViewsAccessCheck
arguments: ['@current_route_match']
tags:
- { name: access_check, applies_to: _asset_logs_access }

View file

@ -0,0 +1,57 @@
<?php
namespace Drupal\farm_ui_views\Access;
use Drupal\Core\Database\Database;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
/**
* Checks access for displaying Views of logs that reference assets.
*/
class FarmAssetLogViewsAccessCheck implements AccessInterface {
/**
* The database object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* FarmAssetLogViewsAccessCheck constructor.
*/
public function __construct() {
$this->database = Database::getConnection();
}
/**
* A custom access check.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function access(RouteMatchInterface $route_match) {
// If there is no "asset" or "log_type" parameter, bail.
$asset_id = $route_match->getParameter('asset');
$log_type = $route_match->getParameter('log_type');
if (empty($asset_id) || empty($log_type)) {
return AccessResult::allowed();
}
// If the log type is "all", bail.
if ($log_type == 'all') {
return AccessResult::allowed();
}
// Run a count query to see if there are any logs of this type that
// reference the asset.
$query = "SELECT COUNT(*) FROM {log} l LEFT JOIN {log__asset} la ON l.id = la.entity_id WHERE l.type = :log_type AND la.asset_target_id = :asset_id";
$args = [':log_type' => $log_type, ':asset_id' => $asset_id];
$log_count = $this->database->query($query, $args)->fetchField();
return (!empty((int) $log_count)) ? AccessResult::allowed() : AccessResult::forbidden();
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace Drupal\farm_ui_views\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Provides task links for farmOS Logs Views.
*/
class FarmLogViewsTaskLink extends DeriverBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$links = [];
// Add links for each bundle.
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('log');
foreach ($bundles as $type => $info) {
$links['farm.asset.logs.' . $type] = [
'title' => $info['label'],
'parent_id' => 'farm.asset.logs',
'route_name' => 'view.farm_log.page_asset',
'route_parameters' => [
'log_type' => $type,
],
] + $base_plugin_definition;
}
return $links;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Drupal\farm_ui_views\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Alter routes for the farm_api module.
*
* @ingroup farm
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
// Add our _asset_logs_access requirement to view.farm_log.page_asset.
if ($route = $collection->get('view.farm_log.page_asset')) {
$route->setRequirement('_asset_logs_access', 'Drupal\farm_ui_views\Access\FarmAssetLogViewsAccessCheck::access');
}
}
}