Hi, not entirely sure of the feasibility of this one, but I thought I'd throw it up here to see if there's any interest (and it'd certainly help me out). Ideally I'd like to be able to set a context condition that would fire whenever a node with a entry under a particular menu is viewed. That is to say, if I have the following menu structure...

[primary-links]
 - Item 1
   -- Item 1.1
   -- Item 1.2
 - Item 2
 - Item 3
[another-menu]
 - Item 1
 - Item 2
   -- Item 2.1
   -- Item 2.2

... I'd like to set a condition on the entirety of [primary-menu] or [another-menu] or what have you. Obviously I could select every item under the particular menu with the current Context UI but this would fall down when new items are added to the menu. Does that all make sense? Comments/questions?

CommentFileSizeAuthor
#9 655256.patch998 bytesalanburke

Comments

steven jones’s picture

Status: Active » Postponed

Interesting, but the actual drupal menu trail doesn't include the menu either.

nauthiz693’s picture

Exactly what I'm looking to do too.. Did you figure out another way to do this?

terrencewood’s picture

Here's a module that set's a context based on the menu the current item belongs to.

context_active_menu.info

name = "Context active menu"
description = "Context plugin to expose the active menu as a context condition"
version = "6.x-1.0"
core = "6.x"

context_active_menu.module


/**
 * @file
 *  Provides a plugin to expose the active menu as a context condition 
 */

/**
 * Implement hook_ctools_plugin_api
 */

function context_active_menu_ctools_plugin_api($module, $api) {
  if ($module == 'context' && $api == 'plugins') {
    return array('version' => 3);
  }
}


/**
 * Implement hook_context_plugins
 */

function context_active_menu_context_plugins() {
  $plugins = array();
  $plugins['context_condition_active_menu'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'context_active_menu') .'/plugins',
      'file' => 'context_condition_active_menu.inc',
      'class' => 'context_condition_active_menu',
      'parent' => 'context_condition',
    ),
  );
  return $plugins;
}


/**
 * Implement hook_context_registry
 */

function context_active_menu_context_registry() {
  return array(
    'conditions' => array(
    'active_menu' => array(
      'title' => t('Active menu'),
      'plugin' => 'context_condition_active_menu',
      ),
    ),
  );
}

/**
 * Implement hook_init
 */
 
function context_active_menu_init() {
  $map = context_condition_map();
  if (!empty($map['active_menu'])  && $plugin = context_get_plugin('condition', 'active_menu')) {
    $plugin->execute();
  }
}

plugins/context_condition_active_menu.inc


/**
 * Expose the active menu as a context condition
 */
class context_condition_active_menu extends context_condition {
  function condition_values() {
    $menus = array_reverse(menu_get_menus());
    array_unshift($menus, "-- ". t('None') ." --");
    return $menus;
  }

  function condition_form($context) {
    $form = parent::condition_form($context);
    $form['#type'] = 'select';
    $form['#multiple'] = TRUE;
    return $form;
  }

  function execute() {
    $item = menu_get_item($_GET['q']);
    // There's no other way to get the menu name for a menu item without querying the db.
    $menu = db_result(db_query("SELECT menu_name FROM {menu_links} WHERE link_title='%s'", $item['title']));
    foreach (context_enabled_contexts() as $context) {
      if (isset($context->conditions['active_menu']['values']) && in_array($menu, $context->conditions['active_menu']['values'])) {
        $this->condition_met($context, $menu);
      }
    }
  }
}
steven jones’s picture

Version: 6.x-2.0-beta7 » 6.x-3.0-beta4
Status: Postponed » Active

One for context 3.x

DjebbZ’s picture

Version: 6.x-3.0-beta4 » 6.x-2.0-beta7
Status: Active » Postponed

I'm using context 3.0-beta4 on the last Drupal (6.16).
I don't know if the plugin is not working, or if I didn't understand it. Here's what I'm doing with it :

In my primary menu, I want all menu item under one of them (including the selected menu item itself) to set the condition. Which means, all content in the menu trail should activate the condition and trigger the necessary reaction, which is "display a menu_block in the left region".

Is this what this plugin does ? Because I have no difference in the condition setter in the Context UI, and no different effect : my left block does not appear at all.

Note that I also tried to activate the Menu Trails module, but it's not working either.

Any idea how to achieve this ?

steven jones’s picture

Version: 6.x-2.0-beta7 » 6.x-3.0-beta4
Status: Postponed » Active

Lets leave it on the context 3.x branch then.

terrencewood’s picture

The context_active_menu plugin takes the entire menu from the root so you can set the context if a menu item is a selected menu. e.g. primary-links .

What you are after setting a context when a menu item is in a subtree. See the context_active_menu_tree plugin below

terrencewood’s picture

Module to set a context if the current menu item is in a subtree

context_active_menu_tree.info

name = "Context active menu tree"
description = "Context plugin to expose the active menu tree as a context condition"
version = "6.x-1.0"
core = "6.x"

context_active_menu_tree.module


/**
 * @file
 *  Provides a context plugin to expose the active menu tree as a context condition 
 */

