forked from BurdaMagazinOrg/module-nexx_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexx_integration.install
362 lines (325 loc) · 11.3 KB
/
nexx_integration.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* @file
* Install, uninstall and update hooks for nexx integration module.
*/
use Drupal\Core\Database\Database;
/**
* Implements hook_schema().
*/
function nexx_integration_schema() {
$schema['nexx_taxonomy_term_data'] = [
'description' => 'Stores taxonomy data for exchange with Omnia video cms per taxonomy term.',
'fields' => [
'tid' => [
'description' => 'Taxonomy term id: {taxonomy_term_data}.tid.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'nexx_item_id' => [
'description' => 'Nexx item ID.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'vid' => [
'type' => 'varchar',
'description' => 'The ID of the terms target entity: {taxonomy_term_data}.vid.',
'length' => 32,
'not null' => TRUE,
],
],
'primary key' => ['tid'],
];
return $schema;
}
/**
* Implements hook_install().
*/
function nexx_integration_install() {
$source = drupal_get_path('module', 'nexx_integration') . '/images/icons';
$destination = \Drupal::config('media_entity.settings')->get('icon_base');
media_entity_copy_icons($source, $destination);
}
/**
* Add nexx_taxonomy_term_data database schema.
*/
function nexx_integration_update_8001() {
$spec = [
'description' => 'Stores taxonomy data for exchange with Omnia video cms per taxonomy term.',
'fields' => [
'tid' => [
'description' => 'Taxonomy term id: {taxonomy_term_data}.tid.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'vid' => [
'description' => 'The ID of the terms target entity: {taxonomy_term_data}.vid.',
'type' => 'varchar_ascii',
'not null' => TRUE,
'length' => 32,
],
'nexx_item_id' => [
'description' => 'Nexx item ID.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
];
$schema = Database::getConnection()->schema();
$schema->createTable('nexx_taxonomy_term_data', $spec);
}
/**
* Removed faulty update.
*/
function nexx_integration_update_8002() {
}
/**
* Add primary key, remove vid.
*/
function nexx_integration_update_8003() {
$spec = ['tid'];
$schema = Database::getConnection()->schema();
// A primary key.
$schema->addPrimaryKey('nexx_taxonomy_term_data', $spec);
// Remove unused vid column.
$schema->dropField('nexx_taxonomy_term_data', 'vid');
}
/**
* Add new tags_ids field to nexx field type.
*/
function nexx_integration_update_8004() {
$messages = [];
$database = \Drupal::database();
$database_schema = $database->schema();
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$schema = \Drupal::keyValue('entity.storage_schema.sql')->getAll();
foreach ($schema as $item_name => $item) {
list($entity_type_id, , $field_name) = explode('.', $item_name);
// Basic sanity check.
if (!isset($entity_types[$entity_type_id])) {
continue;
}
foreach ($item as $table_name => $table_schema) {
foreach ($table_schema as $schema_key => $schema_data) {
if ($schema_key === 'fields') {
// Test if field schema contains a known nexx property.
// TODO: investigate how to properly determine,
// if we are dealing with a nexx field.
$existing_field_property = $field_name . '_encodedSSC';
$new_field_property = $field_name . '_tags_ids';
if (isset($schema_data[$existing_field_property])) {
// This is the actual update, adding the new field.
if ($database_schema->tableExists($table_name) && !$database_schema->fieldExists($table_name, $new_field_property)) {
$messages[] = t('Updating nexx field @field in table @table', [
'@field' => $field_name,
'@table' => $table_name,
]);
$database_schema->addField($table_name, $new_field_property, [
'type' => 'varchar',
'length' => 256,
]);
}
}
}
}
}
}
$message = '';
if (!empty($messages)) {
$message .= implode('</br>', $messages);
}
return $message;
}
/**
* Add new runtime and copyright fields to nexx field type.
*/
function nexx_integration_update_8005() {
$messages = [];
$database = \Drupal::database();
$database_schema = $database->schema();
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$schema = \Drupal::keyValue('entity.storage_schema.sql')->getAll();
foreach ($schema as $item_name => $item) {
list($entity_type_id, , $field_name) = explode('.', $item_name);
// Basic sanity check.
if (!isset($entity_types[$entity_type_id])) {
continue;
}
foreach ($item as $table_name => $table_schema) {
foreach ($table_schema as $schema_key => $schema_data) {
if ($schema_key === 'fields') {
// Test if field schema contains a known nexx property.
// TODO: investigate how to properly determine,
// if we are dealing with a nexx field.
$existing_field_property = $field_name . '_encodedSSC';
$new_field_properties = [$field_name . '_runtime', $field_name . '_copyright'];
if (isset($schema_data[$existing_field_property])) {
foreach ($new_field_properties as $new_field_property) {
// This is the actual update, adding the new field.
if ($database_schema->tableExists($table_name) && !$database_schema->fieldExists($table_name, $new_field_property)) {
$messages[] = t('Updating nexx field @field in table @table', [
'@field' => $field_name,
'@table' => $table_name,
]);
$database_schema->addField($table_name, $new_field_property, [
'type' => 'varchar',
'length' => 256,
]);
}
}
}
}
}
}
}
$message = '';
if (!empty($messages)) {
$message .= implode('</br>', $messages);
}
return $message;
}
/**
* Add new hash field to nexx field type.
*/
function nexx_integration_update_8006() {
$messages = [];
$database = \Drupal::database();
$database_schema = $database->schema();
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$schema = \Drupal::keyValue('entity.storage_schema.sql')->getAll();
foreach ($schema as $item_name => $item) {
list($entity_type_id, , $field_name) = explode('.', $item_name);
// Basic sanity check.
if (!isset($entity_types[$entity_type_id])) {
continue;
}
foreach ($item as $table_name => $table_schema) {
foreach ($table_schema as $schema_key => $schema_data) {
if ($schema_key === 'fields') {
// Test if field schema contains a known nexx property.
// TODO: investigate how to properly determine,
// if we are dealing with a nexx field.
$existing_field_property = $field_name . '_encodedSSC';
$new_field_properties = [$field_name . '_hash'];
if (isset($schema_data[$existing_field_property])) {
foreach ($new_field_properties as $new_field_property) {
// This is the actual update, adding the new field.
if ($database_schema->tableExists($table_name) && !$database_schema->fieldExists($table_name, $new_field_property)) {
$messages[] = t('Updating nexx field @field in table @table', [
'@field' => $field_name,
'@table' => $table_name,
]);
$database_schema->addField($table_name, $new_field_property, [
'type' => 'varchar',
'length' => 256,
]);
}
}
}
}
}
}
}
$message = '';
if (!empty($messages)) {
$message .= implode('</br>', $messages);
}
return $message;
}
/**
* Remove description fields.
*/
function nexx_integration_update_8007() {
$messages = [];
$database = \Drupal::database();
$database_schema = $database->schema();
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$schema_key_value = \Drupal::keyValue('entity.storage_schema.sql');
$schema = $schema_key_value->getAll();
foreach ($schema as $item_name => $item) {
list($entity_type_id, , $field_name) = explode('.', $item_name);
// Basic sanity check.
if (!isset($entity_types[$entity_type_id])) {
continue;
}
foreach ($item as $table_name => $table_schema) {
foreach ($table_schema as $schema_key => $schema_data) {
if ($schema_key === 'fields') {
$field_schema_key = 'media.field_schema_data.' . $field_name;
$field_schema = $schema_key_value->get($field_schema_key, []);
// Test if field schema contains a known nexx property.
// TODO: investigate how to properly determine,
// if we are dealing with a nexx field.
$existing_field_property = $field_name . '_encodedSSC';
$update_field_properties = [$field_name . '_description', $field_name . '_altdescription'];
if (isset($schema_data[$existing_field_property])) {
foreach ($update_field_properties as $update_field_property) {
// This is the actual update, deleting the fields.
if ($database_schema->tableExists($table_name) && $database_schema->fieldExists($table_name, $update_field_property)) {
$messages[] = t('Updating nexx field @field in table @table', [
'@field' => $field_name,
'@table' => $table_name,
]);
$database_schema->dropField($table_name, $update_field_property);
// Also update schema key value.
foreach ($field_schema as $table => $definition) {
unset($field_schema[$table]['fields'][$update_field_property]);
}
}
}
$schema_key_value->set($field_schema_key, $field_schema);
}
}
}
}
}
$message = '';
if (!empty($messages)) {
$message .= implode('</br>', $messages);
}
return $message;
}
/**
* Add vid field into nexx_taxonomy_term_data table and fill them.
*/
function nexx_integration_update_8008() {
$table = 'nexx_taxonomy_term_data';
/** @var \Drupal\Core\Database\Connection $db */
$database = \Drupal::service('database');
$schema = $database->schema();
$schema->addField($table, 'vid', [
'type' => 'varchar',
'description' => 'The ID of the terms target entity: {taxonomy_term_data}.vid.',
'length' => 32,
'not null' => FALSE,
]);
$result = $database->select($table, 'n')
->fields('n', ['tid'])
->execute();
$tids = $result->fetchAll();
foreach ($tids as $item) {
$result = $database->select('taxonomy_term_field_data', 't')
->fields('t', ['vid'])
->condition('t.tid', $item->tid)
->execute();
$vid = $result->fetchField();
$database->update($table)
->fields(['vid' => $vid])
->condition('tid', $item->tid)
->execute();
}
$schema->changeField($table, 'vid', 'vid', [
'type' => 'varchar',
'description' => 'The ID of the terms target entity: {taxonomy_term_data}.vid.',
'length' => 32,
'not null' => TRUE,
]);
}