forked from systopia/drupal-civiremote_funding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civiremote_funding.module
165 lines (150 loc) · 5.39 KB
/
civiremote_funding.module
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* Copyright (C) 2022 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
use Drupal\civiremote_funding\Api\FundingApi;
use Drupal\civiremote_funding\Breadcrumb\BreadcrumbRouteAnalyzer;
use Drupal\civiremote_funding\File\FundingFileDownloadHook;
use Drupal\civiremote_funding\File\FundingFileManager;
use Drupal\civiremote_funding\Install\DashboardBlockInstaller;
use Drupal\civiremote_funding\Views\ApplicationProcessDropButton;
use Drupal\civiremote_funding\ViewTranslator;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\FileInterface;
use Drupal\views\Plugin\views\field\Dropbutton;
use Drupal\views\ViewExecutable;
function civiremote_funding_views_pre_build(ViewExecutable $view): void {
if (!in_array($view->id(), ['civiremote_funding_application_list', 'civiremote_funding_combined_application_process_list'], TRUE)) {
return;
}
if (($view->field['dropbutton'] ?? NULL) instanceof Dropbutton) {
/** @var \Drupal\civiremote_funding\Api\FundingApi $fundingApi */
$fundingApi = \Drupal::service(FundingApi::class);
$view->field['dropbutton'] = new ApplicationProcessDropButton($fundingApi, $view->field['dropbutton']);
}
}
/**
* Implements hook_rebuild().
*/
function civiremote_funding_rebuild(): void {
/** @var ViewTranslator $viewTranslator */
$viewTranslator = \Drupal::service(ViewTranslator::class);
$viewTranslator->translateViews('views.view.civiremote_funding_');
}
/**
* Implements hook_batch_alter().
*
* @param array<string, mixed> $batch See \batch_set() for details.
*
* @see \batch_set()
*/
function civiremote_funding_batch_alter(array &$batch): void {
// Translate views when language is added
// @phpstan-ignore-next-line
foreach ($batch['sets'] ?? [] as &$set) {
// @phpstan-ignore-next-line
if ('locale_translation_batch_fetch_finished' === ($set['finished'] ?? NULL)) {
// @phpstan-ignore-next-line
$batch['sets'][] = [
'operations' => [],
'finished' => 'translateViews',
'results' => [],
'elapsed' => 0,
];
break;
}
}
}
/**
* Implements hook_theme().
*
* @return array<string, mixed>
*/
function civiremote_funding_theme(): array {
return [
'civiremote_funding_application_history' => [
'template' => 'application/history',
'render element' => 'civiremote_funding_application_history',
],
'civiremote_funding_application_history_entry' => [
'template' => 'application/history_entry',
'variables' => [
'attributes' => [],
'title' => NULL,
'icon' => NULL,
'icon_color' => NULL,
'date' => NULL,
'content' => NULL,
],
],
'civiremote_funding_dashboard_element' => [
'template' => 'dashboard_element',
'render element' => 'civiremote_funding_dashboard_element',
],
];
}
/**
* Implements hook_system_breadcrumb_alter().
*/
function civiremote_funding_system_breadcrumb_alter(Breadcrumb $breadcrumb, RouteMatchInterface $routeMatch): void {
$route = $routeMatch->getRouteObject();
if (NULL !== $route && BreadcrumbRouteAnalyzer::create(\Drupal::getContainer())->containsVariableData($route)) {
// If breadcrumb contains data that might change, i.e. funding case name, it should not take too long until a
// change is reflected in breadcrumb.
$cacheableMetadata = (new CacheableMetadata())->setCacheMaxAge(600);
$breadcrumb->addCacheableDependency($cacheableMetadata);
}
}
/**
* Implements hook_ENTITY_TYPE_predelete().
*/
function civiremote_funding_file_predelete(FileInterface $file): void {
/** @var \Drupal\civiremote_funding\File\FundingFileManager $fundingFileManager */
$fundingFileManager = \Drupal::service(FundingFileManager::class);
$fundingFileManager->onFilePreDelete($file);
}
/**
* Implements hook_file_download().
*
* @return array<string, mixed>
*/
function civiremote_funding_file_download(string $uri): array {
/** @var \Drupal\civiremote_funding\File\FundingFileDownloadHook $fundingFileDownloadHook */
$fundingFileDownloadHook = \Drupal::service(FundingFileDownloadHook::class);
return $fundingFileDownloadHook($uri);
}
/**
* Implements hook_cron().
*/
function civiremote_funding_cron(): void {
$cleanupDelay = \Drupal::config('civiremote_funding.settings')->get('file_cleanup_delay') ?? 0;
if (!is_int($cleanupDelay) || 0 === $cleanupDelay) {
return;
}
/** @var \Drupal\civiremote_funding\File\FundingFileManager $fundingFileManager */
$fundingFileManager = \Drupal::service(FundingFileManager::class);
foreach ($fundingFileManager->loadByLastAccessBefore(time() - $cleanupDelay) as $fundingFile) {
$fundingFileManager->delete($fundingFile);
}
}
/**
* Implements hook_install().
*/
function civiremote_funding_install(): void {
DashboardBlockInstaller::addDashboardBlocks();
}