/**
 * Implement hook_ctools_plugin_api
 */

function context_active_menu_tree_ctools_plugin_api($module, $api) {
  if ($module == 'context' && $api == 'plugins') {
    return array('version' => 3);
  }
}


/**
 * Implement hook_context_plugins
 */

function context_active_menu_tree_context_plugins() {
  $plugins = array();
  $plugins['context_condition_active_menu_tree'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'context_active_menu_tree') .'/plugins',
      'file' => 'context_condition_active_menu_tree.inc',
      'class' => 'context_condition_active_menu_tree',
      'parent' => 'context_condition',
    ),
  );
  return $plugins;
}


/**
 * Implement hook_context_registry
 */

function context_active_menu_tree_context_registry() {
  return array(
    'conditions' => array(
    'active_menu_tree' => array(
      'title' => t('Active menu tree'),
      'plugin' => 'context_condition_active_menu_tree',
      ),
    ),
  );
}

/**
 * Implement hook_init
 */
 
function context_active_menu_tree_init() {
  $map = context_condition_map();
  if (!empty($map['active_menu_tree'])  && $plugin = context_get_plugin('condition', 'active_menu_tree')) {
    $plugin->execute();
  }
}

plugins/context_condition_active_menu_tree.inc


/**
 * Expose the active menu tree as a context condition
 */
class context_condition_active_menu_tree extends context_condition {
  function condition_values() {
    $menus = menu_parent_options(array_reverse(menu_get_menus()), NULL);
    $root_menus = array();
    foreach ($menus as $key => $name) {
      $id = explode(':', $key);
      if ($id[1] == '0') {
        $root_menus[$id[0]] = check_plain($name);
      }
      else {
        $link = menu_link_load($id[1]);
        $root_menu = $root_menus[$id[0]];
        $menus[$root_menu][$link['mlid']] = $name;
      }
      unset($menus[$key]);
    }
    array_unshift($menus, "-- ". t('None') ." --");
    return $menus;
  }

  function condition_form($context) {
    $form = parent::condition_form($context);
    $form['#type'] = 'select';
    $form['#multiple'] = TRUE;
    return $form;
  }

  function execute() {
 	// grab the parents of the menu item at the current path
    $results = db_query("SELECT p1, p2, p3, p4, p5, p6, p7, p8, p9 FROM {menu_links} WHERE link_path='%s'", $_GET['q']);
    while($result = db_fetch_array($results)) {
      foreach (context_enabled_contexts() as $context) {
        if (isset($context->conditions['active_menu_tree']['values'])) {
          foreach($context->conditions['active_menu_tree']['values'] as $item) {
            if(in_array($item, $result)) {
              $this->condition_met($context, $result);
              break;
            }
          }
        }
      }
    }
  }
}

alanburke’s picture

StatusFileSize
new998 bytes

I needed to get this working for Context 2.
Patch takes the ideas in the preceding pasted-in module, and puts it into the context module itself.
This is how I was expecting the behavior of this particular context to function.
My code is a bit rough, to say the least, but it works for my particular use case.

yhahn’s picture

Version: 6.x-3.0-beta4 » 6.x-3.x-dev
Assigned: Unassigned » yhahn
Priority: Normal » Critical

On my list.

yhahn’s picture

Status: Active » Closed (duplicate)
millenniumtree’s picture

#8 appears to be in context now, but #3 is NOT!!!

We needed to activate a context if any page/view/etc in a particular menu was being viewed.
#3 works great EXCEPT if you've linked to a Views page.
I've modified the plugin include code to work with both node and Views pages.

The only line you have to change is the $menu= line in the execute() function. Search for the link_path instead of the link_title

    $menu = db_result(db_query("SELECT menu_name FROM {menu_links} WHERE link_path='%s'", $item['href']));

plugins/context_condition_active_menu.inc

<?php
/**
 * Expose the active menu as a context condition
 */
class context_condition_active_menu extends context_condition {
  function condition_values() {
    $menus = array_reverse(menu_get_menus());
    array_unshift($menus, "-- ". t('None') ." --");
    return $menus;
  }

  function condition_form($context) {
    $form = parent::condition_form($context);
    $form['#type'] = 'select';
    $form['#multiple'] = TRUE;
    return $form;
  }

  function execute() {
    $item = menu_get_item($_GET['q']);
    // There's no other way to get the menu name for a menu item without querying the db.
    $menu = db_result(db_query("SELECT menu_name FROM {menu_links} WHERE link_path='%s'", $item['href']));
    foreach (context_enabled_contexts() as $context) {
      if (isset($context->conditions['active_menu']['values']) && in_array($menu, $context->conditions['active_menu']['values'])) {
        $this->condition_met($context, $menu);
      }
    }
  }
}

The real question is this: Why is this not already in Context??

davidseth’s picture

I have upgraded this great module to D7. If anyone is interested I can put it into a sandbox. I would also like to get this as a full module. @terrencewood what do you think?

Cheers,

David

davidseth’s picture

I have created a sandbox project for D7: http://drupal.org/sandbox/davidseth/1541422