You are here

function menu_links_configuration_rebuild_ordered in Configuration Management 7

Generate a depth tree of all menu links.

1 call to menu_links_configuration_rebuild_ordered()
menu_links_configuration_rebuild in includes/configuration.menu.inc
Implements hook_configuration_export_rebuild().

File

includes/configuration.menu.inc, line 248

Code

function menu_links_configuration_rebuild_ordered($menu_links, $reset = FALSE) {
  static $ordered;
  static $all_links;
  if (!isset($ordered) || $reset) {
    $ordered = array();
    $unordered = configuration_get_default('menu_links');

    // Order all links by depth.
    if ($unordered) {
      do {
        $current = count($unordered);
        foreach ($unordered as $key => $link) {
          $identifier = menu_links_configuration_identifier($link);
          $parent = isset($link['parent_path']) ? "{$link['menu_name']}:{$link['parent_path']}" : '';
          if (empty($parent)) {
            $ordered[$identifier] = 0;
            $all_links[$identifier] = $link;
            unset($unordered[$key]);
          }
          elseif (isset($ordered[$parent])) {
            $ordered[$identifier] = $ordered[$parent] + 1;
            $all_links[$identifier] = $link;
            unset($unordered[$key]);
          }
        }
      } while (count($unordered) < $current);
    }
    asort($ordered);
  }

  // Ensure any default menu items that do not exist are created.
  foreach (array_keys($ordered) as $identifier) {
    $link = $all_links[$identifier];
    $existing = configuration_menu_link_load($identifier);
    if (!$existing || in_array($link, $menu_links)) {

      // Retrieve the mlid if this is an existing item.
      if ($existing) {
        $link['mlid'] = $existing['mlid'];
      }

      // Retrieve the plid for a parent link.
      if (!empty($link['parent_path']) && ($parent = configuration_menu_link_load("{$link['menu_name']}:{$link['parent_path']}"))) {
        $link['plid'] = $parent['mlid'];
      }
      else {
        $link['plid'] = 0;
      }
      menu_link_save($link);
    }
  }
}