-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtb_megamenu.install
56 lines (51 loc) · 1.6 KB
/
tb_megamenu.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
<?php
/**
* @file
* Install and Update code for tb_megamenu.
*/
use Drupal\tb_megamenu\Entity\MegaMenuConfig;
use Drupal\Core\Utility\UpdateException;
/**
* Migrate any settings from the DB table to configuration objects.
*/
function tb_megamenu_update_8001(&$sandbox) {
// Migrate any settings from the DB table to configuration objects.
$database = \Drupal::database();
$count = 0;
// Check if table exists.
if ($database->schema()->tableExists('tb_megamenus')) {
$query = $database->select('tb_megamenus', 't');
$query->fields('t', ['menu_name', 'theme', 'block_config', 'menu_config']);
$results = $query->execute();
foreach ($results as $row) {
$menu = $row->menu_name;
$theme = $row->theme;
$config = MegaMenuConfig::loadMenu($menu, $theme);
if ($config == NULL) {
$values = [
'id' => "{$menu}__{$theme}",
];
$config = MegaMenuConfig::create($values);
}
$config->menu = $menu;
$config->theme = $theme;
$config->block_config = $row->block_config;
$config->menu_config = $row->menu_config;
try {
$config->save();
}
catch (Exception $e) {
throw new UpdateException('Failed to save configuration object for @menu / @theme', [
'@menu' => $menu,
'@theme' => $theme,
]);
}
$count++;
}
$database->schema()->dropTable('tb_megamenus');
}
if ($count) {
return t('Converted @count TB Mega Menu database settings to configuration entities.', ['@count' => $count]);
}
return t('No existing MegaMenu configurations to convert.');
}