-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhm_newsletter.install
109 lines (95 loc) · 3.5 KB
/
hm_newsletter.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
<?php
/**
* @file
* Update hooks for for HM Newsletter module.
*/
use Drupal\block\Entity\Block;
/**
* Add new configuration options.
*/
function hm_newsletter_update_8001() {
// Set configuration to display "datenschutzeinwilligung" and "privacy", since
// that was previous behaviour.
\Drupal::configFactory()
->getEditable('hm_newsletter.settings')
->set('hm_displayed_agreements', ['datenschutzeinwilligung', 'privacy'])
->save();
}
/**
* Remove "hm_displayed_agreements" global config setting.
*
* Update existing block configs to introduce new options.
*/
function hm_newsletter_update_8002() {
// Read hm_displayed_agreements settings.
$configFactory = \Drupal::configFactory();
$hmNewsletterSettings = $configFactory->getEditable('hm_newsletter.settings');
$hmDisplayedAgreements = $hmNewsletterSettings->get('hm_displayed_agreements');
// Find/load all existing block instances.
$blocks = Block::loadMultiple();
// Mapping for labels.
$labelMappings = [
'firstname' => 'Vorname',
'name' => 'Nachname',
'zipcode' => 'Postleitzahl',
'location' => 'Land',
'birthdate' => 'Geburtsdatum',
];
/* @var \Drupal\block\Entity\Block $block */
foreach ($blocks as $block) {
if ($block->getPlugin()->getBaseId() === 'hm_newsletter_block') {
$blockConfig = $configFactory->getEditable('block.block.' . $block->getOriginalId());
if ($hmDisplayedAgreements) {
// Set privacy->required, optin->optional (dependant on global config).
if (in_array('privacy', $hmDisplayedAgreements) ||
in_array('datenschutzeinwilligung', $hmDisplayedAgreements)) {
$blockConfig->set('settings.privacy', 'required')->save(TRUE);
}
if (in_array('anspracheerlaubnis', $hmDisplayedAgreements) ||
in_array('optin', $hmDisplayedAgreements) ||
in_array('opt-in', $hmDisplayedAgreements)) {
$blockConfig->set('settings.optin', 'optional')->save(TRUE);
}
}
// Set other basic values.
foreach ($blockConfig->get('settings') as $setting => $value) {
// Existing configs.
switch ($setting) {
case 'title':
// In case of title, migrate setting to "salutation".
$isVisible = $blockConfig->get('settings.' . $setting);
$blockConfig->set('settings.salutation', [
'is_visible' => $isVisible,
'label_display' => ['label' => 'label'],
'label_text' => 'Anrede',
]);
$blockConfig->clear('settings.' . $setting);
break;
case 'firstname':
case 'name':
case 'zipcode':
case 'location':
case 'birthdate':
$isVisible = $blockConfig->get('settings.' . $setting);
$blockConfig->set('settings.' . $setting, [
'is_visible' => $isVisible,
'label_display' => ['label' => 'label'],
'label_text' => $labelMappings[$setting],
]);
break;
}
}
// New configs - default value when no source is provided.
$blockConfig->set('settings.source', 'web');
$blockConfig->set('settings.submit_label', 'Anmelden');
$blockConfig->set('settings.email', [
'is_visible' => '1',
'label_display' => ['label' => 'label'],
'label_text' => 'E-Mail',
]);
$blockConfig->save(TRUE);
}
}
// Remove hm_displayed_agreements settings.
$hmNewsletterSettings->clear('hm_displayed_agreements')->save();
}