-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaw_profile.install
248 lines (219 loc) · 7.51 KB
/
caw_profile.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
<?php
/**
* @file
* caw_profile.install
*/
use Drupal\Core\Menu\MenuTreeParameters;
/**
* Save the system pages from the original config values into state.
*/
function caw_profile_update_8002() {
$state = \Drupal::state();
$state->set('caw_profile.403_page', '/node/3');
$state->set('caw_profile.404_page', '/node/2');
$state->set('caw_profile.front_page', '/node/1');
}
/**
* Rerun pathauto update hook.
*/
function caw_profile_update_8003() {
// Removed.
}
/**
* Install Claro theme and uninstall Seven.
*/
function caw_profile_update_9103() {
/** @var \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer */
$theme_installer = \Drupal::service('theme_installer');
$theme_installer->install(['claro', 'stanford_profile_admin']);
\Drupal::configFactory()
->getEditable('system.theme')
->set('admin', 'stanford_profile_admin')
->save();
try {
$theme_installer->uninstall(['seven']);
}
catch (\Exception $e) {
// Theme was already uninstalled.
}
}
/**
* Install menu_link module and configure the field.
*/
function caw_profile_update_9001() {
$entity_type_manager = \Drupal::entityTypeManager();
\Drupal::service('module_installer')->install(['menu_link']);
$entity_type_manager->getStorage('field_storage_config')
->create([
'uuid' => '963caf4a-7a55-4ed6-961d-765ea7594192',
'field_name' => 'field_menulink',
'type' => 'menu_link',
'entity_type' => 'node',
'cardinality' => 1,
])->save();
$field_config_storage = $entity_type_manager->getStorage('field_config');
$bundles = [
'stanford_page',
'stanford_event_series',
'stanford_person',
'stanford_policy',
];
foreach ($bundles as $bundle) {
$field_config_storage->create([
'entity_type' => 'node',
'field_name' => 'field_menulink',
'bundle' => $bundle,
'label' => 'Menu Link',
])->save();
}
$menu_items = $entity_type_manager->getStorage('menu_link_content')
->loadByProperties(['menu_name' => 'main']);
// Re-save all menu items to update their link uris.
// @see \Drupal\caw_profile_helper\EventSubscriber\EntityEventSubscriber::preSaveMenuLinkContent()
foreach ($menu_items as $menu_item) {
$menu_item->save();
}
drupal_flush_all_caches();
}
/**
* Update menu links on nodes for the updated version of menu_link_weight.
*/
function caw_profile_update_9102() {
// Instantiate the path alias path processor because it doesn't get added in
// this update hook.
\Drupal::service('path_processor_manager')
->addOutbound(\Drupal::service('path_alias.path_processor'), 300);
/** @var \Drupal\Core\Menu\MenuLinkTree $menu_link_tree */
$menu_link_tree = \Drupal::service('menu.link_tree');
$parameters = new MenuTreeParameters();
$menu = $menu_link_tree->load('main', $parameters);
_caw_profile_fix_menu($menu);
}
/**
* Update menu links on nodes for the updated version of menu_link_weight.
*
* @param array $menu_items
* Menu items.
* @param string|null $parent
* Parent menu id.
*/
function _caw_profile_fix_menu(array $menu_items = [], string $parent = NULL): void {
$node_storage = \Drupal::entityTypeManager()
->getStorage('node');
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$database = \Drupal::database();
foreach ($menu_items as $id => $menu_item) {
/** @var \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $link */
$link = $menu_item->link;
$url = $link->getUrlObject();
try {
if (
$url->isRouted() &&
$url->getRouteName() == 'entity.node.canonical'
) {
$node_id = $link->getUrlObject()->getRouteParameters()['node'] ?? NULL;
/** @var \Drupal\node\NodeInterface $node */
$node = $node_storage->load($node_id);
if (!$node->get('field_menulink')->isEmpty()) {
throw new \Exception('Menu link already exists on node ' . $node->label());
}
$menu_field_data = [
'menu_name' => $link->getMenuName(),
'title' => $link->getTitle(),
'description' => $link->getDescription(),
'parent' => $parent,
'weight' => $link->getweight(),
'expanded' => $link->isExpanded(),
];
$changed_time = $node->getChangedTime();
$node->set('field_menulink', $menu_field_data)->save();
$new_parent = 'menu_link_field:node_field_menulink_' . $node->uuid() . '_und';
// Reset the changed time to the value before this process.
$database->update('node_field_data')
->fields(['changed' => $changed_time])
->condition('nid', $node->id())
->execute();
$database->update('node_field_revision')
->fields(['changed' => $changed_time])
->condition('vid', $node->getRevisionId())
->execute();
if ($menu_item->subtree) {
_caw_profile_fix_menu($menu_item->subtree, $new_parent);
}
if (!$link->isEnabled()) {
$database->update('menu_tree')
->fields(['enabled' => 0])
->condition('id', $new_parent)
->execute();
}
$link->deleteLink();
continue;
}
}
catch (\Throwable $e) {
\Drupal::logger('caw_profile')
->error('Unable to update link %title. Error: %error', [
'%title' => $link->getTitle(),
'%error' => $e->getMessage(),
]);
}
if ($parent) {
$link->updateLink(['parent' => $parent], TRUE);
// Saving the link above, updates the entity, but it doesn't update the
// menu tree. So we need to update the menu tree manually.
$menu_link_manager->updateDefinition($link->getPluginId(), $link->getPluginDefinition(), FALSE);
}
if ($menu_item->subtree) {
_caw_profile_fix_menu($menu_item->subtree, $id);
}
}
}
/**
* Update configs 'stanford_profile_admin' with 'stanford_profile_admin_theme'.
*/
function caw_profile_update_9104() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll() as $config_name) {
$config = $config_factory->getEditable($config_name);
// Delete the admin theme blocks first.
if (str_starts_with($config_name, 'block.block.stanford_profile_admin_')) {
$config->delete();
continue;
}
// Change the dependencies to the new theme name.
if ($theme_dependencies = $config->get('dependencies.theme')) {
$position = array_search('stanford_profile_admin', $theme_dependencies);
if ($position !== FALSE) {
$config->set("dependencies.theme.$position", 'stanford_profile_admin_theme')
->save(TRUE);
}
}
}
$core_extension = $config_factory->getEditable('core.extension');
$core_extension->clear('theme.stanford_profile_admin')
->set('theme.stanford_profile_admin_theme', 0)
->save();
$system_theme = $config_factory->getEditable('system.theme');
if ($system_theme->get('admin') == 'stanford_profile_admin') {
$system_theme->set('admin', 'stanford_profile_admin_theme')->save();
}
}
/**
* Remove data for paragraph rows.
*/
function caw_profile_update_9105() {
$tables = [
'paragraph_row__su_page_components',
'paragraph_row__su_pubs_components',
'paragraph_row_revision__su_page_components',
'paragraph_row_revision__su_pubs_components',
'paragraph_rows_field_revision',
'paragraph_rows_item',
'paragraph_rows_item_field_data',
'paragraph_rows_revision',
];
foreach ($tables as $table) {
\Drupal::database()->truncate($table)->execute();
}
}