-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatternbuilder.install
475 lines (424 loc) · 14.7 KB
/
patternbuilder.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* @file
* Patternbuilder install file.
*/
/**
* Implements hook_schema().
*/
function patternbuilder_schema() {
$schema = array();
$schema['patternbuilder_components'] = array(
'description' => 'Relationship of components to unique ids.',
'fields' => array(
'id' => array(
'description' => 'Unique ID.',
'type' => 'serial',
'not null' => TRUE,
),
'machine_name' => array(
'description' => 'Component machine name.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
),
'pattern_type' => array(
'description' => 'Pattern type.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
),
'status' => array(
'description' => 'The status name of this pattern.',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
),
'bundle_name' => array(
'description' => 'Entity bundle name.',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'field_name' => array(
'description' => 'Field name representing the schema.',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
),
),
'primary key' => array('id'),
'unique keys' => array(
'unique_machine_name' => array('machine_name'),
),
);
return $schema;
}
/**
* Defined patternbuilder directories.
*
* @return array
* An array of directories with keys of 'schemas' and 'templates'.
*/
function _patternbuilder_install_get_patternbuilder_dirs() {
$path = drupal_get_path('module', 'patternbuilder');
return array(
'templates' => array(
$path . '/twig',
),
);
}
/**
* Add patternbuilder directories.
*/
function _patternbuilder_install_add_patternbuilder_dirs() {
drupal_load('module', 'patternbuilder');
// Add new directories.
$pb_dirs = _patternbuilder_install_get_patternbuilder_dirs();
if (!empty($pb_dirs['schemas'])) {
patternbuilder_schema_dirs_add($pb_dirs['schemas']);
}
if (!empty($pb_dirs['templates'])) {
patternbuilder_template_dirs_add($pb_dirs['templates']);
}
}
/**
* Implements hook_modules_enabled().
*/
function patternbuilder_modules_enabled($modules) {
// Determine modules implementing patternbuilder hooks.
$implements = array();
$reset_hook = TRUE;
foreach (patternbuilder_hook_info() as $hook_name => $group) {
$hook_implements = module_implements($hook_name, FALSE, $reset_hook);
if ($hook_implements) {
$implements = array_merge($implements, $hook_implements);
}
}
// Clear patternbuilder cache if there are new hook implementations.
if ($implements) {
$implements = array_unique($implements);
$new_implements = array_diff($modules, $implements);
if ($new_implements) {
patternbuilder_invalidate_cache();
}
}
}
/**
* Helper function to convert "text" to "text_long" fields.
*
* Usage: Update functions to convert a text field before importing JSON schema
* changes.
*
* Allowed conversions:
* - 'text' TO 'text_long' or 'text_with_summary'.
* - 'text_long' TO 'text_with_summary'
* - 'text_with_summary' TO 'text_long': The 'summary' column is kept.
*
* The text_long or text_with_summary to text is not allowed since data could
* be truncated in the conversion of the value column.
*
* @param string $field_name
* The field name to convert.
* @param string $field_type_new
* The new field type.
*
* @return bool|null
* TRUE if the text field was converted or already converted.
* FALSE if the conversion failed.
* NULL if there was invalid information provided.
*/
function patternbuilder_convert_text_field($field_name, $field_type_new) {
$field_original = field_info_field($field_name);
// Return if the field is the new type.
if ($field_original['type'] == $field_type_new) {
return TRUE;
}
// Exit if there is no field or no field type.
if (empty($field_original['type']) || empty($field_original['storage']['type'])) {
return NULL;
}
// Limit to text field types.
$allowed_types = array('text', 'text_long', 'text_with_summary');
if (!in_array($field_original['type'], $allowed_types, TRUE)) {
return NULL;
}
// Allowed conversions: [original type => new types allowed].
$allowed_conversions = array(
'text' => array('text_long', 'text_with_summary'),
'text_long' => array('text_with_summary'),
'text_with_summary' => array('text_long'),
);
if (!isset($allowed_conversions[$field_original['type']]) || !in_array($field_type_new, $allowed_conversions[$field_original['type']])) {
return NULL;
}
// Only SQL storage is supported.
if ($field_original['storage']['type'] != 'field_sql_storage' ||
empty($field_original['storage']['module']) ||
!module_exists($field_original['storage']['module'])) {
return NULL;
}
// Determine if the field has data.
$has_data = field_has_data($field_original);
// Create new field info.
$field = $field_original;
$field['type'] = $field_type_new;
// Include required files.
module_load_include('inc', 'field', 'field.info');
module_load_install($field['module']);
// Get type info with defaults.
$field_type_new_info = field_info_field_types($field_type_new);
if (isset($field_type_new_info['settings'])) {
if (!empty($field['settings'])) {
$field['settings'] += $field_type_new_info['settings'];
// Cleanup field settings from old field.
if ($field['settings']) {
foreach (array_keys($field['settings']) as $field_setting_key) {
if (!array_key_exists($field_setting_key, $field_type_new_info['settings'])) {
unset($field['settings'][$field_setting_key]);
}
}
}
}
else {
$field['settings'] = $field_type_new_info['settings'];
}
}
// Collect field storage information.
// See field_create_field().
$field_schema = (array) module_invoke($field['module'], 'field_schema', $field);
$field_schema += array(
'columns' => array(),
'indexes' => array(),
'foreign keys' => array(),
);
// 'columns' are hardcoded in the field type.
$field['columns'] = $field_schema['columns'];
// 'foreign keys' are hardcoded in the field type.
$field['foreign keys'] = $field_schema['foreign keys'];
// 'indexes' can be both hardcoded in the field type, and specified in the
// incoming $field definition.
$field += array(
'indexes' => array(),
);
$field['indexes'] += $field_schema['indexes'];
// Get the storage schema in case it's different than the field schema.
$storage_schema = _field_sql_storage_schema($field);
if (empty($storage_schema)) {
return FALSE;
}
$transaction = NULL;
$have_transactional_support = Database::getConnection()->supportsTransactionalDDL();
if ($have_transactional_support) {
// If the database supports transactional DDL, we can go ahead and rely
// on it. If not, we will have to rollback manually if something fails.
$transaction = db_transaction();
}
$converted = FALSE;
try {
// Update the field storage tables.
// Text fields all have the same indexes so this only needs to update the
// field schema columns.
// Note: This only updates or adds columns. It does not remove columns.
// If converting to a type that has fewer columns, then the original
// columns will remain.
foreach ($storage_schema as $table_name => $table_schema) {
foreach ($field['columns'] as $field_column_name => $field_attributes) {
$column_name = _field_sql_storage_columnname($field['field_name'], $field_column_name);
if (isset($table_schema['fields'][$column_name])) {
if (db_field_exists($table_name, $column_name)) {
db_change_field($table_name, $column_name, $column_name, $table_schema['fields'][$column_name]);
}
else {
db_add_field($table_name, $column_name, $table_schema['fields'][$column_name]);
}
}
}
}
// Tell the storage engine to update the field. Do this before
// saving the new definition since it still might fail.
$storage_type = field_info_storage_types($field['storage']['type']);
module_invoke($storage_type['module'], 'field_storage_update_field', $field, $field_original, $has_data);
// Update the field config.
// The serialized 'data' column contains everything from $field that does not
// have its own column and is not automatically populated when the field is
// read.
$data = $field;
unset($data['columns'], $data['field_name'], $data['type'], $data['locked'], $data['module'], $data['cardinality'], $data['active'], $data['deleted']);
// Additionally, do not save the 'bundles' property populated by
// field_info_field().
unset($data['bundles']);
$field['data'] = $data;
$field_config_primary_key = array('id');
drupal_write_record('field_config', $field, $field_config_primary_key);
// Clear caches.
field_cache_clear();
// Invoke external hooks after the cache is cleared for API consistency.
// See field_update_field().
module_invoke_all('field_update_field', $field, $field_original, $has_data);
$converted = TRUE;
}
catch (Exception $e) {
if ($have_transactional_support) {
$transaction->rollback();
}
else {
throw $e;
}
}
// Update field instances.
if ($converted) {
$field_map = field_info_field_map();
if (!empty($field_map[$field_name]['bundles'])) {
$allowed_widgets = array();
foreach (field_info_widget_types() as $widget_name => $widget_type) {
if (in_array($field_type_new, $widget_type['field types'], TRUE)) {
$allowed_widgets[$widget_name] = array(
'type' => $widget_name,
'active' => 1,
'settings' => !empty($widget_type['settings']) ? $widget_type['settings'] : array(),
);
}
}
$allowed_formatters = array();
foreach (field_info_formatter_types() as $formatter_name => $formatter) {
if (in_array($field_type_new, $formatter['field types'], TRUE)) {
$allowed_formatters[$formatter_name] = array(
'type' => $formatter_name,
'settings' => !empty($formatter['settings']) ? $formatter['settings'] : array(),
);
}
}
$instance_new = array(
'settings' => array(),
'widget' => array(
'active' => 1,
'settings' => array(),
),
'display' => array(),
);
if (!empty($field_type_new_info['instance_settings'])) {
$instance_new['settings'] = $field_type_new_info['instance_settings'];
}
if (!empty($field_type_new_info['default_widget']) && isset($allowed_widgets[$field_type_new_info['default_widget']])) {
$instance_new['widget'] = $allowed_widgets[$field_type_new_info['default_widget']];
}
elseif ($allowed_widgets) {
$instance_new['widget'] = reset($allowed_widgets);
}
if (!empty($field_type_new_info['default_formatter']) && isset($allowed_formatters[$field_type_new_info['default_formatter']])) {
$instance_new['display']['default'] = $allowed_formatters[$field_type_new_info['default_formatter']];
}
elseif ($allowed_formatters) {
$instance_new['display']['default'] = reset($allowed_formatters);
}
foreach ($field_map[$field_name]['bundles'] as $entity_type => $entity_bundles) {
foreach ($entity_bundles as $entity_bundle) {
$instances = field_info_instances($entity_type, $entity_bundle);
foreach ($instances as $instance) {
// Settings.
if (empty($instance['settings'])) {
$instance['settings'] = $instance_new['settings'];
}
else {
$instance['settings'] += $instance_new['settings'];
}
// Widget.
if (empty($instance['widget']['type']) || !isset($allowed_widgets[$instance['widget']['type']])) {
$instance['widget'] = $instance_new['widget'];
}
// Formatter.
if (!empty($instance['display'])) {
foreach ($instance['display'] as $view_mode => $display) {
if (empty($display['type']) || !isset($allowed_formatters[$display['type']])) {
$instance['display'][$view_mode] = $instance_new['display']['default'];
}
}
}
// Update.
field_update_instance($instance);
}
}
}
}
// Clear caches.
field_cache_clear();
}
return $converted;
}
/**
* Increase machine name field, add bundle and pattern type fields.
*/
function patternbuilder_update_7101() {
$table = 'patternbuilder_components';
$field_updates = array('machine_name', 'bundle_name', 'pattern_type');
if (!db_table_exists($table)) {
drupal_install_schema('patternbuilder');
}
else {
$schema = drupal_get_schema_unprocessed('patternbuilder', $table);
foreach ($field_updates as $field) {
if (isset($schema['fields'][$field])) {
if (db_field_exists($table, $field)) {
db_change_field($table, $field, $field, $schema['fields'][$field]);
}
else {
db_add_field($table, $field, $schema['fields'][$field]);
}
}
}
}
}
/**
* Add field_name column.
*/
function patternbuilder_update_7102() {
$table = 'patternbuilder_components';
$field_inserts = array('field_name');
if (!db_table_exists($table)) {
drupal_install_schema('patternbuilder');
}
else {
$schema = drupal_get_schema_unprocessed('patternbuilder', $table);
foreach ($field_inserts as $field) {
if (isset($schema['fields'][$field]) && !db_field_exists($table, $field)) {
db_add_field($table, $field, $schema['fields'][$field]);
}
}
}
}
/**
* Add status field, set pattern_type and bundle_name field default to NULL.
*/
function patternbuilder_update_7103() {
$table = 'patternbuilder_components';
$field_changes = array('status', 'pattern_type', 'bundle_name');
if (!db_table_exists($table)) {
drupal_install_schema('patternbuilder');
}
else {
$schema = drupal_get_schema_unprocessed('patternbuilder', $table);
foreach ($field_changes as $field) {
if (isset($schema['fields'][$field])) {
if (!db_field_exists($table, $field)) {
db_add_field($table, $field, $schema['fields'][$field]);
}
else {
db_change_field($table, $field, $field, $schema['fields'][$field]);
}
}
}
}
}
/**
* Add a native twig template directory.
*/
function patternbuilder_update_7104() {
// Pass to 7105.
}
/**
* Set correct native twig template directory.
*/
function patternbuilder_update_7105() {
_patternbuilder_install_add_patternbuilder_dirs();
}