forked from esmero/format_strawberryfield
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_strawberryfield.module
366 lines (333 loc) · 13.9 KB
/
format_strawberryfield.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* @file
* Contains formater_strawberryfield.module.
*/
use Drupal\format_strawberryfield\Controller\WebAnnotationController;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Swaggest\JsonDiff\JsonDiff;
/**
* Implements hook_page_attachments().
*
* Adds the iiif_openseadragon js library.
*/
function format_strawberryfield_page_attachments(array &$page) {
/*if (!\Drupal::currentUser()->hasPermission('access search')) {
return;
@TODO discuss if we need to limit viewers to a particular role?
Could be nice to allow annotations as a drop replacement
of the same viewer dependant on the rol
}*/
// Remove IIIF on admin routes, why make peoples life more complicated
/* if (\Drupal::service('router.admin_context')->isAdminRoute()) {
return;
}*/
$page['#attached']['library'][] = 'format_strawberryfield/lazyload_strawberry';
$page['#attached']['library'][] = 'core/jquery';
$page['#attached']['library'][] = 'core/drupal.dialog.ajax';
$page['#attached']['library'][] = 'core/drupal.ajax';
}
/**
* Implements hook_form_FORM_ID_alter() for node_form.
*/
function format_strawberryfield_form_node_form_alter(&$form, FormStateInterface $form_state) {
// Deal with Possible Addded Annotations.
if ($form_state->get('hadAnnotations')) {
$count = $form_state->get('hadAnnotations');
$form['annotations'] = [
'#type' => 'fieldset',
'#weigth ' => -1000,
'#title' => t('Unsaved Web Annotation Changes'),
];
$form['annotations']['container'] = [
'#type' => 'container',
'#markup' => \Drupal::translation()->formatPlural($count,
'You have one unsaved Annotation/Modification to an Annotation for this Digital Object',
'You have @count unsaved Annotation/Modification for this Digital Object'
)
];
$form['annotations']['markup'] = [
'#type' => 'markup',
'#markup' => 'To persist them <em>save</em> the Object or continue editing by going back to <em>View</em>. '
];
$form['annotations']['markup2'] = [
'#type' => 'markup',
'#markup' => 'Pressing <em>Discard</em> will keep the original ones untouched and will reload this page.'
];
// We may had the need to reload the page since we are overriding the SBF
// field value on format_strawberryfield_node_prepare_form
// @TODO maybe someone has a better idea on how to avoid/force reloading?
// The simples way really is to redirect the user back here.
$form['annotations']['discard'] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#type' => 'button',
'#value' => t('Discard'),
'#ajax' => [
'callback' => 'Drupal\format_strawberryfield\Controller\WebAnnotationController::deleteKeyStoreAjaxCallback',
'event' => 'click',
'progress' => [
'type' => 'throbber',
'message' => 'Restoring original Web Annotations ',
],
]
];
}
}
/**
* Implements hook_entity_view_mode_alter().
*/
function format_strawberryfield_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
if ($entity->getEntityTypeId() != 'node') {
return;
}
if ($view_mode != 'full') {
return;
}
$view_mode = \Drupal::service('format_strawberryfield.view_mode_resolver')->get($entity);
// To be determined if we want to be dependat on ds module or not
// if so we really want to act on this alter and not the general one.
/* \Drupal::moduleHandler()->alter('ds_switch_view_mode', $view_mode, $original_view_mode, $entity); */
}
/**
* Implements hook_theme().
*/
function format_strawberryfield_theme() {
return [
'format_strawberryfield_pdfs' => [
'variables' => [
'item' => NULL
],
'template' => 'format-strawberryfield-pdfs'
],
];
}
/**
* Implements hook__file_mimetype_mapping_alter().
*/
function format_strawberryfield_file_mimetype_mapping_alter(&$mapping) {
// Add relevant Repository Mimetypes missing from D8
$mapping['mimetypes']['obj_model_mimetype'] = 'model/obj';
$mapping['mimetypes']['webarchive_mimetype'] = 'application/warc';
$mapping['mimetypes']['ob_material_mimetype'] = 'model/mtl';
$mapping['extensions']['obj'] = 'obj_model_mimetype';
$mapping['extensions']['warc'] = 'webarchive_mimetype';
$mapping['extensions']['wacz'] = 'webarchive_mimetype';
$mapping['extensions']['warc.gz'] = 'webarchive_mimetype';
$mapping['extensions']['mtl'] = 'ob_material_mimetype';
// @see https://www.iana.org/assignments/media-types/media-types.xhtml
}
/**
* Implements hook_ENTITY_TYPE_prepare_form() for node entities.
*/
function format_strawberryfield_node_prepare_form(NodeInterface $entity, $operation, FormStateInterface $form_state) {
// Prepare defaults for the add/edit form.
// Because of Form Modes we can not only depend on "edit". But more likely on
if (($operation == 'delete' || $operation == 'quick_node_clone' || $entity->isNew())) {
return;
}
$account = \Drupal::currentUser();
if (($sbf_fields = \Drupal::service('strawberryfield.utility')->bearsStrawberryfield($entity)) &&
$account->hasPermission('add strawberryfield webannotation')) {
foreach ($sbf_fields as $field_name) {
/* @var $field \Drupal\Core\Field\FieldItemInterface */
$field = $entity->get($field_name);
/* @var \Drupal\strawberryfield\Field\StrawberryFieldItemList $field */
foreach ($field->getIterator() as $delta => $itemfield) {
/** @var $itemfield \Drupal\strawberryfield\Plugin\Field\FieldType\StrawberryFieldItem */
$fullvalues = $itemfield->provideDecoded(TRUE);
/* @var $tempstore \Drupal\Core\TempStore\PrivateTempStore */
$tempstore = \Drupal::service('tempstore.private')->get('webannotation');
$keystoreid = WebAnnotationController::getTempStoreKeyName(
$field_name,
$delta,
$entity->uuid()
);
$annotation_values = $tempstore->get($keystoreid);
// If not present escape the loop. Needs to be EXACT NULL.
if ($annotation_values === NULL) {
break 2;
}
$annotation_values = is_array($annotation_values) ? $annotation_values : [];
// This covers:
/*
* We got more than 0 annotations from Cache and we also have saved ones and both are not equal OR (UPDATE)
* We got more than 0 annotations from Cache and we have no saved nor the supporting structure to save (NEW)
* None from Cache (but cache exists) and we also have saved ones (REMOVE ALL)
*/
if (
(count($annotation_values) && isset($fullvalues['ap:annotationCollection']) && $annotation_values != $fullvalues['ap:annotationCollection']) ||
(count($annotation_values) && !isset($fullvalues['ap:annotationCollection'])) ||
(count($annotation_values) == 0 && isset($fullvalues['ap:annotationCollection']))
) {
// Super handy! Potential for the future: JSON UNDO!!
$fullvalues['ap:annotationCollection'] = isset($fullvalues['ap:annotationCollection']) ? $fullvalues['ap:annotationCollection'] : [];
$r = new JsonDiff(
$fullvalues['ap:annotationCollection'],
$annotation_values,
JsonDiff::REARRANGE_ARRAYS + JsonDiff::SKIP_JSON_MERGE_PATCH
);
// We just keep track of the changes. If none! Then we do not set
// the formstate flag.
if ($r->getDiffCnt() > 0) {
$fullvalues['ap:annotationCollection'] = $annotation_values;
$entity->{$field_name}[0]->setMainValueFromArray($fullvalues);
$form_state->set('hadAnnotations', $r->getDiffCnt());
}
break 2;
}
if (!is_array($fullvalues)) {
break;
}
}
}
}
}
/**
* Implements hook_css_alter().
*/
function format_strawberryfield_css_alter(&$css) {
// Remove the stable.theme's off-canvas CSS reset for metadata display edit
// routes, otherwise CodeMirror doesn't display properly.
// @TODO Reevaluate for Drupal 9.
// @see https://www.drupal.org/project/drupal/issues/2826722
if (\Drupal::routeMatch()->getRouteName() !== 'entity.metadatadisplay_entity.edit_form') {
return;
}
foreach ($css as $key => $item) {
// @see 'core/misc/dialog/off-canvas
// @see core/themes/stable/css/core/dialog/off-canvas
if (strpos($key, '/off-canvas.') !== FALSE) {
unset($css[$key]);
}
}
}
/**
* Implements hook_contextual_links_alter().
*
* Converts one Metadata Display Entity Contextual Link with metadata to multiple
* Links with propert Titles.
*
*/
function format_strawberryfield_contextual_links_alter(array &$links, $group, array $route_parameters) {
if ($group == 'metadatadisplay_entity') {
$entities = [];
$other_entities = [];
$entities[] = $route_parameters['metadatadisplay_entity'] ?? NULL;
if (isset($links['metadatadisplay.page_edit']['metadata']['metadatadisplay_entity_ids']) &&
is_array($links['metadatadisplay.page_edit']['metadata']['metadatadisplay_entity_ids'])) {
$other_entities = $links['metadatadisplay.page_edit']['metadata']['metadatadisplay_entity_ids'];
}
$combined_entities = array_unique(array_merge($entities, $other_entities));
$metadata_display_entities = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->loadMultiple($combined_entities);
$weight = 0;
foreach ($metadata_display_entities as $entity) {
$weight++;
$links['metadatadisplay.page_edit.' . $entity->id()] = $links['metadatadisplay.page_edit'];
$links['metadatadisplay.page_edit.' . $entity->id()]['route_parameters']['metadatadisplay_entity'] = $entity->id();
$links['metadatadisplay.page_edit.' . $entity->id()]['title'] = t('Edit Metadatadata Display: @label',
['@label' => $entity->label()]);
$links['metadatadisplay.page_edit.' . $entity->id()]['weight'] = $weight;
}
// After building new link entries Delete the original One.
unset($links['metadatadisplay.page_edit']);
}
}
/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function format_strawberryfield_node_view_alter(&$build, EntityInterface $entity, EntityDisplayInterface $display) {
// Empty. Issue is if this hook does not exist, the broader one fail;
}
/**
* Implements hook_entity_view_alter().
*/
function format_strawberryfield_entity_view_alter(&$build, EntityInterface $entity, EntityDisplayInterface $display) {
if ($sbf_fields = \Drupal::service('strawberryfield.utility')->bearsStrawberryfield($entity)) {
$display_array = $display->toArray();
// First we get main field/that has a SBF
foreach ($sbf_fields as $sbf_field_name) {
if (isset($display_array['content'][$sbf_field_name]['settings']['metadatadisplayentity_uuid'])) {
$uuid = $display_array['content'][$sbf_field_name]['settings']['metadatadisplayentity_uuid'];
$entities = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->loadByProperties(['uuid' => $uuid]);
$entity = reset($entities);
if ($entity) {
$build['#contextual_links']['metadatadisplay_entity'] = [
'route_parameters' => ['metadatadisplay_entity' => $entity->id()],
];
}
}
}
// Now we deal with in memory DS copy fields
if (isset($display_array['third_party_settings']['ds']['fields'])) {
foreach ($display_array['third_party_settings']['ds']['fields'] as $candidate_field) {
// The list of formatters (static here, we may want to make this a method in a service
// or add a Field Formatter annotation in the future?) that use metadatadisplay_entities
if (in_array($candidate_field['formatter'],
[
'strawberry_metadata_formatter',
'strawberry_mirador_formatter',
'strawberry_paged_formatter',
'strawberry_map_formatter'
])) {
$uuid = $candidate_field['settings']['formatter']['metadatadisplayentity_uuid'] ?? NULL;
if ($uuid) {
$entities = \Drupal::entityTypeManager()
->getStorage('metadatadisplay_entity')
->loadByProperties(['uuid' => $uuid]);
$entity = reset($entities);
if ($entity) {
if (isset($build['#contextual_links']['metadatadisplay_entity'])) {
$build['#contextual_links']['metadatadisplay_entity']['metadata']['metadatadisplay_entity_ids'][] = (int) $entity->id();
}
else {
$build['#contextual_links']['metadatadisplay_entity'] = [
'route_parameters' => ['metadatadisplay_entity' => (int) $entity->id()],
];
}
}
}
}
}
}
}
}
/**
* Implements hook_module_implements_alter().
*
* We push our hooks to the end so we have all DS processed data available.
*/
function format_strawberryfield_module_implements_alter(&$implementations, $hook) {
if (($hook == 'entity_view_alter') || ($hook == 'node_view_alter')) {
$group = $implementations['format_strawberryfield'] ?? NULL;
if ($group !== NULL) {
unset($implementations['format_strawberryfield']);
$implementations['format_strawberryfield'] = $group;
}
}
}
/**
* Implements hook_cron().
*/
function format_strawberryfield_cron() {
$now = \Drupal::requestStack()->getCurrentRequest()->server->get('REQUEST_TIME');
$timesincerun = $now - \Drupal::state()->get('system.cron_last');
if ($timesincerun/60/60/24 > 7) {
\Drupal\Core\Cache\Cache::invalidateTags(['format_strawberryfield:all_embargo']);
}
else {
$dayssince = ceil($timesincerun/60/60/24);
$current_time = time();
for ($i = 0; $i < $dayssince; $i++) {
$date_to_invalidate = date('Y-m-d', $current_time - 60 * 60 * 24 * $i);
\Drupal\Core\Cache\Cache::invalidateTags(['format_strawberryfield:embargo:'.$date_to_invalidate]);
}
}
}