Advertisement
Sk8erPeter

Enable a node's menu item when on that node? /3, renewed

Apr 13th, 2012
332
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.  * @see http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_link/7
  9.  */
  10. function YOURTHEMENAME_link($variables) {
  11.     // in this example, I let "contact" and "node/9" links only be visible if the user is on these pages
  12.     // and I let some other paths to be displayed at, see below...
  13.     $your_paths_to_hide_array = array('contact', 'node/9'); // SUBSTITUTE IT with yours - empty cache every time you change that
  14.     // these links will be visible only on their own pages -
  15.     // but here you can define other paths where they can be visible at
  16.     // if you don't want any other pages for these to be visible at, then
  17.     // leave it empty (like this: array()),
  18.     $link_also_visible_on_paths = array('node/3', 'any_other_path'); // SUBSTITUTE IT with your paths! - empty cache every time you change that
  19.     $is_link_to_hide = in_array($variables['path'], $your_paths_to_hide_array);
  20.     if ($is_link_to_hide) {
  21.         $is_current_page_self = ($variables['path'] == $_GET['q']);
  22.         $element_can_be_visible = $is_current_page_self || (!empty($link_also_visible_on_paths) && in_array($_GET['q'], $link_also_visible_on_paths));
  23.  
  24.         if (!$element_can_be_visible) {
  25.             return '';
  26.         }
  27.     }
  28.     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>';
  29. }
  30.  
  31. // ....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement