You are here

function menu_links_features_rebuild_ordered in Features 7.2

Same name and namespace in other branches
  1. 6 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.

Parameters

array[] $menu_links: Array of menu links.

bool $reset: If TRUE, the static cache will be reset.

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

File

includes/features.menu.inc, line 343
Features integration for 'menu' module.

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_identifier']) ? $link['parent_identifier'] : '';
          $weight = 0;

          // Parent has been seen, so weigh this above parent.
          if (isset($ordered[$parent])) {
            $weight = $ordered[$parent] + 1;
          }
          elseif ($parent) {
            continue;
          }
          $ordered[$identifier] = $weight;
          $all_links[$identifier] = $link;
          unset($unordered[$key]);
        }

        // Exit out when the above does no changes this loop.
      } while (count($unordered) < $current);
    }

    // Add all remaining unordered items to the ordered list.
    foreach ($unordered as $link) {
      $identifier = menu_links_features_identifier($link);
      $ordered[$identifier] = 0;
      $all_links[$identifier] = $link;
    }
    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_identifier']) && ($parent = features_menu_link_load($link['parent_identifier']))) {
        $link['plid'] = $parent['mlid'];
      }
      elseif (!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);
    }
  }
}