You are here

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

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

Order display menu item.

Parameters

object $menu_config: Public static function menuItemInsert menu_config.

File

src/WeMegaMenuBuilder.php, line 453

Class

WeMegaMenuBuilder

Namespace

Drupal\we_megamenu

Code

public static function orderMenuItems($menu_config) {
  if (isset($menu_config->menu_config)) {
    $menus = $menu_config->menu_config;
    if (isset($menus) && is_object($menus)) {
      foreach ($menus as $key_menu => $menu) {
        if (isset($menu->rows_content)) {
          $rows_content = $menu->rows_content;
          foreach ($rows_content as $key_rows => $rows) {
            $positions = [];
            $list_menu_items = [];
            $list_mega_menu_items = [];
            $row_count = 0;
            $col_count = 0;
            if (is_array($rows)) {
              foreach ($rows as $key_row_col => $row) {
                if (isset($row->col_content)) {
                  $cols = $row->col_content;
                  if (is_array($cols) && isset(reset($cols)->mlid)) {
                    $row_count = $key_rows;
                    $col_count = $key_row_col;
                    $positions[] = $row_count . '-' . $col_count . '-' . count($cols);
                    foreach ($cols as $key_col => $col) {
                      $menu_item = Drupal::entityTypeManager()
                        ->getStorage('menu_link_content')
                        ->loadByProperties([
                        'uuid' => $col->mlid,
                      ]);
                      if (is_array($menu_item)) {
                        $menu_item = reset($menu_item);
                        if (method_exists($menu_item, 'get')) {
                          $list_menu_items[] = [
                            'derivativeId' => $menu_item
                              ->get('uuid')
                              ->getString(),
                            'title' => $menu_item
                              ->get('title')
                              ->getString(),
                            'weight' => $menu_item
                              ->get('weight')
                              ->getString(),
                          ];
                          $list_mega_menu_items[] = $col;
                        }
                      }
                    }
                  }
                }
              }
            }
            if (!sizeof($positions)) {
              continue;
            }
            $list_menu_items = WeMegaMenuBuilder::sortMenu($list_menu_items);
            foreach ($positions as $key_position => $position) {
              $pos_params = explode('-', $position);
              $row = $pos_params[0];
              $col = $pos_params[1];
              $size = $pos_params[2];
              if ($size >= 0) {
                if (is_array($list_menu_items)) {
                  $list_item = array_slice($list_menu_items, 0, $size);
                  if (is_array($list_item)) {
                    $list_menu_items = array_map('unserialize', array_diff_assoc(array_map('serialize', $list_menu_items), array_map('serialize', $list_item)));
                    $menu_config->menu_config->{$key_menu}->rows_content[$row][$col]->col_content = [];
                    foreach ($list_item as $key_menu_item => $menu_itemnew) {
                      foreach ($list_mega_menu_items as $key_mega_menu => $mega_menu_item) {
                        if (isset($mega_menu_item->mlid)) {
                          if ($menu_itemnew['derivativeId'] == $mega_menu_item->mlid) {
                            $menu_config->menu_config->{$key_menu}->rows_content[$row][$col]->col_content[] = $mega_menu_item;
                            $items_validate_serialize = array_map("serialize", $menu_config->menu_config->{$key_menu}->rows_content[$row][$col]->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][$col]->col_content = $items_validate;
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}