-
Notifications
You must be signed in to change notification settings - Fork 0
/
anonymous_subscriptions.install
167 lines (145 loc) · 5.21 KB
/
anonymous_subscriptions.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
<?php
/**
* @file
* Install/update/uninstall functions for the anonymous subscription module.
*/
use Drupal\anonymous_subscriptions\Form\SettingsForm;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\views\Views;
/**
* Implements hook_uninstall().
*/
function anonymous_subscriptions_uninstall() {
Drupal::configFactory()->getEditable(SettingsForm::$configName)->delete();
}
/**
* Implements hook_update_N().
*
* Install missing field entity_id.
*/
function anonymous_subscriptions_update_8901() {
$field_storage_definition = BaseFieldDefinition::create('integer')
->setLabel(t('Subscribed entity id'))
->setDescription(t('The entity id for subscription.'));
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('entity_id', 'anonymous_subscription', 'anonymous_subscription', $field_storage_definition);
}
/**
* Implements hook_update_N().
*
* Marking all existing subscriptions as "entity_type = node".
*/
function anonymous_subscriptions_update_8902() {
\Drupal::database()->update('anonymous_subscription')
->fields(['entity_type' => 'node'])
->condition('entity_type', NULL, 'IS NULL')
->execute();
}
/**
* Implements hook_update_N().
*
* Changing column type to entity_bundle.
*/
function anonymous_subscriptions_update_8903() {
$table_name = 'anonymous_subscription';
$database = \Drupal::database();
/** @var \Drupal\Core\Database\Schema $schema */
$schema = $database->schema();
$messenger = Drupal::messenger();
if ($schema->fieldExists($table_name, 'entity_bundle')) {
$messenger->addMessage(t('Column entity_bundle already exist in table @table', ['@table' => $table_name]));
}
else {
// Create a new BaseFieldDefinition for entity_bundle feild.
$field_storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Subscribed entity bundle'))
->setDescription(t('The entity bundle for subscription.'))
->setSettings([
'max_length' => 255,
]);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('entity_bundle', 'anonymous_subscription', 'anonymous_subscription', $field_storage_definition);
}
if ($schema->fieldExists($table_name, 'type')) {
// Storing the existing values from type field.
$type_values = $database->select($table_name)
->fields($table_name, ['id', 'type'])
->execute()
->fetchAll();
// Updating values for entity_bundle field with values from type field.
foreach ($type_values as $row) {
$database->update($table_name)
->fields(['entity_bundle' => $row->type])
->condition('id', $row->id)
->execute();
}
// Clear out the values (required to uninstall a field).
$database->update($table_name)
->fields(['type' => NULL])
->execute();
// Uninstalling type field.
$definition_manager = \Drupal::entityDefinitionUpdateManager();
$field_storage_definition = $definition_manager->getFieldStorageDefinition('type', 'anonymous_subscription');
$definition_manager->uninstallFieldStorageDefinition($field_storage_definition);
}
else {
Drupal::messenger()->addMessage(t('Column type does not exist in table @table', ['@table' => $table_name]));
}
}
/**
* Implements hook_update_N().
*
* Updates configuration settings name.
*/
function anonymous_subscriptions_update_8904() {
$old_config_name = "anonymous_subscription.settings";
$new_config_name = "anonymous_subscriptions.settings";
$configFactory = Drupal::configFactory();
if (!empty($configFactory->get($new_config_name)->getRawData())) {
Drupal::messenger()->addWarning($new_config_name . ' configuration object is not empty and should be updated manually');
}
$configFactory->rename($old_config_name, $new_config_name);
}
/**
* Implements hook_update_N().
*
* Removing outdated configuration entity.
*/
function anonymous_subscriptions_update_8906() {
$old_config_name = "anonymous_subscription.settings";
$configFactory = Drupal::configFactory();
if (!empty($configFactory->get($old_config_name)->getRawData())) {
Drupal::configFactory()->getEditable($old_config_name)->delete();
}
}
/**
* Implements hook_update_N().
*
* Import anonymous subscription view.
*/
function anonymous_subscriptions_update_8908() {
if (Views::getView('anonymous_subscriptions')) {
Drupal::messenger()->addWarning('There is already exists view with id anonymous_subscriptions already. Review the existing view.');
return;
}
$dir = drupal_get_path('module', 'anonymous_subscriptions');
$fileStorage = new FileStorage($dir . '/config/install');
$config = $fileStorage->read('views.view.anonymous_subscriptions');
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
$storage = \Drupal::entityTypeManager()
->getStorage('view');
/** @var \Drupal\views\Entity\View $view */
$view = $storage->create($config);
$view->save();
}
/**
* Implements hook_update_N().
*
* Setting anonymous_subscriptions_scheduled_hour to -1 value = next cron.
*/
function anonymous_subscriptions_update_8909() {
$config = Drupal::configFactory()->getEditable(SettingsForm::$configName);
$config->set('anonymous_subscriptions_scheduled_hour', -1);
$config->save();
}