SHOW:
|
|
- or go back to the newest paste.
| 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 | - | * @see http://api.drupal.org/api/drupal/includes%21menu.inc/function/theme_menu_link/7 |
| 5 | + | // .... |
| 6 | ||
| 7 | - | function YOURTHEMENAME_menu_link(array $variables) {
|
| 7 | + | |
| 8 | * @see http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_link/7 | |
| 9 | - | $element_can_be_visible_on_node_id = 9; // SUBSTITUTE IT with your node's id!! |
| 9 | + | |
| 10 | - | $your_path_to_hide = 'node/'.$element_can_be_visible_on_node_id; // SUBSTITUTE IT with yours (when using nodes, use it like this: 'node/9', etc.!) |
| 10 | + | function YOURTHEMENAME_link($variables) {
|
| 11 | - | $is_element_to_hide = ($variables['element']['#href'] == $your_path_to_hide); |
| 11 | + | // in this example, I let "contact" and "node/9" links only be visible if the user is on these pages |
| 12 | - | if($is_element_to_hide){
|
| 12 | + | // and I let some other paths to be displayed at, see below... |
| 13 | - | $element_can_be_visible = (arg(0)=='node' && arg(1) == $element_can_be_visible_on_node_id); |
| 13 | + | $your_paths_to_hide_array = array('contact', 'node/9'); // SUBSTITUTE IT with yours - empty cache every time you change that
|
| 14 | - | |
| 14 | + | // these links will be visible only on their own pages - |
| 15 | - | if(!$element_can_be_visible){
|
| 15 | + | // but here you can define other paths where they can be visible at |
| 16 | - | return ''; |
| 16 | + | // if you don't want any other pages for these to be visible at, then |
| 17 | // leave it empty (like this: array()), | |
| 18 | - | } |
| 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 | - | |
| 19 | + | $is_link_to_hide = in_array($variables['path'], $your_paths_to_hide_array); |
| 20 | - | $element = $variables['element']; |
| 20 | + | if ($is_link_to_hide) {
|
| 21 | - | $sub_menu = ''; |
| 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 | - | if ($element['#below']) {
|
| 23 | + | |
| 24 | - | $sub_menu = drupal_render($element['#below']); |
| 24 | + | if (!$element_can_be_visible) {
|
| 25 | - | } |
| 25 | + | return ''; |
| 26 | - | $output = l($element['#title'], $element['#href'], $element['#localized_options']); |
| 26 | + | } |
| 27 | - | return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n"; |
| 27 | + | |
| 28 | - | } |
| 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 | // .... |