-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopencase.module
217 lines (192 loc) · 6.5 KB
/
opencase.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* @file
* Contains opencase.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\opencase\EntityTypeRelationsWidget;
/**
* Implements hook_element_info_alter().
*/
function opencase_element_info_alter(array &$types) {
$types['datetime']['#process'][] = 'opencase_process_element';
}
/**
* Element process callback for datetime fields. Removes the seconds part.
*/
function opencase_process_element($element) {
if ($element['#date_time_element'] !== 'none') {
$element['#date_time_format'] = 'H:i';
}
if (!empty($element['time']['#value'])) {
$parts = explode(':', $element['time']['#value']);
$parts = array_splice($parts, 0, 2);
$element['time']['#value'] = implode(':', $parts);
}
// Remove seconds in browsers that support HTML5 type=date.
$element['time']['#attributes']['step'] = 60;
return $element;
}
/**
* Implements hook_block_access
*
* Forbids the opencase_contextual_menu block on pages where it has no content.
* (Without this, it was displaying an empty sidebar)
*/
function opencase_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\Core\Session\AccountInterface $account) {
if ($operation == 'view' && $block->getPluginId() == 'opencase_contextual_menu') {
$route_name = \Drupal::routeMatch()->getRouteName();
$routes_where_it_should_be_shown = [
'entity.oc_actor.canonical',
'entity.oc_actor.edit_form',
'view.cases.page_1',
'entity.oc_case.canonical',
'entity.oc_case.edit_form',
'entity.oc_case.add_form',
'view.activities.page_1',
'entity.oc_activity.canonical',
'entity.oc_activity.edit_form',
'entity.oc_activity.add_form',
];
return AccessResult::forbiddenIf(!in_array($route_name, $routes_where_it_should_be_shown))->addCacheableDependency($block);
}
// No opinion.
return AccessResult::neutral();
}
/**
* Implements hook_page_attachments
*
* Add the opencase library to every page
*/
function opencase_page_attachments(array &$page) {
$page['#attached']['library'][] = 'opencase/opencase-lib';
}
/**
* Implements hook_link_alter
*
* Makes menu items that are external links open in new tab.
*/
function opencase_link_alter(&$variables) {
if ($variables['url']->isExternal()) {
$variables['options']['attributes'] = ['target' => '_blank'];
}
}
/**
* Implements hook_preprocess_page_title
*
* Modify the page title to include more information
*/
function opencase_preprocess_page_title(&$variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
switch ($route_name) {
case 'entity.oc_case.canonical':
$case = \Drupal::routeMatch()->getParameter('oc_case');
$variables['title'] = $case->getName() . ": Case Details and Files";
break;
case 'view.cases.page_1':
$actor_id = \Drupal::routeMatch()->getParameter('actor_id');
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
$variables['title'] = $actor->getName() . ": Cases";
break;
case 'view.activities.page_1':
$case_id = \Drupal::routeMatch()->getParameter('case_id');
$case = \Drupal::entityTypeManager()->getStorage('oc_case')->load($case_id);
$variables['title'] = $case->getName() . ": Activities";
break;
}
}
/**
* Implements hook_help().
*/
function opencase_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the opencase module.
case 'help.page.opencase':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Simple Case Management') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function opencase_theme() {
return [
'opencase' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_form_ID_alter
*/
function opencase_form_oc_case_type_add_form_alter(&$form, $form_state) {
$widget = new EntityTypeRelationsWidget();
$widget->setup($form);
}
/**
* Implements hook_form_ID_alter
*/
function opencase_form_oc_case_type_edit_form_alter(&$form, $form_state) {
$widget = new EntityTypeRelationsWidget();
$widget->setup($form);
$widget->populate($form);
}
/**
* Implements hook_uninstall().
* Removes configs.
*/
function opencase_uninstall() {
$configs = [
'block.block.opencasecontextualmenu',
'block.block.opencase',
'system.menu.opencase',
'views.view.cases',
'views.view.activities',
'views.view.contact_details_changes'
];
foreach($configs as $config) {
Drupal::configFactory()->getEditable($config)->delete();
}
}
function opencase_views_pre_render($view) {
if (empty($view->result) && empty($view->exposed_input)) {
$view->exposed_widgets = NULL;
}
}
function opencase_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $account, $items = NULL) {
if ($field_definition->getName() == 'field_linked_opencase_actor'
&& $operation == 'edit'
&& !$account->hasPermission('administer users')) {
return AccessResult::forbidden();
}
return AccessResult::neutral();
}
/**
* Implementation of hook_form_alter()
* After deleting an actor or a case, redirect to the main page, which is a view of all cases.
*/
function opencase_form_alter(&$form, &$form_state, $form_id) {
if (preg_match('/oc_actor_.*_delete_form/', $form_id) or (preg_match('/oc_case_.*_delete_form/', $form_id))) {
$form['actions']['submit']['#submit'][] = '_opencase_delete_case_redirect';
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
}
if (preg_match('/oc_activity_.*_delete_form/', $form_id)) {
$form['actions']['submit']['#submit'][] = '_opencase_delete_activity_redirect';
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
}
}
function _opencase_delete_case_redirect($form, &$form_state) {
$form_state->setRedirect('view.cases.page_2'); // redirect to view of all cases
}
function _opencase_delete_activity_redirect($form, &$form_state) {
$case_id = $form_state->getFormObject()->getEntity()->oc_case->target_id;
$form_state->setRedirect('view.activities.page_1', ['case_id' => $case_id]); // redirect to the activity list
}
function _opencase_cancel_delete_activity_redirect($form, &$form_state) {
$entity = $form_state->getFormObject()->getEntity();
$form_state->setRedirect('entity.oc_activity.canonical', ['oc_activity' => $entity->id()]); // redirect to the activity
}