forked from discoverygarden/islandora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora.module
515 lines (455 loc) · 16.8 KB
/
islandora.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
/**
* @file
* Contains islandora.module.
*
* This file is part of the Islandora Project.
*
* (c) Islandora Foundation
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Diego Pino Navarro <dpino@metro.org> https://github.com/diegopino
*/
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\islandora\Form\IslandoraSettingsForm;
use Drupal\islandora\GeminiLookup;
use Drupal\node\NodeInterface;
use Drupal\media\MediaInterface;
use Drupal\file\FileInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function islandora_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the islandora module.
case 'help.page.islandora':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Islandora integrates Drupal with a Fedora repository.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_rdf_namespaces().
*/
function islandora_rdf_namespaces() {
// Yes, it's amazing, rdf is not registered by default!
return [
'ldp' => 'http://www.w3.org/ns/ldp#',
'dc11' => 'http://purl.org/dc/elements/1.1/',
'dcterms' => 'http://purl.org/dc/terms/',
'nfo' => 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo/v1.1/',
'ebucore' => 'http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#',
'fedora' => 'http://fedora.info/definitions/v4/repository#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'ore' => 'http://www.openarchives.org/ore/terms/',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'islandora' => 'http://islandora.ca/',
'pcdm' => 'http://pcdm.org/models#',
'use' => 'http://pcdm.org/use#',
'iana' => 'http://www.iana.org/assignments/relation/',
'premis' => 'http://www.loc.gov/premis/rdf/v1#',
'premis3' => 'http://www.loc.gov/premis/rdf/v3/',
'co' => 'http://purl.org/co/',
];
}
/**
* Implements hook_node_insert().
*/
function islandora_node_insert(NodeInterface $node) {
$utils = \Drupal::service('islandora.utils');
// Execute index reactions.
$utils->executeNodeReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $node);
}
/**
* Implements hook_node_update().
*/
function islandora_node_update(NodeInterface $node) {
$utils = \Drupal::service('islandora.utils');
if (!$utils->haveFieldsChanged($node, $node->original)) {
return;
};
// Execute index reactions.
$utils->executeNodeReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $node);
}
/**
* Implements hook_node_delete().
*/
function islandora_node_delete(NodeInterface $node) {
$utils = \Drupal::service('islandora.utils');
// Execute delete reactions.
$utils->executeNodeReactions('\Drupal\islandora\Plugin\ContextReaction\DeleteReaction', $node);
}
/**
* Implements hook_media_insert().
*/
function islandora_media_insert(MediaInterface $media) {
$utils = \Drupal::service('islandora.utils');
// Execute index reactions.
$utils->executeMediaReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $media);
// If it has a parent node...
$node = $utils->getParentNode($media);
if ($node) {
// Fire off derivative reactions for the Media.
$utils->executeDerivativeReactions(
'\Drupal\islandora\Plugin\ContextReaction\DerivativeReaction',
$node,
$media
);
}
}
/**
* Implements hook_media_update().
*/
function islandora_media_update(MediaInterface $media) {
$media_source_service = \Drupal::service('islandora.media_source_service');
// Exit early if nothing's changed.
$utils = \Drupal::service('islandora.utils');
if (!$utils->haveFieldsChanged($media, $media->original)) {
return;
};
// Execute index reactions.
$utils->executeMediaReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $media);
// Does it have a source field?
$source_field = $media_source_service->getSourceFieldName($media->bundle());
if (empty($source_field)) {
return;
}
// Exit early if the source file did not change.
if ($media->get($source_field)->equals($media->original->get($source_field))) {
return;
}
// If it has a parent node...
$node = $utils->getParentNode($media);
if ($node) {
// Fire off derivative reactions for the Media.
$utils->executeDerivativeReactions(
'\Drupal\islandora\Plugin\ContextReaction\DerivativeReaction',
$node,
$media
);
}
}
/**
* Implements hook_media_delete().
*/
function islandora_media_delete(MediaInterface $media) {
$utils = \Drupal::service('islandora.utils');
// Execute delete reactions.
$utils->executeMediaReactions('\Drupal\islandora\Plugin\ContextReaction\DeleteReaction', $media);
}
/**
* Implements hook_file_insert().
*/
function islandora_file_insert(FileInterface $file) {
$utils = \Drupal::service('islandora.utils');
// Execute index reactions.
$utils->executeFileReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $file);
}
/**
* Implements hook_file_update().
*/
function islandora_file_update(FileInterface $file) {
// Exit early if unchanged.
if ($file->filehash['sha1'] == $file->original->filehash['sha1']) {
return;
}
$utils = \Drupal::service('islandora.utils');
// Execute index reactions.
$utils->executeFileReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $file);
// Execute derivative reactions.
foreach ($utils->getReferencingMedia($file->id()) as $media) {
$node = $utils->getParentNode($media);
if ($node) {
$utils->executeDerivativeReactions(
'\Drupal\islandora\Plugin\ContextReaction\DerivativeReaction',
$node,
$media
);
}
}
}
/**
* Implements hook_file_delete().
*/
function islandora_file_delete(FileInterface $file) {
$utils = \Drupal::service('islandora.utils');
// Execute delete reactions.
$utils->executeFileReactions('\Drupal\islandora\Plugin\ContextReaction\DeleteReaction', $file);
}
/**
* Implements hook_taxonomy_term_insert().
*/
function islandora_taxonomy_term_insert(TermInterface $term) {
$utils = \Drupal::service('islandora.utils');
// Execute index reactions.
$utils->executeTermReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $term);
}
/**
* Implements hook_taxonomy_term_update().
*/
function islandora_taxonomy_term_update(TermInterface $term) {
$utils = \Drupal::service('islandora.utils');
// Execute index reactions.
$utils->executeTermReactions('\Drupal\islandora\Plugin\ContextReaction\IndexReaction', $term);
}
/**
* Implements hook_taxonomy_term_delete().
*/
function islandora_taxonomy_term_delete(TermInterface $term) {
$utils = \Drupal::service('islandora.utils');
// Execute delete reactions.
$utils->executeTermReactions('\Drupal\islandora\Plugin\ContextReaction\DeleteReaction', $term);
}
/**
* Implements hook_jsonld_alter_normalized_array().
*/
function islandora_jsonld_alter_normalized_array(EntityInterface $entity, array &$normalized, array $context) {
$context_manager = \Drupal::service('context.manager');
foreach ($context_manager->getActiveReactions('\Drupal\islandora\ContextReaction\NormalizerAlterReaction') as $reaction) {
$reaction->execute($entity, $normalized, $context);
foreach ($context_manager->getActiveContexts() as $context_config) {
try {
if ($context_config->getReaction($reaction->getPluginId())) {
$context['cacheability']->addCacheTags($context_config->getCacheTags());
};
}
catch (PluginNotFoundException $e) {
// Squash :(.
}
}
}
}
/**
* Implements hook_entity_view_mode_alter().
*/
function islandora_entity_view_mode_alter(&$view_mode, EntityInterface $entity) {
// Change the view mode based on user input from a 'view_mode_alter'
// ContextReaction.
$storage = \Drupal::service('entity_type.manager')->getStorage('entity_view_mode');
$context_manager = \Drupal::service('context.manager');
$current_entity = \Drupal::routeMatch()->getParameter('node');
$current_id = ($current_entity instanceof NodeInterface) ? $current_entity->id() : NULL;
if (isset($current_id) && $current_id == $entity->id()) {
foreach ($context_manager->getActiveReactions('\Drupal\islandora\Plugin\ContextReaction\ViewModeAlterReaction') as $reaction) {
// Construct the new view mode's machine name.
$entity_type = $entity->getEntityTypeId();
$mode = $reaction->execute();
$machine_name = "$entity_type.$mode";
// Try to load it.
$new_mode = $storage->load($machine_name);
// If successful, alter the view mode.
if ($new_mode) {
$view_mode = $mode;
}
else {
// Otherwise, leave it be, but log a message.
\Drupal::logger('islandora')
->info("EntityViewMode $machine_name does not exist. View mode cannot be altered.");
}
}
}
}
/**
* Implements hook_preprocess_node().
*/
function islandora_preprocess_node(&$variables) {
// Using alternate view modes causes on a node's canoncial page
// causes the title to get printed out twice. Once from the
// fields themselves and again as a block above the main content.
// Setting 'page' to TRUE gets rid of the title in the fields and
// leaves the block. This makes it look uniform with the 'default'
// view mode.
if (node_is_page($variables['elements']['#node'])) {
$variables['page'] = TRUE;
}
}
/**
* Implements hook_form_alter().
*/
function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$media_add_forms = ['media_audio_add_form', 'media_document_add_form',
'media_extracted_text_add_form', 'media_file_add_form', 'media_image_add_form',
'media_fits_technical_metadata_add_form', 'media_video_add_form',
];
if (in_array($form['#form_id'], $media_add_forms)) {
$params = \Drupal::request()->query->all();
if (isset($params['edit'])) {
$media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id'];
$node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid);
if ($node) {
$form['name']['widget'][0]['value']['#default_value'] = $node->getTitle();
}
}
}
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function islandora_field_widget_image_image_form_alter(&$element, $form_state, $context) {
$element['#process'][] = 'islandora_add_default_image_alt_text';
}
/**
* Callback for hook_field_widget_WIDGET_TYPE_form_alter().
*/
function islandora_add_default_image_alt_text($element, $form_state, $form) {
if ($element['alt']['#access']) {
$params = \Drupal::request()->query->all();
if (isset($params['edit'])) {
$media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id'];
$node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid);
if ($node) {
$element['alt']['#default_value'] = $node->getTitle();
}
}
}
return $element;
}
/**
* Implements hook_entity_form_display_alter().
*/
function islandora_entity_form_display_alter(&$form_display, $context) {
// Change the form display based on user input from a 'form_display_alter'
// ContextReaction.
$storage = \Drupal::service('entity_type.manager')->getStorage('entity_form_display');
$context_manager = \Drupal::service('context.manager');
// Alter form display based on context.
foreach ($context_manager->getActiveReactions('\Drupal\islandora\Plugin\ContextReaction\FormDisplayAlterReaction') as $reaction) {
// Construct the new form display's machine name.
$entity_type = $context['entity_type'];
$bundle = $context['bundle'];
$mode = $reaction->execute();
$machine_name = "$entity_type.$bundle.$mode";
// Try to load it.
$new_display = $storage->load($machine_name);
// If successful, alter the form display.
if ($new_display) {
$form_display = $new_display;
}
else {
// Otherwise, leave it be, but log a message.
\Drupal::logger('islandora')->info("EntityFormDisplay $machine_name does not exist. Form display cannot be altered.");
}
}
}
/**
* Implements hook_form_form_id_alter().
*/
function islandora_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Unset our custom conditions. There's too many to use well within
// the core block placement UI, and no other reasonable way to filter
// them out. See https://www.drupal.org/node/2284687. Use
// /admin/structure/context instead if you want to use these conditions
// to alter block layout.
unset($form['visibility']['content_entity_type']);
unset($form['visibility']['parent_node_has_term']);
unset($form['visibility']['node_had_namespace']);
unset($form['visibility']['media_has_term']);
unset($form['visibility']['file_uses_filesystem']);
unset($form['visibility']['node_has_term']);
unset($form['visibility']['node_has_parent']);
unset($form['visibility']['media_uses_filesystem']);
unset($form['visibility']['media_has_mimetype']);
unset($form['visibility']['node_is_islandora_object']);
unset($form['visibility']['media_is_islandora_media']);
}
/**
* Implements hook_entity_extra_field_info().
*/
function islandora_entity_extra_field_info() {
$config_factory = \Drupal::service('config.factory')->get(IslandoraSettingsForm::CONFIG_NAME);
$extra_field = [];
$pseudo_bundles = $config_factory->get(IslandoraSettingsForm::GEMINI_PSEUDO);
if (!empty($pseudo_bundles)) {
foreach ($pseudo_bundles as $key) {
list($bundle, $content_entity) = explode(":", $key);
$extra_field[$content_entity][$bundle]['display']['field_gemini_uri'] = [
'label' => t('Fedora URI'),
'description' => t('The URI to the persistent'),
'weight' => 100,
'visible' => TRUE,
];
}
}
return $extra_field;
}
/**
* Implements hook_entity_view().
*/
function islandora_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
$route_match_item = \Drupal::routeMatch()->getParameters()->get($entity->getEntityTypeId());
// Ensure the entity matches the route.
if ($entity === $route_match_item) {
if ($display->getComponent('field_gemini_uri')) {
$gemini = \Drupal::service('islandora.gemini.lookup');
if ($gemini instanceof GeminiLookup) {
$fedora_uri = $gemini->lookup($entity);
if (!is_null($fedora_uri)) {
$build['field_gemini_uri'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'field-gemini-uri',
],
'internal_label' => [
'#type' => 'item',
'#title' => t('Fedora URI'),
'internal_uri' => [
'#type' => 'link',
'#title' => t("@url", ['@url' => $fedora_uri]),
'#url' => Url::fromUri($fedora_uri),
],
],
];
}
}
}
}
}
/**
* Implements hook_preprocess_views_view_table().
*
* Used for the integer-weight drag-n-drop. Taken almost
* verbatim from the weight module.
*/
function islandora_preprocess_views_view_table(&$variables) {
// Check for a weight selector field.
foreach ($variables['view']->field as $field_key => $field) {
if ($field->options['plugin_id'] == 'integer_weight_selector') {
// Check if the weight selector is on the first column.
$is_first_column = array_search($field_key, array_keys($variables['view']->field)) > 0 ? FALSE : TRUE;
// Add the tabledrag attributes.
foreach ($variables['rows'] as $key => $row) {
if ($is_first_column) {
// If the weight selector is the first column move it to the last
// column, in order to make the draggable widget appear.
$weight_selector = $variables['rows'][$key]['columns'][$field->field];
unset($variables['rows'][$key]['columns'][$field->field]);
$variables['rows'][$key]['columns'][$field->field] = $weight_selector;
}
// Add draggable attribute.
$variables['rows'][$key]['attributes']->addClass('draggable');
}
// The row key identify in an unique way a view grouped by a field.
// Without row number, all the groups will share the same table_id
// and just the first table can be draggable.
$table_id = 'weight-table-' . $variables['view']->dom_id . '-row-' . $key;
$variables['attributes']['id'] = $table_id;
$options = [
'table_id' => $table_id,
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight-selector',
];
drupal_attach_tabledrag($variables, $options);
}
}
}