forked from activelamp/purl-d8
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpurl.module
211 lines (176 loc) · 6.35 KB
/
purl.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
<?php
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\purl\Entity\Provider;
use Drupal\node\NodeTypeInterface;
use Drupal\Core\Form;
function purl_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() !== 'menu_link_content') {
return;
}
$fields = [];
/* @todo Remove this; we don't need these extra fields anymore */
$fields['purl_provider'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('PURL provider'))
->setName('purl_provider')
->setRevisionable(false)
->setTranslatable(false)
->setDescription('PURL provider')
->setSettings([
'target_type' => 'purl_provider',
'default_value' => null,
]);
$fields['purl_modifier'] = BaseFieldDefinition::create('string')
->setLabel(t('PURL modifier'))
->setName('purl_modifier')
->setRevisionable(false)
->setDescription('PURL modififer')
->setSettings([
'default_value' => null,
'max_length' => 255,
]);
$fields['purl_strip_context'] = BaseFieldDefinition::create('boolean')
->setLabel(t('PURL strip context'))
->setName('purl_strip_context')
->setRevisionable(false)
->setDescription('PURL strip context')
->setSettings([
'default_value' => false,
]);
return $fields;
}
function purl_form_menu_edit_form_alter(&$form, $form_state) {
$providers = Provider::loadMultiple();
$options = [null => '-- None --'];
foreach ($providers as $provider) {
$modifiers= [];
foreach ($provider->getProviderPlugin()->getModifierData() as $modifier => $value) {
$modifiers[$provider->id() . ':' . $modifier] = $modifier;
}
$options[$provider->getLabel()] = $modifiers;
}
$form['purl'] = [
'#tree' => true,
'#type' => 'fieldset',
'#title' => 'Persistent URLs',
'#collapsible' => false,
'#collapsed' => false,
];
$form['purl']['purl_provider'] = [
'#type' => 'select',
'#title' => 'PURL provider',
'#value' => null,
'#options' => $options
];
}
function purl_form_menu_link_content_menu_link_content_form_alter(&$form, FormStateInterface $form_state) {
$providers = Provider::loadMultiple();
$menuLinkEntity = $form_state->getBuildInfo()['callback_object']->getEntity();
$selected = null;
if (isset($menuLinkEntity->purl_strip_context[0]) && $menuLinkEntity->purl_strip_context[0]->getValue()['value'] == 1) {
$selected = '__STRIP__';
} elseif (isset($menuLinkEntity->purl_modifier[0]) && isset($menuLinkEntity->purl_provider[0])) {
$selected = sprintf(
'%s:%s',
$menuLinkEntity->purl_provider[0]->getValue()['target_id'],
$menuLinkEntity->purl_modifier[0]->getValue()['value']
);
}
$options = [null => '-- Maintain context --', '__STRIP__' => 'Strip PURL context'];
foreach ($providers as $provider) {
$modifiers= [];
foreach ($provider->getProviderPlugin()->getModifierData() as $modifier => $value) {
$modifiers[$provider->id() . ':' . $modifier] = $modifier;
}
$options[$provider->getLabel()] = $modifiers;
}
$form['purl'] = [
'#tree' => true,
'#type' => 'fieldset',
'#title' => 'Persistent URLs',
'#collapsible' => false,
'#collapsed' => false,
];
$form['purl']['purl_modifier'] = [
'#type' => 'select',
'#title' => 'PURL modifier',
'#options' => $options,
'#default_value' => $selected
];
$form['#entity_builders'][] = 'purl_form_menu_link_content_menu_link_content_form_builder';
}
function purl_form_menu_link_content_menu_link_content_form_builder($entity_type, $type, &$form, FormStateInterface $form_state) {
$menuLinkEntity = $form_state->getBuildInfo()['callback_object']->getEntity();
if ($form_state->getValue('purl') && $form_state->getValue('purl')['purl_modifier']) {
$purlSpec = $form_state->getValue('purl')['purl_modifier'];
$tokens = explode(':', $purlSpec);
if (count($tokens) == 1) { // Must be `__STRIP__`
$provider = null;
$modifier = null;
$menuLinkEntity->set('purl_strip_context', true);
$form_state->getValue('link')[0]['options']['purl_context'] = false;
} else {
$providerKey = array_shift($tokens);
$modifier = implode(':', $tokens);
$provider = Provider::load($providerKey);
$menuLinkEntity->set('purl_strip_context', false);
$form_state->getValue('link')[0]['options']['purl_context'] =
['provider' => $providerKey, 'modifier' => $modifier];
}
$menuLinkEntity->set('purl_provider', $provider);
$menuLinkEntity->set('purl_modifier', $modifier);
} else {
$menuLinkEntity->set('purl_strip_context', false);
$menuLinkEntity->set('purl_modifier', null);
$menuLinkEntity->set('purl_provider', null);
}
}
function purl_form_alter(&$form, $form_state) {
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function purl_form_node_type_form_alter(&$form, $form_state) {
/* @var Drupal\node\NodeInterface $node */
$node_type = $form_state->getFormObject()->getEntity();
$form['purl'] = array(
'#type' => 'details',
'#title' => t('PURL settings'),
'#group' => 'additional_settings',
);
$form['purl']['keep_context'] = array(
'#type' => 'checkbox',
'#title' => t('Keep context of the node.'),
'#default_value' => $node_type->getThirdPartySetting('purl', 'keep_context', 0),
'#description' => t('This allows a node-type PURL context.'),
);
$form['#entity_builders'][] = 'purl_form_node_type_form_builder';
}
/**
* Entity form builder for the node type form to map some values to third party
* settings
*/
function purl_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, $form_state) {
if ($form_state->getValue('keep_context') == 1) {
$type->setThirdPartySetting('purl', 'keep_context', $form_state->getValue('keep_context'));
return;
}
$type->unsetThirdPartySetting('purl', 'keep_context');
}
/**
* Implements hook_preprocess_html().
*/
function purl_preprocess_html(&$vars) {
$init = \Drupal::requestStack();
$request = $init->getCurrentRequest();
$modifiers = $request->attributes->get('purl.matched_modifiers', []);
foreach ($modifiers as $modifier) {
foreach ($modifier as $key => $item) {
if (!is_object($item)) {
$vars['attributes']['class'][] = 'purl-' . $key . '-' . $item;
}
}
}
}