You are here

function _menu_breadcrumb_process_new_menus in Menu Breadcrumb 6

Same name and namespace in other branches
  1. 7 menu_breadcrumb.module \_menu_breadcrumb_process_new_menus()

Helper for _menu_breadcrumb_get_menus(). Compare new menus against the defined menu patterns, and update the persistent variable caches accordingly.

1 call to _menu_breadcrumb_process_new_menus()
_menu_breadcrumb_get_menus in ./menu_breadcrumb.module
Get the menu selection configuration.

File

./menu_breadcrumb.module, line 185
The main file for the menu_breadcrumb module.

Code

function _menu_breadcrumb_process_new_menus($new_menus, &$menus, &$match_cache, $match_cache_rebuild) {

  // Load the current regex patterns.
  $patterns = array();
  $menu_patterns = variable_get('menu_breadcrumb_menu_patterns', MENU_BREADCRUMB_REGEX_DEFAULT);
  $menu_patterns = array_filter(explode("\n", $menu_patterns));
  foreach ($menu_patterns as $pattern) {
    $part = array();

    // Form validation has already ensured these will match.
    preg_match(MENU_BREADCRUMB_REGEX_MATCH, $pattern, $part);
    $regex = $part[1];
    $title = $part[2];
    $patterns[$regex] = $title;
  }

  // Remove any deprecated patterns.
  if ($match_cache_rebuild) {
    foreach ($new_menus as $menu_name => $menu) {
      if ($menu['type'] == 'pattern' && !in_array($menu_name, array_keys($patterns), TRUE)) {
        unset($new_menus[$menu_name]);
      }
    }
  }

  // Aggregate the menus which match the specified patterns.
  if ($patterns) {
    $update_match_cache = FALSE;
    foreach ($patterns as $regex => $title) {
      foreach ($new_menus as $menu_name => $menu) {
        if ($menu['type'] == 'menu') {
          if (preg_match($regex, $menu_name)) {

            // This menu name matches a pattern. Add the pattern
            // itself as a menu entry if it's new.
            if (!array_key_exists($regex, $menus)) {

              // Use existing weight and enabled status.
              $menus[$regex] = $new_menus[$menu_name];
              $menus[$regex]['type'] = 'pattern';
            }

            // Remove the matching name, and update the match cache.
            unset($new_menus[$menu_name]);
            $match_cache[$menu_name] = $regex;
            $update_match_cache = TRUE;
          }
        }
      }

      // We don't have the titles for new patterns yet in
      // 'menu_breadcrumb_menus', so add it now for the settings form.
      if (array_key_exists($regex, $menus)) {
        $menus[$regex]['title'] = $title;
      }
    }
    if ($update_match_cache) {
      variable_set('menu_breadcrumb_pattern_matches', $match_cache);
    }
  }

  // Merge in any remaining new menus that did not match any pattern
  // and update the 'menu_breadcrumb_menus' cache.
  $menus = array_merge($new_menus, $menus);
  foreach (array_keys($menus) as $menu_name) {
    $menus[$menu_name]['name'] = $menu_name;
  }
  uasort($menus, '_menu_breadcrumb_sort');

  // sort by weight.
  variable_set('menu_breadcrumb_menus', $menus);
}