You are here

tb_megamenu.install in The Better Mega Menu 2.x

Same filename and directory in other branches
  1. 8 tb_megamenu.install
  2. 7 tb_megamenu.install

Install and Update code for tb_megamenu.

File

tb_megamenu.install
View source
<?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.');
}

Functions

Namesort descending Description
tb_megamenu_update_8001 Migrate any settings from the DB table to configuration objects.