This repository was archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpersistent_identifiers.module
106 lines (98 loc) · 4.2 KB
/
persistent_identifiers.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
<?php
/**
* @file
* Contains the persistent_identifiers.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_form_node_form_alter().
*/
function persistent_identifiers_form_node_form_alter(&$form, FormStateInterface $form_state) {
$config = \Drupal::config('persistent_identifiers.settings');
$entity = $form_state->getFormObject()->getEntity();
$content_type = $entity->bundle();
$allowed_types = $config->get('persistent_identifiers_bundles');
if (!in_array($content_type, $allowed_types, TRUE)) {
return;
}
if (\Drupal::currentUser()->hasPermission('mint persistent identifiers')) {
$minter = \Drupal::service($config->get('persistent_identifiers_minter'));
$pid_type = $minter->getPidType();
$form['persistent_identifiers_node_options'] = [
'#type' => 'fieldset',
'#access' => TRUE,
'#title' => t('Persistent Identifier'),
'#weight' => 99,
];
$form['persistent_identifiers_node_options']['mint_and_persist'] = [
'#type' => 'checkbox',
'#title' => t('Create @pid_type', ['@pid_type' => $pid_type]),
'#attributes' => [
'id' => 'persistent_identifiers_mint_and_persist',
],
];
$form['actions']['submit']['#submit'][] = 'persistent_identifiers_mint_from_form';
}
}
/**
* Submit callback.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function persistent_identifiers_mint_from_form(array &$form, FormStateInterface $form_state) {
$config = \Drupal::config('persistent_identifiers.settings');
$entity = $form_state->getFormObject()->getEntity();
$content_type = $entity->bundle();
$allowed_types = $config->get('persistent_identifiers_bundles');
$mint_and_persist = $form_state->getValue('mint_and_persist', FALSE);
if ($mint_and_persist && $entity->getEntityTypeId() == 'node' && in_array($content_type, $allowed_types, TRUE)) {
$minter = \Drupal::service($config->get('persistent_identifiers_minter'));
$pid = $minter->mint($entity, $form_state);
if (is_null($pid)) {
\Drupal::logger('persistent_identifiers')->warning(t("Persistent identifier not created for node @nid.", ['@nid' => $entity->id()]));
\Drupal::messenger()->addWarning(t("Problem creating persistent identifier for this node. Details are available in the Drupal system log."));
return;
}
$persister = \Drupal::service($config->get('persistent_identifiers_persister'));
if ($persister->persist($entity, $pid)) {
\Drupal::logger('persistent_identifiers')->info(t("Persistent identifier %pid created for node @nid.", ['%pid' => $pid, '@nid' => $entity->id()]));
\Drupal::messenger()->addStatus(t("Persistent identifier %pid created for this node.", ['%pid' => $pid]));
}
else {
\Drupal::logger('persistent_identifiers')->warning(t("Persistent identifier not created for node @nid.", ['@nid' => $entity->id()]));
\Drupal::messenger()->addWarning(t("Problem creating persistent identifier for this node. Details are available in the Drupal system log."));
}
}
}
/**
* Implements hook_jsonld_alter_normalized_array().
*/
function persistent_identifiers_jsonld_alter_normalized_array(EntityInterface $entity, array &$normalized, array $context) {
if ($entity->getEntityTypeId() == 'node') {
$config = \Drupal::config('persistent_identifiers.settings');
$map_to_schema_sameas = $config->get('persistent_identifiers_map_to_schema_sameas');
$resolver_base_url = $config->get('persistent_identifiers_resolver_base_url');
if (!$map_to_schema_sameas) {
return;
}
$doi_field_name = $config->get('persistent_identifiers_target_field');
if ($entity->hasField($doi_field_name)) {
$doi_field_values = $entity->get($doi_field_name)->getValue();
if (array_key_exists('value', $doi_field_values[0])) {
$doi = $doi_field_values[0]['value'];
}
}
if (isset($normalized['@graph'])) {
if (strlen($doi) && !is_array($normalized["@graph"])) {
$normalized['@graph'] = [$normalized['@graph']];
}
$normalized['@graph'][0]['http://schema.org/sameAs'][] = [
'@id' => $resolver_base_url . $doi,
];
}
}
}