You are here

function tb_megamenu_update_8001 in The Better Mega Menu 8

Same name and namespace in other branches
  1. 2.x tb_megamenu.install \tb_megamenu_update_8001()

Migrate any settings from the DB table to configuration objects.

File

./tb_megamenu.install, line 14
Install and Update code for tb_megamenu.

Code

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.');
}