context_reaction_menu.inc

Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * Expose menu items as context reactions.
00005  */
00006 class context_reaction_menu extends context_reaction {
00007   /**
00008    * Provide a form element that allow the admin to chose a menu item.
00009    */
00010   function options_form($context) {
00011     if (module_exists('menu')) {
00012       $menus = menu_parent_options(menu_get_menus(), array('mlid' => 0));
00013       $root_menus = array();
00014       foreach ($menus as $key => $name) {
00015         $id = explode(':', $key);
00016         if ($id[1] == '0') {
00017           $root_menus[$id[0]] = check_plain($name);
00018         }
00019         else {
00020           $link = menu_link_load($id[1]);
00021           $identifier = $link['link_path'];
00022           $root_menu = $root_menus[$id[0]];
00023           while (isset($menus[$root_menu][$identifier])) {
00024             $identifier .= "'";
00025           }
00026           $menus[$root_menu][$identifier] = $name;
00027         }
00028         unset($menus[$key]);
00029       }
00030       array_unshift($menus, "-- " . t('None') . " --");
00031     }
00032     else {
00033       $menus = array();
00034     }
00035     return array(
00036       '#title' => $this->title,
00037       '#description' => $this->description,
00038       '#options' => $menus,
00039       '#type' => 'select',
00040       '#default_value' => $this->fetch_from_context($context),
00041     );
00042   }
00043 
00044   /**
00045    * Override of options_form_submit().
00046    * Trim any identifier padding for non-unique path menu items.
00047    */
00048   function options_form_submit($values) {
00049     return trim($values, "'");
00050   }
00051 
00052   /**
00053    * If primary + secondary links are pointed at the same menu, provide
00054    * contextual trailing by default.
00055    */
00056   function execute(&$vars = NULL) {
00057     if (variable_get('menu_main_links_source', 'main-menu') == variable_get('menu_secondary_links_source', 'user-menu')) {
00058       $vars['main_menu'] = theme_get_setting('toggle_main_menu') ? $this->menu_navigation_links(variable_get('menu_main_links_source', 'main-menu')) : $vars['main_menu'];
00059       $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'];
00060     }
00061 
00062     $vars['main_menu'] = $this->menu_set_active($vars['main_menu']);
00063     $vars['secondary_menu'] = $this->menu_set_active($vars['secondary_menu']);
00064   }
00065 
00066   function get_active_paths() {
00067     $active_paths = array();
00068     foreach ($this->get_contexts() as $context) {
00069       if (isset($context->reactions[$this->plugin])) {
00070         $active_paths[] = $context->reactions[$this->plugin];
00071       }
00072     }
00073     return $active_paths;
00074   }
00075 
00076   /**
00077    * Iterates through a provided links array for use with theme_links()
00078    * (e.g. from menu_primary_links()) and provides an active class for
00079    * any items that have a path that matches an active context.
00080    *
00081    * @param $links
00082    *   An array of links.
00083    * @param $reset
00084    *   A boolean flag for resetting the static cache.
00085    *
00086    * @return
00087    *   A modified links array.
00088    */
00089   function menu_set_active($links = array(), $reset = FALSE) {
00090     $new_links = array();
00091     if (!empty($links)) {
00092       $active_paths = $this->get_active_paths();
00093 
00094       // Iterate through the provided links and build a new set of links
00095       // that includes active classes
00096       foreach ($links as $key => $link) {
00097         if (!empty($link['href']) && in_array($link['href'], $active_paths)) {
00098           $link['attributes']['class'][] = 'active';
00099 
00100           if (strpos(' active', $key) === FALSE) {
00101             $new_links[$key . ' active'] = $link;
00102           }
00103         }
00104         else {
00105           $new_links[$key] = $link;
00106         }
00107       }
00108     }
00109     return $new_links;
00110   }
00111 
00112   /**
00113    * Wrapper around menu_navigation_links() that gives themers the option of
00114    * building navigation links based on an active context trail.
00115    */
00116   function menu_navigation_links($menu_name, $level = 0) {
00117     // Retrieve original path so we can repair it after our hack.
00118     $original_path = $_GET['q'];
00119     $original_menu_trail = drupal_static('menu_set_active_trail');
00120 
00121     // Retrieve the first active menu path found.
00122     if ($active_paths = $this->get_active_paths()) {
00123       $path = current($active_paths);
00124       if (menu_get_item($path)) {
00125         menu_set_active_item($path);
00126       }
00127     }
00128 
00129     // Build the links requested
00130     if (module_exists('i18n_menu')) {
00131       $links = i18n_menu_navigation_links($menu_name, $level);
00132     } else {
00133       $links = menu_navigation_links($menu_name, $level);
00134     }
00135 
00136     // Repair and get out
00137     menu_set_active_item($original_path);
00138     $repair_menu_trail = &drupal_static('menu_set_active_trail');
00139     $repair_menu_trail = $original_menu_trail;
00140     return $links;
00141   }
00142 }

Generated on Fri May 24 02:23:53 2013 for Context by  doxygen 1.4.7