You are here

function menu_links_features_rebuild_ordered in Features 6

Same name and namespace in other branches
  1. 7.2 includes/features.menu.inc \menu_links_features_rebuild_ordered()
  2. 7 includes/features.menu.inc \menu_links_features_rebuild_ordered()

Generate a depth tree of all menu links.

1 call to menu_links_features_rebuild_ordered()
menu_links_features_rebuild in includes/features.menu.inc
Implementation of hook_features_rebuild().

File

includes/features.menu.inc, line 285

Code

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

    // Order all links by depth.
    if ($unordered) {
      do {
        $current = count($unordered);
        foreach ($unordered as $key => $link) {
          $identifier = menu_links_features_identifier($link);
          $parent = isset($link['parent_path']) ? "{$link['menu_name']}:{$link['parent_path']}" : '';
          if (empty($parent) || !empty($parent) && !isset($unordered[$parent]) && !isset($ordered[$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 = features_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 = features_menu_link_load("{$link['menu_name']}:{$link['parent_path']}"))) {
        $link['plid'] = $parent['mlid'];
      }
      else {
        $link['plid'] = 0;
      }
      menu_link_save($link);
    }
  }
}