-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtripal_jbrowse.module
90 lines (82 loc) · 3.03 KB
/
tripal_jbrowse.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
<?php
/**
* @file
* Provides a jbrowse instance entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\tripal_jbrowse\Entity\JbrowseInstance;
use Drupal\user\UserInterface;
/**
* Implements hook_theme().
*/
function tripal_jbrowse_theme() {
return [
'jbrowse_instance' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for jbrowse instance templates.
*
* Default template: jbrowse-instance.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the jbrowse instance information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_jbrowse_instance(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['#attached']['library'][] = 'tripal_jbrowse/react';
$variables['#attached']['library'][] = 'tripal_jbrowse/jbrowse';
$variables['#attached']['library'][] = 'tripal_jbrowse/createJBrowseLinearGenomeView';
// Grab the URL and the starting location (sequence coordinates) of our JBrowse instance
// from the content entity
// We are taking this approach rather than through content so that we are not vulnerable
// to a user disabling it through the managed display UI
$instance = $variables['elements']['#jbrowse_instance'];
$url = $instance->get('jbrowse_url')->getValue();
$variables['#attached']['drupalSettings']['jbrowseUrl'] = $url[0]['value'];
// Location is not required so check that there is a value first
if ($instance->get('location')->getValue()) {
$location = $instance->get('location')->getValue();
$variables['#attached']['drupalSettings']['location'] = $location[0]['value'];
}
// Add our URL as an ajax trusted URL
$variables['#attached']['drupalSettings']['ajaxTrustedUrl'][$url[0]['value']] = TRUE;
}
/**
* Implements hook_user_cancel().
*/
function tripal_jbrowse_user_cancel($edit, UserInterface $account, $method) {
switch ($method) {
case 'user_cancel_reassign':
// Anonymize jbrowse instances.
$storage = \Drupal::entityTypeManager()->getStorage('jbrowse_instance');
$jbrowse_instance_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
foreach ($storage->loadMultiple($jbrowse_instance_ids) as $jbrowse_instance) {
$jbrowse_instance->setOwnerId(0);
$jbrowse_instance->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function tripal_jbrowse_user_predelete(UserInterface $account) {
// Delete jbrowse instances.
$storage = \Drupal::entityTypeManager()->getStorage('jbrowse_instance');
$jbrowse_instance_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
$jbrowse_instances = $storage->loadMultiple($jbrowse_instance_ids);
$storage->delete($jbrowse_instances);
}