We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22770
    • 285 Posts
    Hi All. I’m plowing my way through Wayfinder, and I think I’ve got it figured out for fairly simple menus. But I’ve got a slightly more complicated case and just can’t work out how to solve it. Here’s what I want to do. I’ve got a four level menu, a bit like this:

    Level 1 item
    Level 1 item
    - Level 2 item
    - Level 2 item
    -- Level 3 item
    -- Level 3 item
    --- Level 4 item
    --- Level 4 item
    -- Many other level 3 items
    - Level 2 item
    Level 1 item

    And so on. The problem is that I have far too many Level 3 items to display at the same time. I want to keep most of them and their child (level 4) items hidden unless that particular level 3 item or one of its child (level 4) items is selected. At that point I want to display that particular level 3 item and all its child level 4 items. I do want to keep a few of the level 3 items visible at all times, though. Items in red would be the ones to disappear if none of them were selected.

    Anyone have any idea how I might go about doing that?

    Thanks!
      • 33337
      • 3,975 Posts
      Use
      &hideSubMenus=`true`
      

      The sub-menus will appear only if its parent menu is active.
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 22770
        • 285 Posts
        Hi zi. Thanks for the reply.

        I think I must have explained the problem badly, though. If I set &hideSubMenus=`true`, that will hide the level 4 items until the appropriate parent is selected. But what I want to do is display only a limited number of level 3 items at any one time. I could do that by choosing not to display the other items in the menu when I edit their document settings. But then I couldn’t get them to reappear again when their children are selected. Still not sure I’m explaining this very clearly.
          • 33337
          • 3,975 Posts
          Well, I guess I’ll let someone more experienced shed some light on this.
            Zaigham R - MODX Professional | Skype | Email | Twitter

            Digging the interwebs for #MODX gems and bringing it to you. modx.link
            • 22770
            • 285 Posts
            Okay, I’ve come up with a solution, which may not be very pretty but which works. Basically, I’ve written a little snippet to carry out what I want it to do. It’s only useful for this very particular case. People who know the API better or who know PHP better will no doubt be able to come up with something more useful and efficient.

            <?php
            if(!isset($id)) { //Find current document id.
            	$id=$modx->documentIdentifier;
            }
            
            if($modx->documentObject['isfolder'] == 1) {
            	if($modx->documentObject['hidemenu'] == 0) { //If the page is a container and it is NOT hidden, then call wayfinder with hidden items still hidden. Otherwise, find parent of document.
            		return $modx->runSnippet('Wayfinder', array('startId'=>'0', 'level'=>'4', 'hideSubMenus'=>'TRUE', etc));
            	} else { //Otherwise, make sure this folder will be included later.
            		$includeId = $id;
            	}	
            } else { //If this is not a container, then find the parent of the document (which will be a folder) and check to see if it is hidden. If it is hidden, make sure will be included later.
            	$parentId = $modx->documentObject['parent'];
            	$result = $modx->db->query( "SELECT hidemenu FROM modx_site_content WHERE id=$parentId" );
            	$row = $modx->db->getRow( $result ); 
            	if($row['hidemenu'] == 1) { //If hidden, ensure will be displayed.
            		$includeId = $parentId;
            	} else { //If not hidden, run wayfinder as normal.
            		return $modx->runSnippet('Wayfinder', array('startId'=>'0', 'level'=>'4', 'hideSubMenus'=>'TRUE', etc));
            	}
            }
            if(isset($includeId)) { //If there we have selected a hidden container, or if we have selected the child of a hidden container, find all hidden documents
            	$result = $modx->db->query( "SELECT id FROM modx_site_content WHERE hidemenu = 1" );
            	$excludeIds = '';
            	while($row = $modx->db->getRow( $result )) { //Cycle through hidden documents, and concatenate them in a comma-delimited string.
            		if($row['id'] != $includeId) { //Except for the identified container.
            			$excludeIds .= $row['id'] . ",";
            		}
            	}
            	$excludeIds = substr($excludeIds, 0, -1); // Remove redundent comma from end of list.
            	//Finally, run wayfinder with hidden documents shown, but with submenus hidden and all other hidden documents excluded.
            	return $modx->runSnippet('Wayfinder', array('startId'=>'0', 'level'=>'4', 'hideSubMenus'=>'TRUE', 'excludeDocs'=>$excludeIds, etc));
            }
            ?>


            Snippets are fun! Sometime I’m going to learn to do them better!