Advertisement
Sk8erPeter

Enable a node's menu item when on that node? /6, exact case

May 30th, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Answer for question on Drupal Stack Exchange:
  2. // http://drupal.stackexchange.com/questions/27907/enable-a-nodes-menu-item-when-on-that-node
  3.  
  4. // this is the template.php file!!
  5. // ....
  6.  
  7.  
  8. /**
  9.  * Should this link be hidden?
  10.  *
  11.  * @see http://drupal.stackexchange.com/questions/27907/enable-a-nodes-menu-item-when-on-that-node/27922
  12.  *
  13.  * @param string $link_path The path that should be inspected.
  14.  * @return boolean
  15.  */
  16. function YOURTHEMENAME_link_should_be_hidden($link_path) {
  17.  
  18.     // in this example, I let "contact" and "node/9" links only be visible
  19.     // on their own path (so for example, I let "contact" path be visible
  20.     // in the menu if the user opened "example.com/contact").    
  21.     $your_paths_to_show_in_menu_only_on_their_own_path = array('node/425'); // SUBSTITUTE IT with yours - empty cache every time you change that
  22.     $is_link_to_hide = in_array($link_path, $your_paths_to_show_in_menu_only_on_their_own_path);
  23.     if ($is_link_to_hide) {
  24.         $is_current_page_self = ($link_path == $_GET['q']);
  25.         // these links will be visible only on their own paths -
  26.         // BUT here you can define other paths too where they can be visible at!
  27.         // So for example, you want "example.com/node/9" to be visible when visiting its own path
  28.         // AND when visiting "example.com/node/3" OR "example.com/any_other_path".
  29.         // But if you do NOT want any other pages for these links to be visible at, then
  30.         // just leave it empty (like this: array()).
  31.         $link_also_visible_at_paths = array(); // SUBSTITUTE IT with your paths! - empty cache every time you change that
  32.         $element_can_be_visible = $is_current_page_self || (!empty($link_also_visible_at_paths) && in_array($_GET['q'], $link_also_visible_at_paths));
  33.  
  34.         if (!$element_can_be_visible) {
  35.             return TRUE;
  36.         }
  37.     }
  38.     return FALSE;
  39. }
  40.  
  41. /**
  42.  * Overriding theme_menu_link()
  43.  *
  44.  * @see http://api.drupal.org/api/drupal/includes%21menu.inc/function/theme_menu_link/7
  45.  */
  46. function YOURTHEMENAME_menu_link(array $variables) {
  47.     /**
  48.      * @see http://drupal.stackexchange.com/questions/27907/enable-a-nodes-menu-item-when-on-that-node/27922
  49.      */
  50.     if (YOURTHEMENAME_link_should_be_hidden($variables['element']['#href'])) {
  51.         return '';
  52.     }
  53.  
  54.     $element = $variables['element'];
  55.     $sub_menu = '';
  56.  
  57.     if ($element['#below']) {
  58.         $sub_menu = drupal_render($element['#below']);
  59.     }
  60.     $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  61.     return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
  62. }
  63.  
  64. /**
  65.  * Helper function that builds the nested lists of a Superfish menu.
  66.  * @see theme_superfish_build()
  67.  */
  68. function YOURTHEMENAME_superfish_build($variables) {
  69.  
  70.     $output = array('content' => '');
  71.     $id = $variables['id'];
  72.     $menu = $variables['menu'];
  73.     $depth = $variables['depth'];
  74.     $trail = $variables['trail'];
  75.     // Keep $sfsettings untouched as we need to pass it to the child menus.
  76.     $settings = $sfsettings = $variables['sfsettings'];
  77.     $megamenu = $settings['megamenu'];
  78.     $total_children = $parent_children = $single_children = 0;
  79.     $i = 1;
  80.  
  81.     // Reckon the total number of available menu items.
  82.     foreach ($menu as $menu_item) {
  83.         if (!isset($menu_item['link']['hidden']) || $menu_item['link']['hidden'] == 0) {
  84.             $total_children++;
  85.         }
  86.     }
  87.  
  88.     foreach ($menu as $menu_item) {
  89.  
  90.         $show_children = $megamenu_wrapper = $megamenu_column = $megamenu_content = FALSE;
  91.         $item_class = $link_options = $link_class = array();
  92.         $mlid = $menu_item['link']['mlid'];
  93.  
  94.         /**
  95.          * @see http://drupal.stackexchange.com/questions/27907/enable-a-nodes-menu-item-when-on-that-node/27922
  96.          */
  97.         if (YOURTHEMENAME_link_should_be_hidden($menu_item['link']['link_path'])) {
  98.             continue;
  99.         }
  100.  
  101.         if (!isset($menu_item['link']['hidden']) || $menu_item['link']['hidden'] == 0) {
  102.             $item_class[] = ($trail && in_array($mlid, $trail)) ? 'active-trail' : '';
  103.  
  104.             // Add helper classes to the menu items and hyperlinks.
  105.             $settings['firstlast'] = ($settings['dfirstlast'] == 1 && $total_children == 1) ? 0 : $settings['firstlast'];
  106.             $item_class[] = ($settings['firstlast'] == 1) ? (($i == 1) ? 'first' : (($i == $total_children) ? 'last' : 'middle')) : '';
  107.             $settings['zebra'] = ($settings['dzebra'] == 1 && $total_children == 1) ? 0 : $settings['zebra'];
  108.             $item_class[] = ($settings['zebra'] == 1) ? (($i % 2) ? 'odd' : 'even') : '';
  109.             $item_class[] = ($settings['itemcount'] == 1) ? 'sf-item-' . $i : '';
  110.             $item_class[] = ($settings['itemdepth'] == 1) ? 'sf-depth-' . $menu_item['link']['depth'] : '';
  111.             $link_class[] = ($settings['itemdepth'] == 1) ? 'sf-depth-' . $menu_item['link']['depth'] : '';
  112.             $item_class[] = ($settings['liclass']) ? $settings['liclass'] : '';
  113.             if (strpos($settings['hlclass'], ' ')) {
  114.                 $l = explode(' ', $settings['hlclass']);
  115.                 foreach ($l as $c) {
  116.                     $link_class[] = $c;
  117.                 }
  118.             }
  119.             else {
  120.                 $link_class[] = $settings['hlclass'];
  121.             }
  122.             $i++;
  123.  
  124.             // Add hyperlinks description (title) to their text.
  125.             $show_linkdescription = ($settings['linkdescription'] == 1 && !empty($menu_item['link']['localized_options']['attributes']['title'])) ? TRUE : FALSE;
  126.             if ($show_linkdescription) {
  127.                 if (!empty($settings['hldmenus'])) {
  128.                     $show_linkdescription = (is_array($settings['hldmenus'])) ? ((in_array($mlid, $settings['hldmenus'])) ? TRUE : FALSE) : (($mlid == $settings['hldmenus']) ? TRUE : FALSE);
  129.                 }
  130.                 if (!empty($settings['hldexclude'])) {
  131.                     $show_linkdescription = (is_array($settings['hldexclude'])) ? ((in_array($mlid, $settings['hldexclude'])) ? FALSE : $show_linkdescription) : (($settings['hldexclude'] == $mlid) ? FALSE : $show_linkdescription);
  132.                 }
  133.                 if ($show_linkdescription) {
  134.                     $menu_item['link']['title'] .= '<span class="sf-description">';
  135.                     $menu_item['link']['title'] .= (!empty($menu_item['link']['localized_options']['attributes']['title'])) ? $menu_item['link']['localized_options']['attributes']['title'] : array();
  136.                     $menu_item['link']['title'] .= '</span>';
  137.                     $link_options['html'] = TRUE;
  138.                 }
  139.             }
  140.  
  141.             // Add custom HTML codes around the menu items.
  142.             if ($sfsettings['wrapul'] && strpos($sfsettings['wrapul'], ',') !== FALSE) {
  143.                 $wul = explode(',', $sfsettings['wrapul']);
  144.                 // In case you just wanted to add something after the element.
  145.                 if (drupal_substr($sfsettings['wrapul'], 0) == ',') {
  146.                     array_unshift($wul, '');
  147.                 }
  148.             }
  149.             else {
  150.                 $wul = array();
  151.             }
  152.  
  153.             // Add custom HTML codes around the hyperlinks.
  154.             if ($settings['wraphl'] && strpos($settings['wraphl'], ',') !== FALSE) {
  155.                 $whl = explode(',', $settings['wraphl']);
  156.                 // The same as above
  157.                 if (drupal_substr($settings['wraphl'], 0) == ',') {
  158.                     array_unshift($whl, '');
  159.                 }
  160.             }
  161.             else {
  162.                 $whl = array();
  163.             }
  164.  
  165.             // Add custom HTML codes around the hyperlinks text.
  166.             if ($settings['wraphlt'] && strpos($settings['wraphlt'], ',') !== FALSE) {
  167.                 $whlt = explode(',', $settings['wraphlt']);
  168.                 // The same as above
  169.                 if (drupal_substr($settings['wraphlt'], 0) == ',') {
  170.                     array_unshift($whlt, '');
  171.                 }
  172.                 $menu_item['link']['title'] = $whlt[0] . check_plain($menu_item['link']['title']) . $whlt[1];
  173.                 $link_options['html'] = TRUE;
  174.             }
  175.  
  176.  
  177.             if (!empty($menu_item['link']['has_children']) && !empty($menu_item['below']) && $depth != 0) {
  178.                 // Megamenu is still beta, there is a good chance much of this will be changed.
  179.                 if (!empty($settings['megamenu_exclude'])) {
  180.                     if (is_array($settings['megamenu_exclude'])) {
  181.                         $megamenu = (in_array($mlid, $settings['megamenu_exclude'])) ? 0 : $megamenu;
  182.                     }
  183.                     else {
  184.                         $megamenu = ($settings['megamenu_exclude'] == $mlid) ? 0 : $megamenu;
  185.                     }
  186.                     // Send the result to the sub-menu.
  187.                     $sfsettings['megamenu'] = $megamenu;
  188.                 }
  189.                 if ($megamenu == 1) {
  190.                     $megamenu_wrapper = ($menu_item['link']['depth'] == $settings['megamenu_depth']) ? TRUE : FALSE;
  191.                     $megamenu_column = ($menu_item['link']['depth'] == $settings['megamenu_depth'] + 1) ? TRUE : FALSE;
  192.                     $megamenu_content = ($menu_item['link']['depth'] >= $settings['megamenu_depth'] && $menu_item['link']['depth'] <= $settings['megamenu_levels']) ? TRUE : FALSE;
  193.                 }
  194.                 // Render the sub-menu.
  195.                 $var = array(
  196.                   'id' => $id,
  197.                   'menu' => $menu_item['below'],
  198.                   'depth' => $depth, 'trail' => $trail,
  199.                   'sfsettings' => $sfsettings
  200.                 );
  201.                 $children = theme('superfish_build', $var);
  202.                 // Check to see whether it should be displayed.
  203.                 $show_children = (($menu_item['link']['depth'] <= $depth || $depth == -1) && $children['content']) ? TRUE : FALSE;
  204.                 if ($show_children) {
  205.                     // Add item counter classes.
  206.                     if ($settings['itemcounter']) {
  207.                         $item_class[] = 'sf-total-children-' . $children['total_children'];
  208.                         $item_class[] = 'sf-parent-children-' . $children['parent_children'];
  209.                         $item_class[] = 'sf-single-children-' . $children['single_children'];
  210.                     }
  211.                     // More helper classes.
  212.                     $item_class[] = ($megamenu_column) ? 'sf-megamenu-column' : '';
  213.                     $item_class[] = $link_class[] = 'menuparent';
  214.                 }
  215.                 $parent_children++;
  216.             }
  217.             else {
  218.                 $item_class[] = 'sf-no-children';
  219.                 $single_children++;
  220.             }
  221.  
  222.             $item_class = implode(' ', array_filter($item_class));
  223.  
  224.             if (isset($menu_item['link']['localized_options']['attributes']['class'])) {
  225.                 $link_class_current = $menu_item['link']['localized_options']['attributes']['class'];
  226.                 $link_class = array_merge($link_class_current, array_filter($link_class));
  227.             }
  228.             $menu_item['link']['localized_options']['attributes']['class'] = $link_class;
  229.  
  230.             $link_options['attributes'] = $menu_item['link']['localized_options']['attributes'];
  231.  
  232.             // Render the menu item.
  233.             $output['content'] .= '<li id="menu-' . $mlid . '-' . $id . '"';
  234.             $output['content'] .= ($item_class) ? ' class="' . trim($item_class) . '">' : '>';
  235.             $output['content'] .= ($megamenu_column) ? '<div class="sf-megamenu-column">' : '';
  236.             $output['content'] .= isset($whl[0]) ? $whl[0] : '';
  237.             $output['content'] .= l($menu_item['link']['title'], $menu_item['link']['link_path'], $link_options);
  238.             $output['content'] .= isset($whl[1]) ? $whl[1] : '';
  239.             $output['content'] .= ($megamenu_wrapper) ? '<ul class="sf-megamenu"><li class="sf-megamenu-wrapper ' . $item_class . '">' : '';
  240.             $output['content'] .= ($show_children) ? (isset($wul[0]) ? $wul[0] : '') : '';
  241.             $output['content'] .= ($show_children) ? (($megamenu_content) ? '<ol>' : '<ul>') : '';
  242.             $output['content'] .= ($show_children) ? $children['content'] : '';
  243.             $output['content'] .= ($show_children) ? (($megamenu_content) ? '</ol>' : '</ul>') : '';
  244.             $output['content'] .= ($show_children) ? (isset($wul[1]) ? $wul[1] : '') : '';
  245.             $output['content'] .= ($megamenu_wrapper) ? '</li></ul>' : '';
  246.             $output['content'] .= ($megamenu_column) ? '</div>' : '';
  247.             $output['content'] .= '</li>';
  248.         }
  249.     }
  250.     $output['total_children'] = $total_children;
  251.     $output['parent_children'] = $parent_children;
  252.     $output['single_children'] = $single_children;
  253.     return $output;
  254. }
  255.  
  256. /**
  257.  * Overriding theme_site_map_menu_link()
  258.  *
  259.  * @param array $variables
  260.  * @return string
  261.  */
  262. function YOURTHEMENAME_site_map_menu_link($variables) {
  263.  
  264.     $element = $variables['element'];
  265.     $sub_menu = '';
  266.  
  267.     /**
  268.      * @see http://drupal.stackexchange.com/questions/27907/enable-a-nodes-menu-item-when-on-that-node/27922
  269.      */
  270.     if (YOURTHEMENAME_link_should_be_hidden($element['#href'])) {
  271.         return '';
  272.     }
  273.  
  274.     if ($element['#below']) {
  275.         $sub_menu = drupal_render($element['#below']);
  276.     }
  277.     $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  278.     return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
  279. }
  280.  
  281. /**
  282.  * Returns HTML for a set of links.
  283.  * @see theme_links()
  284.  */
  285. function YOURTHEMENAME_links($variables) {
  286.     $links = $variables['links'];
  287.     $attributes = $variables['attributes'];
  288.     $heading = $variables['heading'];
  289.     global $language_url;
  290.     $output = '';
  291.  
  292.     if (count($links) > 0) {
  293.         $output = '';
  294.  
  295.         // Treat the heading first if it is present to prepend it to the
  296.         // list of links.
  297.         if (!empty($heading)) {
  298.             if (is_string($heading)) {
  299.                 // Prepare the array that will be used when the passed heading
  300.                 // is a string.
  301.                 $heading = array(
  302.                   'text' => $heading,
  303.                   // Set the default level of the heading.
  304.                   'level' => 'h2',
  305.                 );
  306.             }
  307.             $output .= '<' . $heading['level'];
  308.             if (!empty($heading['class'])) {
  309.                 $output .= drupal_attributes(array('class' => $heading['class']));
  310.             }
  311.             $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
  312.         }
  313.  
  314.         $output .= '<ul' . drupal_attributes($attributes) . '>';
  315.  
  316.         $num_links = count($links);
  317.         $i = 1;
  318.  
  319.         foreach ($links as $key => $link) {
  320.  
  321.             /**
  322.              * @see http://drupal.stackexchange.com/questions/27907/enable-a-nodes-menu-item-when-on-that-node/27922
  323.              */
  324.             if (YOURTHEMENAME_link_should_be_hidden($link['href'])) {
  325.                 continue;
  326.             }
  327.  
  328.  
  329.             $class = array($key);
  330.  
  331.             // Add first, last and active classes to the list of links to help out themers.
  332.             if ($i == 1) {
  333.                 $class[] = 'first';
  334.             }
  335.             if ($i == $num_links) {
  336.                 $class[] = 'last';
  337.             }
  338.             if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
  339.                 && (empty($link['language']) || $link['language']->language == $language_url->language)) {
  340.                 $class[] = 'active';
  341.             }
  342.             $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
  343.  
  344.             if (isset($link['href'])) {
  345.                 // Pass in $link as $options, they share the same keys.
  346.                 $output .= l($link['title'], $link['href'], $link);
  347.             }
  348.             elseif (!empty($link['title'])) {
  349.                 // Some links are actually not links, but we wrap these in <span> for adding title and class attributes.
  350.                 if (empty($link['html'])) {
  351.                     $link['title'] = check_plain($link['title']);
  352.                 }
  353.                 $span_attributes = '';
  354.                 if (isset($link['attributes'])) {
  355.                     $span_attributes = drupal_attributes($link['attributes']);
  356.                 }
  357.                 $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
  358.             }
  359.  
  360.             $i++;
  361.             $output .= "</li>\n";
  362.         }
  363.  
  364.         $output .= '</ul>';
  365.     }
  366.  
  367.     return $output;
  368. }
  369.  
  370. /**
  371.  * Theming links (theme_link())
  372.  * @see http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_link/7
  373.  *
  374.  */
  375. function YOURTHEMENAME_link($variables) {
  376.     /**
  377.      * @see http://drupal.stackexchange.com/questions/27907/enable-a-nodes-menu-item-when-on-that-node/27922
  378.      */
  379.     if (YOURTHEMENAME_link_should_be_hidden($variables['path'])) {
  380.         return '';
  381.     }
  382.  
  383.     return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
  384. }
  385.  
  386. // ....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement