context_reaction_menu.inc

  1. cis7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
  2. cle7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
  3. ecd7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
  4. elmsmedia7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
  5. harmony7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
  6. icor7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
  7. meedjum_blog7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
  8. mooc7 sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc

Classes

Namesort descending Description
context_reaction_menu Expose menu items as context reactions.

File

sites/all/modules/ulmus/context/plugins/context_reaction_menu.inc
View source
  1. <?php
  2. /**
  3. * Expose menu items as context reactions.
  4. */
  5. class context_reaction_menu extends context_reaction {
  6. /**
  7. * Provide a form element that allow the admin to chose a menu item.
  8. */
  9. function options_form($context) {
  10. if (module_exists('menu')) {
  11. $menus = menu_parent_options(menu_get_menus(), array('mlid' => 0));
  12. $root_menus = array();
  13. foreach ($menus as $key => $name) {
  14. $id = explode(':', $key);
  15. if ($id[1] == '0') {
  16. $root_menus[$id[0]] = check_plain($name);
  17. }
  18. else {
  19. $link = menu_link_load($id[1]);
  20. $identifier = $link['link_path'];
  21. $root_menu = $root_menus[$id[0]];
  22. while (isset($menus[$root_menu][$identifier])) {
  23. $identifier .= "'";
  24. }
  25. $menus[$root_menu][$identifier] = $name;
  26. }
  27. unset($menus[$key]);
  28. }
  29. array_unshift($menus, "-- " . t('None') . " --");
  30. }
  31. else {
  32. $menus = array();
  33. }
  34. return array(
  35. '#title' => $this->title,
  36. '#description' => $this->description,
  37. '#options' => $menus,
  38. '#type' => 'select',
  39. '#default_value' => $this->fetch_from_context($context),
  40. );
  41. }
  42. /**
  43. * Override of options_form_submit().
  44. * Trim any identifier padding for non-unique path menu items.
  45. */
  46. function options_form_submit($values) {
  47. return trim($values, "'");
  48. }
  49. /**
  50. * If primary + secondary links are pointed at the same menu, provide
  51. * contextual trailing by default.
  52. */
  53. function execute(&$vars = NULL) {
  54. if (variable_get('menu_main_links_source', 'main-menu') == variable_get('menu_secondary_links_source', 'user-menu')) {
  55. $vars['main_menu'] = theme_get_setting('toggle_main_menu') ? $this->menu_navigation_links(variable_get('menu_main_links_source', 'main-menu')) : $vars['main_menu'];
  56. $vars['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? $this->menu_navigation_links(variable_get('menu_secondary_links_source', 'secondary-links'), 1) : $vars['secondary_menu'];
  57. }
  58. $vars['main_menu'] = $this->menu_set_active($vars['main_menu']);
  59. $vars['secondary_menu'] = $this->menu_set_active($vars['secondary_menu']);
  60. }
  61. function get_active_paths() {
  62. $active_paths = array();
  63. foreach ($this->get_contexts() as $context) {
  64. if (isset($context->reactions[$this->plugin])) {
  65. $active_paths[] = $context->reactions[$this->plugin];
  66. }
  67. }
  68. return $active_paths;
  69. }
  70. /**
  71. * Iterates through a provided links array for use with theme_links()
  72. * (e.g. from menu_primary_links()) and provides an active class for
  73. * any items that have a path that matches an active context.
  74. *
  75. * @param $links
  76. * An array of links.
  77. * @param $reset
  78. * A boolean flag for resetting the static cache.
  79. *
  80. * @return
  81. * A modified links array.
  82. */
  83. function menu_set_active($links = array(), $reset = FALSE) {
  84. $new_links = array();
  85. if (!empty($links)) {
  86. $active_paths = $this->get_active_paths();
  87. // Iterate through the provided links and build a new set of links
  88. // that includes active classes
  89. foreach ($links as $key => $link) {
  90. if (!empty($link['href']) && in_array($link['href'], $active_paths)) {
  91. $link['attributes']['class'][] = 'active';
  92. if (strpos(' active', $key) === FALSE) {
  93. $new_links[$key . ' active'] = $link;
  94. }
  95. }
  96. else {
  97. $new_links[$key] = $link;
  98. }
  99. }
  100. }
  101. return $new_links;
  102. }
  103. /**
  104. * Wrapper around menu_navigation_links() that gives themers the option of
  105. * building navigation links based on an active context trail.
  106. */
  107. function menu_navigation_links($menu_name, $level = 0) {
  108. // Retrieve original path so we can repair it after our hack.
  109. $original_path = $_GET['q'];
  110. $original_menu_trail = drupal_static('menu_set_active_trail');
  111. // Retrieve the first active menu path found.
  112. if ($active_paths = $this->get_active_paths()) {
  113. $path = current($active_paths);
  114. if (menu_get_item($path)) {
  115. menu_set_active_item($path);
  116. }
  117. }
  118. // Build the links requested
  119. if (module_exists('i18n_menu')) {
  120. $links = i18n_menu_navigation_links($menu_name, $level);
  121. } else {
  122. $links = menu_navigation_links($menu_name, $level);
  123. }
  124. // Repair and get out
  125. menu_set_active_item($original_path);
  126. $repair_menu_trail = &drupal_static('menu_set_active_trail');
  127. $repair_menu_trail = $original_menu_trail;
  128. return $links;
  129. }
  130. }