-
Notifications
You must be signed in to change notification settings - Fork 0
/
rng.install
182 lines (163 loc) · 5.85 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
<?php
/**
* @file
* Contains install and update functions for RNG.
*/
use Drupal\rng\Entity\EventType;
use Drupal\rng\Entity\EventTypeRule;
/**
* 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 = \Drupal\Core\Field\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 = \Drupal\rng\Entity\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 = \Drupal\field\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();
}
}