You are here

function tb_megamenu_update_megamenus in The Better Mega Menu 8

Same name and namespace in other branches
  1. 7 tb_megamenu.functions.inc \tb_megamenu_update_megamenus()
  2. 2.x tb_megamenu.module \tb_megamenu_update_megamenus()

Sync configuration with tb_megamenu when item in menu is updated.

1 string reference to 'tb_megamenu_update_megamenus'
tb_megamenu_form_menu_edit_form_alter in ./tb_megamenu.module
Implements hook_form_FORM_ID_alter().

File

./tb_megamenu.module, line 689
TB Mega Menu.

Code

function tb_megamenu_update_megamenus(&$form, FormStateInterface $form_state) {
  $menu_name = $form_state
    ->getValue('id');
  if ($menu_name) {
    $theme = \Drupal::config('system.theme')
      ->get('default');
    $menu_builder = \Drupal::service('tb_megamenu.menu_builder');
    if ($menu_config = $menu_builder
      ->getMenuConfig($menu_name, $theme)) {
      $menu_tree_parameters = (new MenuTreeParameters())
        ->onlyEnabledLinks();
      $menu_items = \Drupal::menuTree()
        ->load($menu_name, $menu_tree_parameters);
      $block_config = $menu_builder
        ->getBlockConfig($menu_name, $theme);
      $menu_builder
        ->syncConfigAll($menu_items, $menu_config, 'backend');
      $menu_builder
        ->syncOrderMenus($menu_config);
      $config = MegaMenuConfig::loadMenu($menu_name, $theme);
      if ($config === NULL) {
        \Drupal::messenger()
          ->addStatus(t("Cannot create a new config object in save!"));
        return;
      }
      $config->block_config = Json::encode($block_config);
      $config->menu_config = Json::encode($menu_config);
      $config
        ->save();
    }
  }
}