-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_menu.install
166 lines (150 loc) · 6.68 KB
/
field_menu.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
<?php
/**
* @file
* Install, update and uninstall functions for field_menu module.
*/
use Drupal\Core\Database\Database;
/**
* Update all menu field instances with new properties.
*
* This affects all existing field_menu implementations on this site.
*/
function field_menu_update_9232() {
$schema = Database::getConnection()->schema();
$database = \Drupal::database();
$all_field_menu_fields = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('field_menu');
if (!$all_field_menu_fields) {
return t('No menu fields to update.');
};
foreach ($all_field_menu_fields as $entity_type => $field) {
$field_name = array_key_first($field);
// New column for selected menu.
$spec_menu = [
'description' => "The selected menu name.",
'type' => 'varchar_ascii',
'length' => 32,
'not null' => TRUE,
'default' => '',
];
$schema->addField($entity_type . '__' . $field_name, $field_name . '_menu', $spec_menu);
// New column for menu options.
$spec_options = [
'description' => "Includes menu parameters and other options.",
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
];
$schema->addField($entity_type . '__' . $field_name, $field_name . '_options', $spec_options);
// New revision column for menu options.
$spec_options_revision = [
'description' => 'Menu options',
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_options', $spec_options_revision);
// New revision column for selected menu.
$spec_menu_revision = [
'type' => 'text',
'description' => 'Selected menu',
'not null' => FALSE,
'size' => 'tiny',
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_menu', $spec_menu_revision);
// Modify field data.
$field_query = $database->select($entity_type . '__' . $field_name, 'f');
$field_query->fields('f', [
'entity_id',
'revision_id',
'delta',
'field_menu_menu_title',
'field_menu_menu_item_key',
'field_menu_max_depth',
'field_menu_include_root',
]);
$current_field_rows = $field_query->execute();
if (!is_null($field_query)) {
foreach ($current_field_rows as $row) {
$menu_levels_array = explode(':', $row->field_menu_menu_item_key);
$row->field_menu_menu = $menu_levels_array[0];
$field_update_query = $database->update($entity_type . '__' . $field_name);
$field_update_query->condition('entity_id', $row->entity_id);
$field_update_query->condition('revision_id', $row->revision_id);
$field_update_query->condition('delta', $row->delta);
$field_update_query->fields([
'field_menu_menu' => $row->field_menu_menu,
'field_menu_options' => [
'title' => $row->field_menu_menu_title,
'depth' => $row->field_menu_max_depth,
'expand_all_items' => 1,
'follow' => 0,
'follow_parent' => '',
'parent' => $row->field_menu_menu_item_key,
'render_parent' => $row->field_menu_include_root,
],
]);
$field_update_query->execute();
}
}
// Revisions
$field_revision_query = $database->select($entity_type . '_revision__' . $field_name, 'fr');
$field_revision_query->fields('fr', [
'entity_id',
'revision_id',
'delta',
'field_menu_menu_title',
'field_menu_menu_item_key',
'field_menu_max_depth',
'field_menu_include_root',
]);
$current_field_revision_rows = $field_revision_query->execute();
if (!is_null($field_revision_query)) {
foreach ($current_field_revision_rows as $row) {
$menu_levels_array = explode(':', $row->field_menu_menu_item_key);
$row->field_menu_menu = $menu_levels_array[0];
$field_update_query = $database->update($entity_type . '_revision__' . $field_name);
$field_update_query->condition('entity_id', $row->entity_id);
$field_update_query->condition('revision_id', $row->revision_id);
$field_update_query->condition('delta', $row->delta);
$field_update_query->fields([
'field_menu_menu' => $row->field_menu_menu,
'field_menu_options' => [
'title' => $row->field_menu_menu_title,
'depth' => $row->field_menu_max_depth,
'expand_all_items' => 1,
'follow' => 0,
'follow_parent' => '',
'parent' => $row->field_menu_menu_item_key,
'render_parent' => $row->field_menu_include_root,
],
]);
$field_update_query->execute();
}
// After transforming all of the data, we can drop the old columns.
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_menu_item_key');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_max_depth');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_include_root');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_menu_title');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_menu_item_key');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_max_depth');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_include_root');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_menu_title');
}
// Current node field configurations
$field_manager = \Drupal::getContainer()->get('entity_field.manager');
// Because the manager was already loaded before the above config was forced,
// it will return the old configuration that was cached
$field_manager->clearCachedFieldDefinitions();
$field_storage_configs = $field_manager->getFieldStorageDefinitions($entity_type);
// Get the last installed manager, this is the gatekeeper that determines if
// an update is needed or can be done
$last_installed_repo = \Drupal::getContainer()->get('entity.last_installed_schema.repository');
// Get the last installed configurations for node fields
// These are iterative objects and need to stored as such, not just simple arrays,
// so reusing the previously set configs is not an option
$last_installed_configs = $last_installed_repo->getLastInstalledFieldStorageDefinitions($entity_type);
// Force the last installed config to be the current for the field
$last_installed_configs[$field_name] = $field_storage_configs[$field_name];
$last_installed_repo->setLastInstalledFieldStorageDefinitions($entity_type, $last_installed_configs);
}
}