You are here

public static function WeMegaMenuBuilder::dragDropMenuItems in Drupal Mega Menu 8.x

Same name and namespace in other branches
  1. 8 src/WeMegaMenuBuilder.php \Drupal\we_megamenu\WeMegaMenuBuilder::dragDropMenuItems()

Drag-Drop menu item insert.

Parameters

string $menu_name: Public static function menuItemInsert menu_name.

string $theme_name: Public static function menuItemInsert theme_name.

object $menu_config: Public static function menuItemInsert menu_config.

object $child_item: Public static function menuItemInsert child_item.

File

src/WeMegaMenuBuilder.php, line 549

Class

WeMegaMenuBuilder

Namespace

Drupal\we_megamenu

Code

public static function dragDropMenuItems($menu_name, $theme_name = '', $menu_config, $child_item) {
  $list_menu_items = WeMegaMenuBuilder::getMenuItems($menu_name);
  if (isset($child_item['col_content']) && isset($child_item['col_cfg']) && isset($menu_config->menu_config)) {
    $tmp_col_content = $child_item['col_content'];
    $tmp_col_cfg = $child_item['col_cfg'];
    $menu_items = $menu_config->menu_config;
    foreach ($list_menu_items as $uuid => $childs) {
      $uuid = $uuid == 'standard.front_page' ? base_path() : $uuid;
      foreach ($menu_items as $key_menu => $menu_item) {
        if (isset($menu_item->rows_content)) {
          $rows_content = $menu_item->rows_content;
          if (count($rows_content)) {
            foreach ($rows_content as $key_rows => $rows) {
              if ($key_menu == $uuid) {
                if (is_array($rows)) {
                  $list_mega_items = [];
                  $row_count = 0;
                  $col_count = 0;
                  foreach ($rows as $key_row_col => $row) {
                    if (isset($row->col_content)) {
                      $cols = $row->col_content;
                      if (is_array($cols)) {
                        foreach ($cols as $key_col => $col) {
                          if (isset($col->mlid)) {
                            $row_count = $key_rows;
                            $col_count = $key_row_col;
                            if (!in_array($col->mlid, $childs)) {
                              unset($menu_config->menu_config->{$key_menu}->rows_content[$key_rows][$key_row_col]->col_content[$key_col]);
                              if (!count($menu_config->menu_config->{$key_menu}->rows_content[$key_rows][$key_row_col]->col_content)) {
                                unset($menu_config->menu_config->{$key_menu}->rows_content[$key_rows][$key_row_col]);
                                if (!count($menu_config->menu_config->{$key_menu}->rows_content[$key_rows])) {
                                  unset($menu_config->menu_config->{$key_menu}->rows_content[$key_rows]);
                                }
                              }
                            }
                            else {
                              $list_mega_items[] = $col->mlid;
                            }
                          }
                        }
                      }
                    }
                  }
                  foreach ($childs as $key_child => $child_uuid) {
                    $child_uuid = $child_uuid == 'standard.front_page' ? base_path() : $child_uuid;
                    if (!in_array($child_uuid, $list_mega_items)) {
                      $tmp_col_content->mlid = $child_uuid;
                      $list_mega_items[] = $child_uuid;
                      if (is_object($menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]->col_content)) {
                        $tmp = clone $menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count];
                        unset($menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]);
                        $menu_config->menu_config->{$key_menu}->rows_content[$row_count + 1][$col_count] = $tmp;
                      }
                      $menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]->col_content[] = $tmp_col_content;
                      $items_validate_serialize = array_map('serialize', $menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]->col_content);
                      $items_validate_unique = array_unique($items_validate_serialize);
                      $items_validate = array_map('unserialize', $items_validate_unique);
                      $menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]->col_content = $items_validate;
                      if (!isset($menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]->col_config)) {
                        $menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]->col_config = $tmp_col_cfg;
                      }
                    }
                  }
                }
              }
            }
          }
          else {
            if ($key_menu == $uuid) {
              foreach ($childs as $key_child => $child_uuid) {
                $tmp_col_content->mlid = $child_uuid;
                $menu_config->menu_config->{$key_menu}->rows_content[0][0]->col_content[] = $tmp_col_content;
                $items_validate_serialize = array_map("serialize", $menu_config->menu_config->{$key_menu}->rows_content[0][0]->col_content);
                $items_validate_unique = array_unique($items_validate_serialize);
                $items_validate = array_map("unserialize", $items_validate_unique);
                $menu_config->menu_config->{$key_menu}->rows_content[0][0]->col_content = $items_validate;
                if (!isset($menu_config->menu_config->{$key_menu}->rows_content[0][0]->col_config)) {
                  $menu_config->menu_config->{$key_menu}->rows_content[0][0]->col_config = $tmp_col_cfg;
                }
              }
            }
          }
        }
      }
    }

    // Remove duplicate items
    foreach ($menu_items as $key_menu => $menu_item) {
      if (isset($menu_item->rows_content)) {
        $rows_content = $menu_item->rows_content;
        if (count($rows_content)) {
          foreach ($rows_content as $key_rows => $rows) {
            if (is_array($rows)) {
              $list_mega_items = [];
              $row_count = 0;
              $col_count = 0;
              $flag = [];
              foreach ($rows as $key_row_col => $row) {
                if (isset($row->col_content)) {
                  $cols = $row->col_content;
                  if (is_array($cols)) {
                    foreach ($cols as $key_col => $col) {
                      if (isset($col->mlid)) {
                        $row_count = $key_rows;
                        $col_count = $key_row_col;
                        if (isset($flag[$col->mlid])) {
                          $flag[$col->mlid]++;
                          if ($flag[$col->mlid] > 0) {
                            if (isset($menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count])) {
                              $col_items_content = $menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count];
                              if (isset($col_items_content) && count($col_items_content->col_content)) {
                                foreach ($col_items_content->col_content as $key_col_content => $c_content) {
                                  if (isset($c_content->mlid) && $c_content->mlid == $col->mlid) {
                                    unset($menu_config->menu_config->{$key_menu}->rows_content[$row_count][$col_count]->col_content[$key_col_content]);
                                  }
                                }
                              }
                            }
                          }
                        }
                        else {
                          $flag[$col->mlid] = 0;
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}