You are here

context_menu_block.module in Context: Menu Block 6.2

Same filename and directory in other branches
  1. 6.3 context_menu_block.module
  2. 7.3 context_menu_block.module

Provides support for Context within the Menu Block module.

File

context_menu_block.module
View source
<?php

/**
 * @file
 * Provides support for Context within the Menu Block module.
 */

/**
 * Implementation of hook_help().
 */
function context_menu_block_help($section) {
  switch ($section) {
    case 'admin/help#context_menu_block':

      // Return a line-break version of the module README
      $readme = file_get_contents(dirname(__FILE__) . '/README.txt');
      $readme = filter_filter('process', 2, NULL, $readme);
      $readme = filter_filter('process', 1, NULL, $readme);
      return $readme;
  }
}

/**
 * Implementation of hook_menu_block_tree_alter().
 */
function context_menu_block_menu_block_tree_alter(&$tree, $config) {

  // Check to see if the active trail is already set.
  if (!_context_menu_block_has_active_trail($tree)) {
    $active_paths = context_active_values('menu');
    context_menu_tree_add_active_path($tree, $active_paths);
  }
}

/**
 * Check to see if a menu tree already has an active trail.
 *
 * @param $tree
 *   array The unrendered menu tree.
 * @return
 *   bool True if the menu tree has an active trail, false if otherwise.
 */
function _context_menu_block_has_active_trail($tree) {
  foreach ($tree as $item) {
    if ($item['link']['in_active_trail']) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Add the active trail indicators into the tree based on context.
 *
 * @param $tree
 *   array The menu tree.
 * @param $active_paths
 *   array The active paths defined by Context.
 * @return
 *   bool True if an active trail match has been found, false if otherwise.
 */
function context_menu_tree_add_active_path(&$tree, $active_paths) {
  foreach (array_keys($tree) as $key) {
    if (!empty($tree[$key]['link']['href']) && in_array($tree[$key]['link']['href'], $active_paths)) {

      // Set the active trail.
      $tree[$key]['link']['in_active_trail'] = TRUE;

      // The Drupal Way is to only set the active menu when the page is actually
      // the active page. However, the context reaction is supposed to set the
      // active menu. So, we need to "fake" the active menu by styling the link
      // as active.
      if (isset($tree[$key]['link']['localized_options']['attributes']['class'])) {
        $tree[$key]['link']['localized_options']['attributes']['class'] .= " active";
      }
      else {
        $tree[$key]['link']['localized_options']['attributes']['class'] = "active";
      }

      // Found a match, so break the recursion.
      return TRUE;
    }
    else {
      if (!empty($tree[$key]['below'])) {

        // Check the child menu to see if the active path can be matched.
        $tree[$key]['link']['in_active_trail'] = context_menu_tree_add_active_path($tree[$key]['below'], $active_paths);

        // If a match has been found, break the recursion.
        if ($tree[$key]['link']['in_active_trail']) {
          return TRUE;
        }
      }
    }
  }

  // If we didn't find an active item, return FALSE.
  return FALSE;
}

Functions

Namesort descending Description
context_menu_block_help Implementation of hook_help().
context_menu_block_menu_block_tree_alter Implementation of hook_menu_block_tree_alter().
context_menu_tree_add_active_path Add the active trail indicators into the tree based on context.
_context_menu_block_has_active_trail Check to see if a menu tree already has an active trail.