- Context configuration -
Namespace: context_ui
Key: section
Value: events

Content types: Events
Menus: Primary links > Events

Expected result: The menu item 'events' should gain an 'active' class when viewing event nodes. It doesn't appear to work in the dev release from 11 June.

Comments

yhahn’s picture

Assigned: Unassigned » yhahn

Hey Ronanw,

Does your menu markup look like this:

<ul class='links primary-links'>
  <li class='first menu-1-1-2-active'>
    <a class='first menu-1-1-2-active' href='/my-drupal-site/events'>Events</a>
  </li>
  ...
</ul>

If it does, then the context UI menu handling is actually working correctly. By default, the Drupal menu system makes the "active" class of menu items rather inaccessible to themers by concatenating it onto the menu class -- "menu-1-1-2" in this example. You can make it usable by overriding theme_links or theme_menu_links. Here is an example of a theme_links override:

function phptemplate_menu_links($links, $attributes = array()) {
  if (!count($links)) {
    return '';
  }
  $output = "<ul class='links clear-block'>\n";
  $i = 1;
  foreach ($links as $index => $link) {
    $class = array();
    // check the index string for "active" and set a usable class on the <li>
    if (stristr($index, 'active')) {
      $class[] = 'active';
    }
    if ($i == count($links)) {
      $class[] = 'last';
    }
    else if ($i == 1) {
      $class[] = 'first';
    }
    $class = implode(' ', $class);
    $output .= '<li class="'.$class.'"';
    $output .= ">". l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']) ."</li>\n";
    $i++;
  }
  $output .= '</ul>';

  return $output;
}

On the other hand, if your markup doesn't have any traces of active classes at all, please post back to let me know. Thanks!

Rowanw’s picture

I'm currently using the Primary links block to display the menu, which doesn't give any active class when viewing an event node. If I print theme('menu_links') in page.tpl.php (+ your theme override) then the active class is applied correctly, however as far as I can tell this doesn't give me a hierarchical/nested list, which is what I need.

Would I have to do something special with theme_menu_item() to get this working? Also, is Context module able to 'expand' a menu when it's active?

yhahn’s picture

Hey Ronan,

Sorry for the late reply. Fortunately the blocks provided by the menu module go through theme_menu_tree which means you can override their default behavior. Unfortunately, theme_menu_tree looks like this:

function theme_menu_tree($pid = 1) {
  if ($tree = menu_tree($pid)) {
    return "\n<ul class=\"menu\">\n". $tree ."\n</ul>\n";
  }
}

Meaning that the majority of the logic for building these menus is relegated to the menu_tree() function. You may want to take a good look at this function as you can definitely capture the behavior you need by modifying it (rather, create a copy of it and point your override of theme_menu_tree at your version). You may also need to modify theme_menu_item().

In general, I've been running into various snags with Drupal 5's menu system that makes context_ui's behavior with it a little rocky (for example, after your context definitions have set how the menu should behave, another module can come along and alter that). I'll be doing some serious study for the Drupal 6 version which hopefully will be able to avoid some of these problems thanks to the new menu system.

Let me know if I can be of any more help!

Rowanw’s picture

Thanks yhahn, I just tried the latest dev release and it now appears to expand menu items correctly, which for our site is essentially the same as making it 'active' (since the main menu will always have a second level).

I'll try the approach you suggested when I can't simply rely on the expanded state though.

yhahn’s picture

Status: Active » Closed (fixed)

Glad it's working -- I'm closing this for now.

gagarine’s picture

Status: Active » Closed (fixed)

Same issue with the 6.x-2

gagarine’s picture

Title: Menu item's context doesn't work » Context doesn't expand menu's item of primary links block
Version: 5.x-1.x-dev » 6.x-2.0-beta4
Status: Closed (fixed) » Active
cruzinxxx’s picture

Status: Closed (fixed) » Active

my markup doesnt have any traces of active class. I used the menuclass module to create user defined class names, because I had to upload an image instead of text for menu items, but now the active class doesnt appear anymore. I need that too, to set additional properties...could you help me

