0

In my template preprocess html and page hooks, I test the current page of being a certain kind of node, set by the menu structure. Given such a menu:

main menu

  • link 1
  • link 2 (specials)
  • > link 2.1
  • > link 2.2
  • link 3

Every node which menu's link has the menu parent link 2 gets a treatment. On those nodes, I also want to only show all links inside link 2 menu, so start the main menu from that, so basically the main menu would then consist of link 2.1 and link 2.2. How would you do that?

function mytheme_preprocess_html(&$vars) {

    $special = is_special_page();

    if ($special) {
        // alter menu
    }
}

function mytheme_preprocess_page(&$vars) {

    $special = is_special_page();

    if ($special) {
        // alter menu
    }
}

function is_special_page($node) {
    $breadcrumbs = drupal_get_breadcrumb();
    $businessMatch = preg_grep("/(specials?)/i", $breadcrumbs);

    return (count($businessMatch) > 0);
}

1 Answer 1

1

I've figured it out myself, felt like sharing:

<?php
function _themename_submenu_tree_all_data($menu = 'primary-links', $mlid, $subonly = false) {
    $tree = menu_tree_all_data($menu);
    $childtree = null;

    /*
        thanks Jari http://stackoverflow.com/questions/19112294/how-to-extract-an-array-that-has-a-given-structure-and-whichs-key-value-matc/19112508?noredirect=1#comment28260688_19112508
    */

    function recursiveSearch($array, $key, $value){
      foreach($array as $sub){
        if ($sub['link'][$key] == $value)
          return $sub ;

        if (!empty($sub['below'])){
            $returned = recursiveSearch($sub['below'], $key, $value);
            if ($returned !== null)
                return $returned ;
        }
      }

        return null ;
    }

    $childtree = recursiveSearch($tree, 'mlid', $mlid);

    if ($subonly && $childtree != null) $childtree = $childtree['below'];

    return $childtree;
}

/**
 * Implements hook_preprocess_page().
 */
function themename_preprocess_page(&$vars) {
    if (arg(0) == 'node' && arg(1)) {
        $nid = arg(1);
        $node = node_load($nid);

        $vars['type'] = $node->type;

        // your internal check to see if the menu needs to be truncated

        $isbusiness = _themename_is_business_page($node);

        if ($isbusiness) {
            $trail = menu_get_active_trail();
            $mlid = $trail[ count($trail)-1 ]['mlid'];

            $sub = _themename_submenu_tree_all_data('main-menu', $mlid, true);
            $sub = menu_tree_output($sub);

            $vars['page']['main_menu']['menu_block_2']['#content'] = $sub;
        }
    }
}
?>

Your Answer

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

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