You are here

function dhtml_menu_preprocess_menu_link in DHTML Menu 7

Preprocessor for menu_link. Adds the required HTML attributes and loads subtrees if necessary.

File

./dhtml_menu.theme.inc, line 13
dhtml_menu.theme.inc All functions related to generating the menu markup.

Code

function dhtml_menu_preprocess_menu_link(&$variables) {
  $cookie =& drupal_static(__FUNCTION__);
  $settings = variable_get('dhtml_menu_settings');

  // Parse the cookie, if it is enabled.
  if (!isset($cookie)) {
    if ($settings['effects']['remember'] && $settings['nav'] != 'open' && $settings['effects']['siblings'] != 'close-all') {
      $cookie = explode(',', @$_COOKIE['dhtml_menu']);
    }
    else {
      $cookie = array();
    }
  }

  // Saves a lot of code.
  $l =& $variables['element']['#original_link'];
  if (empty($l)) {
    return;
  }

  // Determine if the menu is blacklisted or not whitelisted.
  // First check menu blocks.
  if (!empty($variables['element']['#bid']['module']) && !empty($variables['element']['#bid']['delta']) && $variables['element']['#bid']['module'] == 'menu_block') {
    $checked = !empty($settings['filter']['menu_block']['menu_block_' . $variables['element']['#bid']['delta']]);
    if ($checked == ($settings['filter']['type'] == 'blacklist')) {

      // This menu block is either blacklisted, or not whitelisted, hence it is disabled.
      return;
    }
  }
  else {
    $disabled = ($settings['filter']['type'] == 'blacklist') == !empty($settings['filter']['list'][$l['menu_name']]);
    if (empty($l['menu_name']) || empty($l['mlid']) || $disabled) {
      return;
    }
  }

  // Add the ID and class attributes.
  $variables['element']['#attributes']['id'] = 'dhtml_menu-' . _dhtml_menu_unique_id($l['mlid']);
  $variables['element']['#attributes']['class'][] = 'dhtml-menu';

  // If there are children, but they were not loaded, load them.
  if ($l['has_children'] && !$variables['element']['#below']) {
    $variables['element']['#below'] = _dhtml_menu_subtree($l['menu_name'], $l['mlid']);
  }

  // If the current item can expand, and is neither saved as open nor in the active trail, close it.
  if ($l['has_children'] && !$l['in_active_trail'] && !in_array($variables['element']['#attributes']['id'], $cookie)) {
    if (!in_array('collapsed', $variables['element']['#attributes']['class'])) {
      $variables['element']['#attributes']['class'][] = 'collapsed';
    }
    if (!in_array('start-collapsed', $variables['element']['#attributes']['class'])) {
      $variables['element']['#attributes']['class'][] = 'start-collapsed';
    }
  }
}