-
Notifications
You must be signed in to change notification settings - Fork 4
/
ami.module
203 lines (187 loc) · 6.17 KB
/
ami.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
<?php
/**
* @file
* Contains ami.module.
*/
use Drupal\ami\AmiEventType;
use Drupal\ami\Event\AmiCrudEvent;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\format_strawberryfield\Event\FormatStrawberryfieldFormAlterEvent;
use Drupal\format_strawberryfield\FormatStrawberryfieldEventType;
use Drupal\ami\AmiUtilityService;
/**
* Implements hook_help().
*/
function ami_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.ami':
$text = file_get_contents(dirname(__FILE__) . "/README.md");
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . $text . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
}
/**
* Implements hook_form_alter().
*/
function ami_form_metadatadisplay_entity_form_alter(&$form,FormStateInterface $form_state, $form_id) {
// Add our AMI options here
$form['preview']['entity_type'] = [
'#type' => 'radios',
'#weight' => -10,
'#title' => t('Entity type'),
'#description' => t('The Entity Type you want to preview.'),
'#options' => [
'ado' => t('Archipelago Digital Objects'),
'ami' => t('AMI Sets'),
],
'#default_value' => $form_state->getValue('entity_type', NULL) ?? 'ado'
];
$form['preview']['ado_context_preview']['#states'] = [
'visible' => [
':input[name="entity_type"]' => ['value' => 'ado'],
],
];
$form['preview']['ado_amiset_preview'] = [
'#type' => 'entity_autocomplete',
'#weight' => -9,
'#title' => t('Ami Set to preview'),
'#description' => t('The AMI Set to be used to preview the data.'),
'#target_type' => 'ami_set_entity',
'#maxlength' => 1024,
'#ajax' => [
'callback' => '\Drupal\ami\Controller\AmiRowAutocompleteHandler::rowAjaxCallback',
'event' => 'autocompleteclose change',
],
'#states' => [
'visible' => [
':input[name="entity_type"]' => ['value' => 'ami']
],
],
];
$form['preview']['ado_amiset_row_context_preview'] = [
'#type' => 'textfield',
'#weight' => -8,
'#title' => t('Row to preview'),
'#states' => [
'visible' => [
':input[name="entity_type"]' => ['value' => 'ami'],
':input[name="ado_amiset_preview"]' => ['filled' => true],
],
],
];
$ami_set = $form_state->getValue('ado_amiset_preview', NULL);
if (is_scalar($ami_set)) {
$form['preview']['ado_amiset_row_context_preview']['#autocomplete_route_name'] = 'ami.rowsbylabel.autocomplete';
$form['preview']['ado_amiset_row_context_preview']['#autocomplete_route_parameters'] = [
'ami_set_entity' => $ami_set
];
}
$form['preview']['button_preview'][
'#states'] = [
'visible' => [
':input[name="ado_context_preview"]' => ['filled' => true],
':input[name="entity_type"]' => ['value' => 'ado'],
],
];
$form['preview']['button_preview_amiset'] = [
'#type' => 'button',
'#op' => 'preview',
'#weight' => -7,
'#value' => t('Show preview for AMI Set'),
'#ajax' => [
'callback' => '\Drupal\ami\Controller\AmiRowAutocompleteHandler::ajaxPreviewAmiSet',
],
'#states' => [
'visible' => [
':input[name="ado_amiset_preview"]' => ['filled' => true],
':input[name="entity_type"]' => ['value' => 'ami']
],
],
];
$form['preview']['render_native'] = [
'#type' => 'checkbox',
'#defaut_value' => FALSE,
'#weight' => -6,
'#title' => 'Show Preview using native Output Format (e.g. HTML)',
'#states' => [
'visible' => [
[
':input[name="ado_context_preview"]' => ['filled' => true]
],
'or',
[
':input[name="ado_amiset_preview"]' => ['filled' => true],
':input[name="ado_amiset_row_context_preview"]' => ['filled' => true]
]
],
],
];
$form['preview']['show_json_table'] = [
'#type' => 'checkbox',
'#defaut_value' => FALSE,
'#weight' => -5,
'#title' => 'Show Preview with JSON keys used in this template',
'#states' => [
'visible' => [
[
':input[name="ado_context_preview"]' => ['filled' => true]
],
'or',
[
':input[name="ado_amiset_preview"]' => ['filled' => true],
':input[name="ado_amiset_row_context_preview"]' => ['filled' => true]
]
],
],
];
return $form;
}
/**
* Implements hook_entity_update().
*/
function ami_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'ami_set_entity') {
// Invalidate AMI Set delete processed ADOs access check cache.
AmiUtilityService::invalidateAmiSetDeleteAdosAccessCache($entity);
}
}
/**
* Implements hook_entity_delete().
*/
function ami_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'ami_set_entity') {
// Invalidate AMI Set delete processed ADOs access check cache.
AmiUtilityService::invalidateAmiSetDeleteAdosAccessCache($entity);
// Remove any AMI LoD Key Values.
\Drupal::service('ami.lod')->cleanKeyValuesPerAmiSet($entity->id());
// Remove any left over logs
try {
$log_location = "private://ami/logs/set{$entity->id()}.log";
$filePath = \Drupal::service('file_system')->realpath($log_location);
\Drupal::service('file_system')->delete($filePath);
}
catch (\Exception $exception) {
watchdog_exception('ami', $exception);
}
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function ami_ami_set_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if ($sbf_fields = \Drupal::service('strawberryfield.utility')->bearsStrawberryfield($entity)) {
$event_type = AmiEventType::PRESAVE;
$event = new AmiCrudEvent($event_type, $entity, $sbf_fields);
\Drupal::service('event_dispatcher')->dispatch($event, $event_type);
}
}