pendashteh’s picture

Status: Active » Fixed

i solved the problem of my case.

i had a primary menu like this:

---Overview
|
+--- Home
|
+--- News
|
+--- Gallery
|
---Products
|
+--- ...

i wanted to active News menu item when in story nodes.

in Context Reaction i set Menu as News, but it did not apply. it even did not expand the Overview menu.

i solved by doing this in template.php and using some code from the module:

function phptemplate_set_active_item() {
    $contexts = context_active_contexts();
    $active_paths = array();
    foreach ($contexts as $context) {
		$path = $context->reactions['menu'];
		if (!empty($path)) {
			$active_paths[] = $context->reactions['menu'];
		}
    }
	if (count($active_paths)) {
		menu_set_active_item($active_paths[0]);
	}
}
function phptemplate_preprocess_block(&$vars, $hook) {
	phptemplate_set_active_item();
}

pendashteh’s picture

Status: Fixed » Active

oh, my solution just hide the "View, Edit, Translate" tab menu over the node.

pendashteh’s picture

i SOLVED it in my case like this:

function phptemplate_get_active_item() {
    $contexts = context_active_contexts();
    $active_paths = array();
    foreach ($contexts as $context) {
		$path = $context->reactions['menu'];
		if (!empty($path)) {
			$active_paths[] = $context->reactions['menu'];
		}
    }
	if (count($active_paths)) {
		return $active_paths[0];
	}
	return $_GET['q'];
}
function phptemplate_menu_tree($tree) {
	static $_done = false;
	if (!$_done) {
		$_done = true;
		$current = $_GET['q'];
		$path = phptemplate_get_active_item();
		menu_set_active_item($path);
		$tree = menu_tree('primary-links');
		menu_set_active_item($current);
	}
	return theme_menu_tree($tree);
}

if you use menu_block module instead of

$tree = menu_tree('primary-links');

use:

		$tree = menu_tree_page_data('primary-links');
		$tree = menu_block_tree_output($tree);
pendashteh’s picture

Status: Active » Fixed

Final code:

function phptemplate_get_context_active_item() {
      $plugin = context_get_plugin('reaction', 'menu');
      $active_paths = array_filter($plugin->get_active_paths($vars));
      if (count($active_paths)) {
            return $active_paths[0];
      }
      return null;
}
function phptemplate_menu_tree($tree) {
      static $_done = false;
      if (!$_done) {
            $_done = true;
            $current = $_GET['q'];
            $path = phptemplate_get_context_active_item();
            if ($path and $path != $current) {
                  menu_set_active_item($path);
                  $tree = menu_tree_page_data('primary-links');
                  $tree = menu_block_tree_output($tree);
                  menu_set_active_item($current);
            }
      }
      return theme_menu_tree($tree);
}
gagarine’s picture

Status: Fixed » Needs work

Thanks for your work.
But bugs are marked as fixed when code are committed. Please submit a patch http://drupal.org/patch/create and change the statue of the bug to "need review" more infos http://drupal.org/node/156119. If you have a problem to create a patch feel free to ask on the forum :).

markabur’s picture

aliasghar's code is only applicable to context v3.

steven jones’s picture

Version: 6.x-2.0-beta4 » 6.x-2.0-beta7
Component: Code » Documentation
Category: bug » task
Issue tags: +context-2.x-rc-blocker

This is probably just another case of needing some documentation with these things in it.

steven jones’s picture

Priority: Critical » Normal

Don't think it's critical.

steven jones’s picture

Assigned: yhahn » steven jones

Assigning to me

steven jones’s picture

Version: 6.x-2.0-beta7 » 6.x-3.0-beta4
Component: Documentation » Code
Assigned: steven jones » Unassigned
Status: Needs work » Closed (duplicate)

Looks like this issue is a duplicate of #653698: Menu Trail Condition incorrectly displays depth, because we're not actually setting the active menu trail.

nedjo’s picture