This repository has been 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
/
hdl.module
175 lines (165 loc) · 6.32 KB
/
hdl.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
<?php
/**
* @file
* Contains the hdl.module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_alter().
*
* This example adds a field to the form at
* /admin/config/persistent_identifiers/settings that is specific to the
* minter provided by this module. The value of the field is saved in the
* submit callback below.
*
* Also, note that the list of minters at /admin/config/persistent_identifiers/settings
* is derived from minters defined in minter modules' .services.yml files, not by
* code in hook_form_alter().
*/
function hdl_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
if ($form_id == 'persistent_identifiers_admin_settings') {
$form['hdl_begin_label'] = array(
'#type' => 'markup',
'#markup' => '<label><b>Handle</b></label><hr>',
);
$config = \Drupal::config('hdl.settings');
$form['hdl_prefix'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle identifier prefix',
'#default_value' => $config->get('hdl_prefix'),
'#description' => t("This string will be prefix used for the handle. An example handle looks like this: 0.NA/20.500.12345"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
'#required' => TRUE,
];
/*
$form['hdl_qualifier'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle identifier qualifier',
'#default_value' => $config->get('hdl_qualifier'),
'#description' => t("This string will be prepended to identifier to make the unique handle. You might use it to distinguish different types of objects, like R.C. for Repository Collection and R.O. for Repository Object and R.M. for Repository Media. So your handle might look like https://handle.net/2286.9/R.O.7"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];
*/
$form['hdl_admin_handle'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Admin Handle',
'#default_value' => $config->get('hdl_admin_handle'),
'#description' => t("The Admin Handle under your prefix"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
'#required' => TRUE,
];
$form['hdl_admin_index'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Admin Index',
'#default_value' => $config->get('hdl_admin_index'),
'#description' => t("The index for your Admin Handle"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
'#required' => TRUE,
];
/*
$form['hdl_handle_permissions'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle Permissions',
'#default_value' => $config->get('hdl_handle_permissions'),
'#description' => t("The permissions to have to the admin user on the handle"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];
*/
$form['hdl_handle_api_endpoint'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle API Endpoint',
'#default_value' => $config->get('hdl_handle_api_endpoint'),
'#description' => t("The API endpoint for your handle server"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
'#required' => TRUE,
];
$form['hdl_admin_private_key'] = [
'#type' => 'textarea',
'#title' => 'Private Key',
'#default_value' => $config->get('hdl_admin_private_key'),
'#required' => TRUE,
];
/*
$form['hdl_handle_basic_auth_password'] = [
'#type' => 'password',
// '#access' => TRUE,
'#title' => 'Handle Basic Auth Password',
'#default_value' => $config->get('hdl_handle_basic_auth_password'),
'#description' => t("The password to be used with the admin handle and index to authenticate for the API"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];
*/
$form['hdl_end_label'] = array(
'#type' => 'markup',
'#markup' => '<br><br>',
);
$form['#submit'][] = 'hdl_submit';
}
}
/**
* Submit callback.
*
* Saves the value of the minter-specific field defined in the implementation
* of hook_form_alter() above.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function hdl_submit(array &$form, FormStateInterface $form_state) {
$existing_config = \Drupal::config('hdl.settings');
$admin_handle = $form_state->getValue('hdl_admin_handle', $existing_config->get('hdl_admin_handle'));
$prefix = $form_state->getValue('hdl_prefix', $existing_config->get('hdl_prefix'));
//$qualifier = $form_state->getValue('hdl_qualifier', $existing_config->get('hdl_qualifier'));
$api_endpoint = $form_state->getValue('hdl_handle_api_endpoint', $existing_config->get('hdl_handle_api_endpoint'));
$index = $form_state->getValue('hdl_admin_index', $existing_config->get('hdl_admin_index'));
$permissions = $form_state->getValue('hdl_handle_permissions', $existing_config->get('hdl_handle_permissions'));
//$password = $form_state->getValue('hdl_handle_basic_auth_password', $existing_config->get('hdl_handle_basic_auth_password'));
$private_key = $form_state->getValue('hdl_admin_private_key', $existing_config->get('hdl_admin_private_key'));
$config_factory = \Drupal::configFactory();
$config_factory->getEditable('hdl.settings')
->set('hdl_admin_handle', trim($admin_handle))
->set('hdl_prefix', trim($prefix))
//->set('hdl_qualifier', trim($qualifier))
->set('hdl_handle_api_endpoint', trim($api_endpoint))
->set('hdl_admin_index', trim($index))
->set('hdl_handle_permissions', trim($permissions))
//->set('hdl_handle_basic_auth_password', trim($password))
->set('hdl_admin_private_key', trim($private_key))
->save();
}