-
Notifications
You must be signed in to change notification settings - Fork 31
/
controlled_access_terms.module
232 lines (214 loc) · 9.21 KB
/
controlled_access_terms.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
<?php
/**
* @file
* Controlled Access Terms stuff.
*/
use Drupal\controlled_access_terms\EDTFConverter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\jsonld\Normalizer\NormalizerBase;
/**
* Implements hook_rdf_namespaces().
*/
function controlled_access_terms_rdf_namespaces() {
return [
'wgs84_pos' => 'http://www.w3.org/2003/01/geo/wgs84_pos#',
'org' => 'https://www.w3.org/TR/vocab-org/#org:',
'xs' => 'http://www.w3.org/2001/XMLSchema#',
];
}
/**
* Implements hook_jsonld_alter_normalized_array().
*/
function controlled_access_terms_jsonld_alter_normalized_array(EntityInterface $entity, array &$normalized, array $context) {
if (isset($normalized['@graph']) && is_array($normalized['@graph'])) {
foreach ($entity->getFieldDefinitions() as $field => $field_definition) {
if (!empty($entity->get($field)->getValue())) {
if ($field_definition->getType() == 'typed_relation') {
foreach ($entity->get($field)->getValue() as $value) {
if (empty($value['target_id'])) {
\Drupal::logger('controlled_access_terms')->warning("Missing target entity for %field in %entity_type/%id (%bundle)",
[
'%field' => $field,
'%entity_type' => $entity->getEntityTypeId(),
'%bundle' => $entity->bundle(),
'%id' => $entity->id(),
]);
continue;
}
$predicate = NormalizerBase::escapePrefix($value['rel_type'], $context['namespaces']);
$referenced_entity = \Drupal::entityTypeManager()->getStorage($field_definition->getSetting('target_type'))->load($value['target_id']);
if (empty($referenced_entity)) {
\Drupal::logger('controlled_access_terms')->warning("Invalid target entity for %field in %entity_type/%id (%bundle)",
[
'%field' => $field,
'%entity_type' => $entity->getEntityTypeId(),
'%bundle' => $entity->bundle(),
'%id' => $entity->id(),
]);
continue;
}
// We are assuming the first graph is the one corresponding
// to the node/taxonomy_term we are modifying.
$normalized['@graph'][0][$predicate][] = [
'@id' => $context['utils']->getEntityUri($referenced_entity),
];
}
}
elseif ($field_definition->getType() == 'edtf') {
// Get the predicate to look in.
foreach ($context['current_entity_rdf_mapping']->get('fieldMappings')[$field]['properties'] as $predicate) {
// Find the predicate value that needs updating.
$predicate_normalized = NormalizerBase::escapePrefix($predicate, $context['namespaces']);
foreach ($normalized['@graph'][0][$predicate_normalized] as $index => $value) {
// Clean the date of ranges, uncertainty, and approximations.
// Have to remap the array from '@value' to 'value'.
$normalized_date = EDTFConverter::dateIso8601Value(['value' => $value['@value']]);
// Determine which type to use.
$date_type = NormalizerBase::escapePrefix('xs:date', $context['namespaces']);
switch (count(explode('-', $normalized_date))) {
case 1:
$date_type = NormalizerBase::escapePrefix('xs:gYear', $context['namespaces']);
break;
case 2:
$date_type = NormalizerBase::escapePrefix('xs:gYearMonth', $context['namespaces']);
break;
}
$normalized['@graph'][0][$predicate_normalized][] = [
'@value' => $normalized_date,
'@type' => $date_type,
];
}
}
}
}
}
}
}
/**
* Implements hook_jsonld_field_mappings().
*/
function controlled_access_terms_jsonld_field_mappings() {
return [
"authority_link" => [
"@type" => "xsd:anyURI",
],
];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function controlled_access_terms_form_field_ui_field_storage_add_form_alter(&$form, &$form_state, $form_id) {
$form['#attached']['library'][] = 'controlled_access_terms/controlled_access_terms.icons';
}
/**
* Implements hook_field_info_entity_type_ui_definitions_alter().
*/
function controlled_access_terms_field_info_entity_type_ui_definitions_alter(array &$ui_definitions, string $entity_type_id) {
// Put the generic typed relation field last and name it 'Other'.
if (isset($ui_definitions['typed_relation'])) {
$ui_definitions['typed_relation']['label'] = t('Other');
$ui_definitions['typed_relation']['weight'] = 10;
}
// Undo some shenanigans done by the Media module to the media option.
if (isset($ui_definitions['field_ui:typed_relation:media'])) {
$ui_definitions['field_ui:typed_relation:media']['category'] = 'typed_relation';
$ui_definitions['field_ui:typed_relation:media']['description'] = 'A reference field with a configurable type selector.';
$ui_definitions['field_ui:typed_relation:media']['weight'] = 0;
}
// Put taxonomy term first in the option list.
if (isset($ui_definitions['field_ui:typed_relation:taxonomy_term'])) {
$ui_definitions['field_ui:typed_relation:taxonomy_term']['weight'] = -10;
}
}
/**
* Update EDTF fields from the 2012 draft to match the 2018 spec.
*/
function controlled_access_terms_update_8003() {
$db = \Drupal::database();
// Find all the fields using edtf.
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('field.storage.') as $field_storage_config_name) {
$field_storage_config = $config_factory->get($field_storage_config_name);
if ($field_storage_config->get('type') === 'edtf') {
// Run through each update. Make sure 'unknown' is updated before 'u'.
$updates = [
'open' => '..',
'unknown' => '',
'y' => 'Y',
'u' => 'X',
'?~' => '%',
'~?' => '%',
];
foreach ($updates as $old => $new) {
$db->update($field_storage_config->get('entity_type') . '__' . $field_storage_config->get('field_name'))
->expression($field_storage_config->get('field_name') . '_value', 'replace(' . $field_storage_config->get('field_name') . '_value, :old, :new)', [
':old' => $old,
':new' => $new,
])
->execute();
}
}
}
}
/**
* Change fields using the EDTF Widget to the new EDTF Field Type.
*/
function controlled_access_terms_update_8002() {
// Ensure the new EDTF plugins can be found.
\Drupal::service('plugin.manager.field.field_type')->clearCachedDefinitions();
// Find all the fields using the text_edtf widget via form configs.
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('core.entity_form_display.') as $entity_form_display_config_name) {
$entity_form_display = $config_factory->getEditable($entity_form_display_config_name);
$fields = $entity_form_display->get('content');
foreach ($fields as $field_name => $field_settings) {
if (isset($field_settings['type']) && $field_settings['type'] === 'text_edtf') {
// Update this form setting.
$entity_form_display->set("content.$field_name.type", 'edtf_default');
// Update the field setting.
if (!$fields = \Drupal::service('entity_type.manager')->getStorage('field_config')->loadByProperties(['field_name' => $field_name])) {
continue;
}
else {
foreach ($fields as $field) {
$new_field = $field->toArray();
$new_field['field_type'] = 'edtf';
$new_field = FieldConfig::create($new_field);
$new_field->original = $new_field;
$new_field->enforceIsNew(FALSE);
$new_field->save();
}
}
// Update the field storage setting.
if (!$field_storage_configs = \Drupal::service('entity_type.manager')->getStorage('field_storage_config')->loadByProperties(['field_name' => $field_name])) {
continue;
}
else {
foreach ($field_storage_configs as $field_storage) {
$new_field_storage = $field_storage->toArray();
$new_field_storage['type'] = 'edtf';
$new_field_storage = FieldStorageConfig::create($new_field_storage);
$new_field_storage->original = $new_field_storage;
$new_field_storage->enforceIsNew(FALSE);
$new_field_storage->save();
}
}
}
}
$entity_form_display->save(TRUE);
}
// Find display configs.
foreach ($config_factory->listAll('core.entity_view_display.') as $entity_view_display_config_name) {
$entity_view_display = $config_factory->getEditable($entity_view_display_config_name);
$fields = $entity_view_display->get('content');
foreach ($fields as $field_name => $field_settings) {
if (isset($field_settings['type']) && ($field_settings['type'] === 'text_edtf_human' || $field_settings['type'] === 'text_edtf_iso8601')) {
// Update this view setting.
$entity_view_display->set("content.$field_name.type", 'edtf_default');
}
}
$entity_view_display->save(TRUE);
}
}