-
Notifications
You must be signed in to change notification settings - Fork 2
/
helfi_tpr.views_execution.inc
61 lines (53 loc) · 1.75 KB
/
helfi_tpr.views_execution.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* @file
* Views alter for helfi_tpr.
*/
declare(strict_types=1);
use Drupal\Core\Entity\EntityType;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_query_alter().
*/
function helfi_tpr_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
if ($view->id() == 'unit_search' || $view->id() == 'service_units') {
assert(isset($query->orderby));
// Use the CASE function from helfi_tpr_views_pre_execute() for sorting.
$query->orderby[0]['field'] = 'name_sort';
$query->orderby[0]['direction'] = 'ASC';
}
}
/**
* Implements hook_views_pre_execute().
*/
function helfi_tpr_views_pre_execute(ViewExecutable $view) {
if ($view->id() == 'unit_search' || $view->id() == 'service_units') {
// CASE function for using the name_override field as default for sorting.
// If that field is empty, use the name field.
$view->build_info['query']->addExpression('
CASE
WHEN tpr_unit_field_data.name_override IS NULL THEN tpr_unit_field_data.name
ELSE tpr_unit_field_data.name_override
END COLLATE utf8mb4_swedish_ci', 'name_sort'
);
}
}
/**
* Implements hook_views_query_substitutions().
*/
function helfi_tpr_views_query_substitutions(ViewExecutable $view) {
// Get permission name dynamically.
$base_entity = $view->getBaseEntityType();
if ($base_entity instanceof EntityType) {
$entity_permission = 'view unpublished ' . $base_entity->id();
}
// Default to no access if entity type can't be loaded.
else {
return ['***VIEW_UNPUBLISHED_TPR_ENTITIES***' => 0];
}
$account = \Drupal::currentUser();
return [
'***VIEW_UNPUBLISHED_TPR_ENTITIES***' => intval($account->hasPermission($entity_permission)),
];
}