-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathmetastore.theme.inc
150 lines (138 loc) · 4.08 KB
/
metastore.theme.inc
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
<?php
/**
* @file
* Theme variables for metadata template.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Decode Metadata JSON blob into useable theme variables.
*/
function metastore_preprocess_node__data(&$variables) {
$json = $variables['node']->get('field_json_metadata')->value;
$rows = [];
$metadata = json_decode($json);
$variables['metastore'] = $metadata;
if (property_exists($metadata, 'description')) {
$variables['dataset']['description'] = $metadata->description;
}
if (property_exists($metadata, 'theme')) {
$variables['dataset']['themes'] = $metadata->theme;
}
if (property_exists($metadata, 'keyword')) {
$variables['dataset']['keywords'] = $metadata->keyword;
}
if (property_exists($metadata, 'contactPoint')) {
$variables['dataset']['contactPoint']["name"] = [
'#markup' => $metadata->contactPoint->fn,
];
$variables['dataset']['contactPoint']["email"] = [
'#markup' => $metadata->contactPoint->hasEmail,
];
$formatted_contact_point = metastore_format_contactpoint($metadata->contactPoint);
$rows = [
[
"contactPoint",
\Drupal::service('renderer')->render($formatted_contact_point),
],
];
}
if (property_exists($metadata, 'accessLevel')) {
$variables['dataset']['accessLevel'] = [
'#markup' => $metadata->accessLevel,
];
}
if (property_exists($metadata, 'identifier')) {
$variables['dataset']['identifier'] = [
'#markup' => $metadata->identifier,
];
}
if (property_exists($metadata, 'issued')) {
$variables['dataset']['issued'] = $metadata->issued;
}
if (property_exists($metadata, 'modified')) {
$variables['dataset']['modified'] = $metadata->modified;
}
if (property_exists($metadata, 'license')) {
$variables['dataset']['license'] = [
'#markup' => $metadata->license,
];
}
if (property_exists($metadata, 'spatial')) {
$variables['dataset']['spatial'] = [
'#markup' => $metadata->spatial,
];
}
if (property_exists($metadata, 'publisher')) {
$rows[] = ["publisher", $metadata->publisher->name];
$variables['dataset']['publisher'] = [
'#markup' => $metadata->publisher->name,
];
}
foreach ($metadata as $property => $value) {
if (metastore_use_in_table($property, $value)) {
$rows[] = [$property, $value];
}
}
$variables['dataset']['metadata_table'] = [
'#type' => 'table',
'#caption' => t('Additional metadata'),
'#header' => [t("Key"), t("Value")],
'#rows' => $rows,
];
if (!empty($metadata->distribution)) {
foreach ($metadata->distribution as $d) {
$rows = [];
foreach ($d as $property => $value) {
if (ctype_alnum(substr($property, 0, 1))) {
$rows[$property] = [
'#markup' => $value,
];
}
}
// Fallback to display file path for link title.
if (isset($rows['downloadURL']) && !property_exists($d, 'title')) {
$rows['title'] = $rows['downloadURL'];
}
$variables['dataset']['distributions'][] = $rows;
}
}
}
/**
* Format email into a valid Url object.
*/
function metastore_create_mailto_link($email) {
$lower_case_email = strtolower($email);
if (!str_starts_with($lower_case_email, 'mailto:')) {
$lower_case_email = 'mailto:' . strtolower($email);
}
if (Url::fromUri($lower_case_email)) {
return Url::fromUri($lower_case_email);
}
return $lower_case_email;
}
/**
* Format contact information name and link.
*/
function metastore_format_contactpoint($contactpoint) {
$str = $contactpoint->fn;
if ($contactpoint->hasEmail) {
$mailto_link = metastore_create_mailto_link($contactpoint->hasEmail);
$str .= ' (' . Link::fromTextAndUrl(t('email'), $mailto_link)->toString() . ')';
}
return ['#markup' => $str];
}
/**
* Build table using only the string data, skipping description and title.
*/
function metastore_use_in_table($property, $value) {
if (in_array($property, ["description", "title"])) {
return FALSE;
}
if (!ctype_alnum(substr($property, 0, 1))) {
return FALSE;
}
if (is_string($value)) {
return TRUE;
}
}