Last active
December 28, 2017 22:30
-
-
Save oranges13/148efd3b089f560e0b4290a40687d699 to your computer and use it in GitHub Desktop.
context 3.7 patch heirarchy fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/plugins/context_condition_menu.inc b/plugins/context_condition_menu.inc | |
index 4b19e02..7582497 100644 | |
--- a/plugins/context_condition_menu.inc | |
+++ b/plugins/context_condition_menu.inc | |
@@ -1,5 +1,7 @@ | |
<?php | |
+require_once('context_menu_helpers.inc'); | |
+ | |
/** | |
* Expose menu items as a context condition. | |
*/ | |
@@ -9,21 +11,23 @@ class context_condition_menu extends context_condition { | |
*/ | |
function condition_values() { | |
if (module_exists('menu')) { | |
- $menus = menu_parent_options(menu_get_menus(), array('mlid' => 0)); | |
+ $menus = context_menu_parent_options(menu_get_menus(), array('mlid' => 0)); | |
$root_menus = array(); | |
- foreach ($menus as $key => $name) { | |
+ foreach ($menus as $key => $link) { | |
$id = explode(':', $key); | |
if ($id[1] == '0') { | |
- $root_menus[$id[0]] = $name; | |
+ $root_menus[$id[0]] = $link; | |
} | |
else { | |
- $link = menu_link_load($id[1]); | |
+ if (!is_array($link)) { | |
+ $link = menu_link_load($id[1]); | |
+ } | |
$identifier = $link['link_path']; | |
$root_menu = $root_menus[$id[0]]; | |
while (isset($menus[$root_menu][$identifier])) { | |
$identifier .= "'"; | |
} | |
- $menus[$root_menu][$identifier] = $name; | |
+ $menus[$root_menu][$identifier] = $link['title']; | |
} | |
unset($menus[$key]); | |
} | |
diff --git a/plugins/context_menu_helpers.inc b/plugins/context_menu_helpers.inc | |
new file mode 100644 | |
index 0000000..fff3ebc | |
--- /dev/null | |
+++ b/plugins/context_menu_helpers.inc | |
@@ -0,0 +1,72 @@ | |
+<?php | |
+ | |
+function context_menu_parent_options($menus, $item, $type = '') { | |
+ // The menu_links table can be practically any size and we need a way to | |
+ // allow contrib modules to provide more scalable pattern choosers. | |
+ // hook_form_alter is too late in itself because all the possible parents are | |
+ // retrieved here, unless menu_override_parent_selector is set to TRUE. | |
+ if (variable_get('menu_override_parent_selector', FALSE)) { | |
+ return array(); | |
+ } | |
+ | |
+ $available_menus = array(); | |
+ if (!is_array($item)) { | |
+ // If $item is not an array then it is a node type. | |
+ // Use it as $type and prepare a dummy menu item for _menu_get_options(). | |
+ $type = $item; | |
+ $item = array('mlid' => 0); | |
+ } | |
+ if (empty($type)) { | |
+ // If no node type is set, use all menus given to this function. | |
+ $available_menus = $menus; | |
+ } | |
+ else { | |
+ // If a node type is set, use all available menus for this type. | |
+ $type_menus = variable_get('menu_options_' . $type, array('main-menu' => 'main-menu')); | |
+ foreach ($type_menus as $menu) { | |
+ $available_menus[$menu] = $menu; | |
+ } | |
+ } | |
+ | |
+ return _context_menu_get_options($menus, $available_menus, $item); | |
+} | |
+ | |
+function _context_menu_get_options($menus, $available_menus, $item) { | |
+ // If the item has children, there is an added limit to the depth of valid parents. | |
+ if (isset($item['parent_depth_limit'])) { | |
+ $limit = $item['parent_depth_limit']; | |
+ } | |
+ else { | |
+ $limit = _menu_parent_depth_limit($item); | |
+ } | |
+ | |
+ $options = array(); | |
+ foreach ($menus as $menu_name => $title) { | |
+ if (isset($available_menus[$menu_name])) { | |
+ $tree = menu_tree_all_data($menu_name, NULL); | |
+ $options[$menu_name . ':0'] = '<' . $title . '>'; | |
+ _context_menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit); | |
+ } | |
+ } | |
+ return $options; | |
+} | |
+ | |
+function _context_menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) { | |
+ foreach ($tree as $data) { dpm($tree); | |
+ if ($data['link']['depth'] > $depth_limit) { | |
+ // Don't iterate through any links on this level. | |
+ break; | |
+ } | |
+ if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) { | |
+ $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, FALSE); | |
+ if ($data['link']['hidden']) { | |
+ $title .= ' (' . t('disabled') . ')'; | |
+ } | |
+ $data['title'] = $title; | |
+ $options[$menu_name . ':' . $data['link']['mlid']] = $data['link']; | |
+ if ($data['below']) { | |
+ _context_menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit); | |
+ } | |
+ } | |
+ } | |
+} | |
diff --git a/plugins/context_reaction_menu.inc b/plugins/context_reaction_menu.inc | |
index 4f0e54c..3ba8b84 100644 | |
--- a/plugins/context_reaction_menu.inc | |
+++ b/plugins/context_reaction_menu.inc | |
@@ -1,5 +1,7 @@ | |
<?php | |
+require_once('context_menu_helpers.inc'); | |
+ | |
/** | |
* Expose menu items as context reactions. | |
*/ | |
@@ -10,22 +12,24 @@ 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)); | |
+ $menus = context_menu_parent_options(menu_get_menus(), array('mlid' => 0)); | |
$menu_names = array(); | |
- foreach ($menus as $id => $title) { | |
+ foreach ($menus as $id => $link) { | |
list($menu_name, $mlid) = explode(':', $id); | |
// Store the title each menu for reference. | |
if ($mlid == '0') { | |
- $menu_names[$menu_name] = $title; | |
+ $menu_names[$menu_name] = check_plain($link); | |
} | |
else { | |
- $link = menu_link_load($mlid); | |
+ if (!is_array($link)) { | |
+ $link = menu_link_load($id[1]); | |
+ } | |
$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; | |
+ $options[$root_menu][$menu_name . ':' . $identifier] = $link['title']; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment