forked from dpi/rng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rng.install
322 lines (291 loc) · 10.5 KB
/
rng.install
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* @file
* Contains install and update functions for RNG.
*/
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\field\Entity\FieldConfig;
use Drupal\rng\Entity\RegistrantType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rng\Entity\EventType;
use Drupal\rng\Entity\EventTypeRule;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Add default rules to event types.
*/
function rng_update_8001() {
// Refresh entity type cache will show rng_event_type_rule configs.
\Drupal::entityTypeManager()->clearCachedDefinitions();
// Clear config schema cache to prevent complaining about plugin schemas
// saved in the rules below.
\Drupal::service('config.typed')->clearCachedDefinitions();
$config_factory = \Drupal::configFactory();
foreach (EventType::loadMultiple() as $event_type) {
/** @var \Drupal\rng\EventTypeInterface $event_type */
$entity_type_id = $event_type->getEventEntityTypeId();
$bundle = $event_type->getEventBundle();
if (!$config_factory->listAll('rng.rule.' . $entity_type_id . '.' . $bundle)) {
$rule = EventTypeRule::create([
'trigger' => 'rng_event.register',
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'machine_name' => 'user_role',
]);
$rule->setCondition('role', [
'id' => 'rng_user_role',
'roles' => [],
]);
$rule->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => [
'create' => TRUE,
],
],
]);
$rule->save();
// Registrant.
$rule = EventTypeRule::create([
'trigger' => 'rng_event.register',
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'machine_name' => 'registrant',
]);
$rule->setCondition('identity', [
'id' => 'rng_registration_identity',
]);
$rule->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => [
'view' => TRUE,
'update' => TRUE,
],
],
]);
$rule->save();
// Event managers.
$rule = EventTypeRule::create([
'trigger' => 'rng_event.register',
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'machine_name' => 'event_manager',
]);
$rule->setCondition('operation', [
'id' => 'rng_event_operation',
'operations' => ['manage event' => TRUE],
]);
$rule->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => [
'create' => TRUE,
'view' => TRUE,
'update' => TRUE,
'delete' => TRUE,
],
],
]);
$rule->save();
}
}
}
/**
* Convert registrant entity to one with bundles.
*/
function rng_update_8002() {
$entity_manager = \Drupal::entityTypeManager();
$config_factory = \Drupal::configFactory();
$update_manager = \Drupal::entityDefinitionUpdateManager();
// Install 'registrant_type' config entity.
$entity_manager->clearCachedDefinitions();
$type = $entity_manager->getDefinition('registrant_type');
$update_manager->installEntityType($type);
// Clear config cache so new registrant type saves.
\Drupal::service('config.typed')->clearCachedDefinitions();
// Install 'type' field.
$field_definition = BaseFieldDefinition::create('entity_reference')
->setLabel('Registrant type')
->setSetting('target_type', 'registrant_type');
$update_manager->installFieldStorageDefinition('type', 'registrant', 'registrant', $field_definition);
// Create the first registrant_type entity which will re-capture any
// pre-existing fields from the formerly bundle-less entity.
$default_registrant_type_id = 'registrant';
$registrant_type = RegistrantType::create([
'id' => $default_registrant_type_id,
'label' => 'Registrant',
]);
$registrant_type->save();
// Re-calculate dependencies by re-saving fields.
$config_names = $config_factory->listAll('field.field.registrant.registrant.');
foreach ($config_names as $name) {
// Remove prefix.
$id = str_replace('field.field.', '', $name);
$entity = FieldConfig::load($id);
$entity->save();
}
}
/**
* Update event types with new registrant and people type configuration.
*/
function rng_update_8003() {
/** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
$bundle_info = \Drupal::service('entity_type.bundle.info');
$config_factory = \Drupal::configFactory();
$default_registrant_type_id = 'registrant';
// Generate default people types allowing all enabled identity types.
$people_types = [];
$identity_types = $config_factory->get('rng.settings')->get('identity_types') ?: [];
foreach ($identity_types as $entity_type) {
$bundles = $bundle_info->getBundleInfo($entity_type);
foreach (array_keys($bundles) as $bundle) {
$people_types[] = [
'entity_type' => $entity_type,
'bundle' => $bundle,
'existing' => 1,
'create' => 0,
'entity_form_mode' => 'default',
];
}
}
$config_names = $config_factory->listAll('rng.event_type.');
foreach ($config_names as $name) {
$config = $config_factory->getEditable($name);
// Set default registrant for event types to the new bundle entity created
// in 8002. 8002 + 8003 happen simultaneously.
$config->set('default_registrant', $default_registrant_type_id);
// Permit referencing existing entities for all enabled identity types.
$config->set('people_types', $people_types);
// Add a dependency on the registrant type used for 'default_registrant'.
$id = 'rng.registrant_type.' . $default_registrant_type_id;
$config_dependencies = $config->get('dependencies.config') ?: [];
if (!in_array($id, $config_dependencies)) {
$config_dependencies[] = $id;
$config->set('dependencies.config', $config_dependencies);
}
$config->save();
}
}
/**
* Replace event_type config entity with rng_event_type entity.
*/
function rng_update_8004(&$sandbox) {
$updateManager = \Drupal::entityDefinitionUpdateManager();
$rng_event_type = $updateManager->getEntityType('rng_event_type');
if (empty($rng_event_type)) {
$rng_event_type = new ConfigEntityType([
'id' => 'rng_event_type',
'label' => new TranslatableMarkup('Event type'),
'config_prefix' => 'rng_event_type',
'admin_permission' => 'administer event types',
'entity_keys' => [
'id' => 'id',
'label' => 'label',
],
]);
$updateManager->installEntityType($rng_event_type);
}
$config_factory = \Drupal::configFactory();
// Rename existing configs.
foreach ($config_factory->listAll('rng.event_type.') as $event_type) {
$new_config = str_replace('rng.event_type.', 'rng.rng_event_type.', $event_type);
$config_factory->rename($event_type, $new_config);
// Rule plugins depend on rng.event_type configs.
foreach ($config_factory->listAll('rng.rule.') as $rng_rule) {
$config = $config_factory->getEditable($rng_rule);
$config_dependencies = $config->get('dependencies.config') ?: [];
$indx = array_search($event_type, $config_dependencies);
if ($indx !== FALSE) {
$config_dependencies[$indx] = $new_config;
$config->set('dependencies.config', $config_dependencies);
}
$config->save();
}
}
// Remove old type.
$old_event_type = $updateManager->getEntityType('event_type');
if (!empty($old_event_type)) {
$updateManager->uninstallEntityType($old_event_type);
}
}
/**
* Add a registrant_qty field to registrations.
*/
function rng_update_8201() {
$definition_manager = \Drupal::entityDefinitionUpdateManager();
$registrant_qty_field = BaseFieldDefinition::create('integer')
->setLabel(t('Registrant Qty'))
->setDescription(t('Number of registrants on this registration'))
->setDefaultValue(0)
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$definition_manager->installFieldStorageDefinition('registrant_qty', 'registration', 'registration', $registrant_qty_field);
}
/**
* Add a UID field to registration.
*/
function rng_update_8202() {
$definition_manager = \Drupal::entityDefinitionUpdateManager();
$user_field = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Owner'))
->setDescription(t('The owner of the registration.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\rng\Entity\Registration::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE);
$definition_manager->installFieldStorageDefinition('uid', 'registration', 'registration', $user_field);
}
/**
* Add status field to registrations.
*/
function rng_update_8203() {
$definition_manager = \Drupal::entityDefinitionUpdateManager();
$status = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Confirmed'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 90,
])
->setDisplayConfigurable('form', TRUE);
$definition_manager->installFieldStorageDefinition('status', 'registration', 'registration', $status);
}
/**
* Add Event dynamic entity reference to registrant.
*/
function rng_update_8204() {
$definition_manager = \Drupal::entityDefinitionUpdateManager();
$event = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Event'))
->setDescription(t('The event for the registrant.'))
->setSetting('exclude_entity_types', 'true')
->setSetting('entity_type_ids', ['registrant', 'registration'])
->setDescription(t('The relationship between this registrant and an event.'))
->setRevisionable(TRUE)
->setReadOnly(TRUE);
$definition_manager->installFieldStorageDefinition('event', 'registrant', 'registrant', $event);
}
/**
* Resave all registrants to populate event links.
*/
function rng_update_8205() {
$storage = \Drupal::entityTypeManager()->getStorage('registrant');
$registrants = $storage->loadMultiple(NULL);
foreach ($registrants as $registrant) {
if ($registrant->id() < 73) {
$registrant->delete();
}
$registrant->save();
}
}