You are here

public function TBMegaMenuAdminController::saveMenuConfig in The Better Mega Menu 8

Same name and namespace in other branches
  1. 2.x src/Controller/TBMegaMenuAdminController.php \Drupal\tb_megamenu\Controller\TBMegaMenuAdminController::saveMenuConfig()

Saves a menu configuration.

Parameters

array $data: A decoded JSON object used to save the configuration.

Return value

array The message and status code indicating the result of the save attempt.

1 call to TBMegaMenuAdminController::saveMenuConfig()
TBMegaMenuAdminController::saveConfiguration in src/Controller/TBMegaMenuAdminController.php
Ajax callback for admin screen.

File

src/Controller/TBMegaMenuAdminController.php, line 158

Class

TBMegaMenuAdminController
Handler for configuring and saving MegaMenu settings.

Namespace

Drupal\tb_megamenu\Controller

Code

public function saveMenuConfig(array $data) {
  $menu_config = self::getMenuConfig($data);
  $block_config = self::getBlockConfig($data);
  $menu_name = self::getMenuName($data);
  $theme = self::getTheme($data);
  $code = 200;

  // Ensure the config can be loaded before proceeding.
  $config = MegaMenuConfig::loadMenu($menu_name, $theme);
  if ($config === NULL) {
    return [
      'message' => self::saveError('load_menu'),
      'code' => 500,
    ];
  }
  if ($menu_config && $menu_name && $block_config && $theme) {

    // This is parameter to load menu_tree with the enabled links.
    $menu_tree_parameters = (new MenuTreeParameters())
      ->onlyEnabledLinks();

    // Load menu items with condition.
    $menu_items = $this->menuTree
      ->load($menu_name, $menu_tree_parameters);

    // Sync mega menu before store.
    $this->menuBuilder
      ->syncConfigAll($menu_items, $menu_config, 'backend');
    $this->menuBuilder
      ->syncOrderMenus($menu_config);
    $config
      ->setBlockConfig($block_config);
    $config
      ->setMenuConfig($menu_config);

    // Save the config and return a success message.
    $saved_config = $config
      ->save();
    if ($saved_config == 1 || $saved_config == 2) {
      $result = $this
        ->t("Saved config sucessfully!");
    }
    else {
      $result = self::saveError('unknown');
      $code = 500;
    }
  }
  else {
    $result = self::saveError('missing_info', $menu_name, $theme, $block_config, $menu_config);
    $code = 500;
  }
  return [
    'message' => $result,
    'code' => $code,
  ];
}