1

If you are new to Drupal, as I am, you surely asked yourself once about "How can I highlight parent Views menu item even if I am on a child node? And how can I keep a correct breadcrumb if I use one?"... That's why I wanted to share a little clue in here.

My situation:

I have a content-type named "Testimonial". I created a View for my testimonial's nodes, with a menu item "Témoignages" in a secondary menu on the left of my page (called "Ressources").

First problem: the menu

/!\ This menu displays only on the pages whose menu item is present in the menu. For example, "Marchés publics en Wallonie", "Aides publiques en Wallonie", "Témoignages" (= Testimonials), ...

enter image description here

When I am the the View page, with the list of my testimonial's nodes, the menu item is highlighted automatically by Drupal. Nice.

But when I click on a node and access the full view page, the "Ressources" menu disappears. This behaviour is normal for all other menu item's children pages. But I want to make an exception for the testimonials.

Second problem: the breadcrumb

Breadcrumb on Views page:

enter image description here

Breadcrumb on testimonials node page:

enter image description here

My final question:

How can I keep the menu block in testimonials node pages (only) and make the menu item "Témoignages" highlighted? And how can I keep a correct breadcrumb in the same time?

1
  • Hi! I don#t have time for a complete answer, but I usually use Crumbs + Menupoly for this exact problem.
    – donquixote
    Commented Jul 10, 2014 at 23:38

1 Answer 1

0

To display the "Ressources" menu in a testimonial node page, Drupal should believe that we are actually on the "Témoignages" Views page. Plus, it will highlight automatically the good menu item.

But, we also need a correct breadcrumb. That's why we have to save in a variable the "true nature of the page" (a node) before letting know to Drupal that we are on a view.

That's that we do here:

function pwe_testimonial_node_view($node, $view_mode, $langcode) {
  // When we are on a Testimonial node page
  if($node->type == "testimonial" && $view_mode == "full") {
    // We save the true nature of the page
    $_SESSION['pwe_testimonial_active_trail'] = serialize(menu_get_active_trail());
    $_GET['pwe_node_id'] = $node->nid;
    // And set the active menu item as "Témoignages"
    menu_set_active_item("testimonial");
  } else {
    // If we are not on a Testimonial node page,
    // we unset the custom variable.
    if(isset($_SESSION['pwe_testimonial_active_trail'])) {
      unset($_SESSION['pwe_testimonial_active_trail']);
    }
  }
}

To rebuild the true breadcrumb, we use the variable saved in previous function.

function pwe_testimonial_menu_breadcrumb_alter(&$active_trail, $item) {
  if($item && $item['path'] == "testimonial") {
    if($_GET['pwe_node_id']) {
      if(isset($_SESSION['pwe_testimonial_active_trail'])) {
        unset($_SESSION['pwe_testimonial_active_trail']);
      }
    }
    if(isset($_SESSION['pwe_testimonial_active_trail'])) {
      $original_active_trail = unserialize($_SESSION['pwe_testimonial_active_trail']);
      if(is_array($original_active_trail)) {
        $total = count($active_trail);
        $active_trail[$total] = $item;
      }
    }
  }
}

enter image description here

That's all. I hope this little Q&A sharing knowledge will help someone in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.