View source
<?php
class context_reaction_menu extends context_reaction {
function options_form($context) {
$options = array(
"-- " . t('None') . " --",
);
if (module_exists('menu')) {
$menus = menu_parent_options(menu_get_menus(), array(
'mlid' => 0,
));
$menu_names = array();
foreach ($menus as $id => $title) {
list($menu_name, $mlid) = explode(':', $id);
if ($mlid == '0') {
$menu_names[$menu_name] = $title;
}
else {
$link = menu_link_load($mlid);
$identifier = $link['link_path'];
$root_menu = $menu_names[$menu_name];
while (isset($options[$root_menu][$menu_name . ':' . $identifier])) {
$identifier .= "'";
}
$options[$root_menu][$menu_name . ':' . $identifier] = $title;
}
}
}
$menu_count = count($options, COUNT_RECURSIVE);
return array(
'#title' => $this->title,
'#description' => $this->description,
'#options' => $options,
'#type' => 'select',
'#multiple' => TRUE,
'#size' => $menu_count > 20 ? 20 : $menu_count,
'#default_value' => $this
->fetch_from_context($context),
);
}
function options_form_submit($values) {
$trimmed = array();
foreach ($values as $value) {
$value = trim($value, "'");
$trimmed[] = $value;
}
return $trimmed;
}
function fetch_from_context($context) {
$values = parent::fetch_from_context($context);
if (is_string($values)) {
$menu = menu_link_get_preferred($values);
if (!$menu) {
return array();
}
return array(
$menu['menu_name'] . ':' . $menu['link_path'],
);
}
return $values;
}
function execute(&$vars = NULL) {
$menu_names = menu_get_active_menu_names();
$active_paths = $this
->get_active_paths();
foreach ($menu_names as $menu_name) {
if (isset($active_paths[$menu_name])) {
foreach ($active_paths[$menu_name] as $path) {
if ($link = menu_link_get_preferred($path, $menu_name)) {
$this
->set_active_trail_from_link($link);
return;
}
}
}
}
foreach ($active_paths as $menu_name => $paths) {
foreach ($paths as $path) {
if ($link = menu_link_get_preferred($path)) {
$this
->set_active_trail_from_link($link);
return;
}
}
}
}
function set_active_trail_from_link($item) {
menu_tree_set_path($item['menu_name'], $item['link_path']);
$trail = array();
while ($item) {
array_unshift($trail, $item);
$item = menu_link_load($item['plid']);
}
array_unshift($trail, array(
'title' => t('Home'),
'href' => '<front>',
'link_path' => '',
'localized_options' => array(),
'type' => 0,
));
menu_set_active_trail($trail);
}
function get_active_paths() {
$active_paths = array();
foreach ($this
->get_contexts() as $context) {
$paths = $this
->fetch_from_context($context);
$active_paths = array_merge($active_paths, $paths);
}
$by_menu_name = array();
foreach ($active_paths as $id) {
list($menu_name, $path) = explode(':', $id);
$by_menu_name[$menu_name][] = $path;
}
return $by_menu_name;
}
}