-
Notifications
You must be signed in to change notification settings - Fork 0
/
loqate.install
88 lines (78 loc) · 2.67 KB
/
loqate.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
<?php
/**
* @file
* Contains loqate.install.
*/
use Drupal\key\Entity\Key;
use Drupal\loqate\Form\LoqateApiKeyConfigForm;
/**
* Decoupled Webform so install pca_webform submodule for the moved plugins.
*/
function loqate_update_8001() {
// All sites using this module up until now would have had these plugins.
\Drupal::service('module_installer')->install(['pca_webform']);
return t('PCA Webform installed.');
}
/**
* Install new dependency on the key module.
*/
function loqate_update_8002() {
// All sites using this module up until now will need this update.
\Drupal::service('module_installer')->install(['key']);
return t('Key installed.');
}
/**
* Convert old key string value into a key config entity.
*/
function loqate_update_8003() {
// Move the string value into a key config entity if any.
$loqate_config = \Drupal::configFactory()->getEditable('loqate.loqateapikeyconfig');
$old_key_value = $loqate_config->get(LoqateApiKeyConfigForm::DEFAULT_API_KEY);
if (!empty($old_key_value)) {
// Create a new entity key with the config key provider.
$key_entity = new Key([
'id' => LoqateApiKeyConfigForm::DEFAULT_API_KEY,
'label' => 'Loqate API key',
'key_provider' => 'config',
'key_input' => 'text_field',
], 'key');
$key_entity->setKeyValue($old_key_value);
$key_entity->save();
// Set the config id now as the key value in config.
$loqate_config
->set(LoqateApiKeyConfigForm::DEFAULT_API_KEY, LoqateApiKeyConfigForm::DEFAULT_API_KEY)
->save();
return t('Key config entity successfully created.');
}
return t('No old key value found.');
}
/**
* Assign administer loqate api permission to roles.
*/
function loqate_update_8004() {
$old_perm = 'access administration pages';
$new_perm = 'administer loqate api';
$did_update = FALSE;
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = \Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple();
foreach ($roles as $role) {
if ($role->hasPermission($old_perm)) {
$role->grantPermission($new_perm)->save();
$did_update = TRUE;
}
}
return $did_update ? t('Loqate permission has been granted') : t('No permission updaes required');
}
/**
* Updated config to capture the field mapping enabled boolean.
*/
function loqate_update_8005() {
// Register enabled as TRUE for pre-existing config values.
$pca_address_config = \Drupal::configFactory()->getEditable('loqate.settings');
$pca_fields = $pca_address_config->get('pca_fields');
foreach ($pca_fields as $i => $pca_field) {
$pca_fields[$i]['enabled'] = TRUE;
}
// Set new value in config.
$pca_address_config->set('pca_fields', $pca_fields)->save();
}