You are here

function we_megamenu_entity_insert in Drupal Mega Menu 8

Same name and namespace in other branches
  1. 8.x we_megamenu.module \we_megamenu_entity_insert()

Implements hook_entity_insert().

Reset menu to default status when origin menu changed.

File

./we_megamenu.module, line 637
Drupal 8 Mega Menu Module.

Code

function we_megamenu_entity_insert(EntityInterface $entity) {
  if (method_exists($entity, 'getTypedData') && method_exists($entity, 'getEntityTypeId')) {
    $data = $entity
      ->getTypedData();
    $data = $data
      ->toArray();
    $entity_type = $entity
      ->getEntityTypeId();
    switch ($entity_type) {
      case 'menu':
        break;
      case 'menu_link_content':
        if (isset($data['parent'][0]['value'])) {
          $mid_parent = $data['parent'][0]['value'];
          $mid_parent = str_replace('menu_link_content:', '', $mid_parent);
          $menu_name = $data['menu_name'][0]['value'];
          $config = \Drupal::config('system.theme');
          $theme_name = $config
            ->get('default');

          # Load main menu and insert to megamenu
          $menu_item_obj = new stdClass();
          $menu_item_obj->rows_content = [];
          $menu_item_obj->submenu_config = new stdClass();
          $menu_item_obj->submenu_config->width = '';
          $menu_item_obj->submenu_config->height = '';
          $menu_item_obj->submenu_config->type = '';
          $menu_item_obj->item_config = new stdClass();
          $menu_item_obj->item_config->level = 0;
          $menu_item_obj->item_config->type = '';
          $menu_item_obj->item_config->id = $entity
            ->get('uuid')
            ->getString();
          $menu_item_obj->item_config->submenu = '';
          $menu_item_obj->item_config->hide_sub_when_collapse = '';
          $menu_item_obj->item_config->group = '';
          $menu_item_obj->item_config->class = '';
          $menu_item_obj->item_config->{'data-icon'} = '';
          $menu_item_obj->item_config->{'data-caption'} = '';
          $menu_item_obj->item_config->{'data-alignsub'} = '';
          $menu_item_obj->item_config->{'data-target'} = '_self';

          # Mega menu config
          $menu_config = WeMegaMenuBuilder::loadConfig($menu_name, $theme_name);
          foreach ($menu_config->menu_config as $key_menu => $menu) {
            $mid_parent = $mid_parent == 'standard.front_page' ? base_path() : $mid_parent;
            if ($key_menu == $mid_parent) {
              $rows_content = $menu->rows_content;
              $tmp_col_content = new stdClass();
              $tmp_col_content->mlid = $entity
                ->get('uuid')
                ->getString();
              $tmp_col_content->type = 'we-mega-menu-li';
              $tmp_col_content->item_config = new stdClass();
              $tmp_col_cfg = new stdClass();
              $tmp_col_cfg->hidewhencollapse = '';
              $tmp_col_cfg->type = 'we-mega-menu-col';
              $tmp_col_cfg->width = 12;
              $tmp_col_cfg->block = '';
              $tmp_col_cfg->class = '';
              $tmp_col_cfg->block_title = 0;
              $child_item = [
                'col_content' => $tmp_col_content,
                'col_cfg' => $tmp_col_cfg,
              ];
              $menu_position = WeMegaMenuBuilder::menuItemInsert($key_menu, $menu_config, $menu, $child_item);
            }
          }
          $menu_config->menu_config->{$entity
            ->get('uuid')
            ->getString()} = $menu_item_obj;
          $data_cfg = json_encode($menu_config);
          WeMegaMenuBuilder::saveConfig($menu_name, $theme_name, $data_cfg);
        }
        break;
    }
  }
}