Jump to content

Dynamically build a menu with infinite optional children


a-ok
 Share

Recommended Posts

I'm attempting to build a dynamic filtering menu. I have two main templates ('activities' and 'works') and these all share the same children template 'singleA'.

Work
  - Work a (singleA)
  - Work b (singleA)
Activities
  - Activity a (singleA)
  - Activity b (singleA)

Within each (singleA) template you can tag it with as many keywords as you wish. These keywords all share the same template 'keyword' and can have infinite children.

Keywords
  - Event (keyword)
    - Talk (keyword)
  - Exhibition (keyword)
    - Solo (keyword)
    - Group (keyword)

What I am wanting to achieve is when a user clicks on 'Work' in a menu, it'll show all the keywords with a depth of 1 that have 'works' (singleA) tagged. Then, when the user clicks the keyword 'Exhibition', for example, it'll show all the keywords, with a depth of 2 etc. I'm using @Robin S's `Connect Page Fields` for this. All the data is set up it's just I'm getting stuck trying to work out how dynamically build this as the depths could be 1, 2, 3 or 40 (unlikely but still keen to keep it dynamic).

Any thoughts?

$menuPages = $pages->find("template=activities|works");
foreach ($menuPages as $menuPage) {

    $keywordsAll = $pages->find("template=keyword, singles.parent=$menuPage");
    foreach ($keywordsAll as $keyword) {
        
    }

}

 

Link to comment
Share on other sites

Are you using URL Segments or query parameters to detect the currently selected keyword? I don't fully understand your setup. What is the "singles" field you use in your selector? I don't think you need Connect Page Fields for what you're trying to do though.

Anyway, once you have your keyword, you can build a query to find all keywords one level below that using the parent selector:

$keyword = $input->urlSegment1;
$keywordPage = $pages->get('name=' . $sanitizer->selectorValue($keyword));
if ($keywordPage && $keywordPage->id) {
	$keywordsBelow = $pages->find("template=keywords, parent={$keywordPage}");
	// ... use keywordsBelow to build your filter options
}

Does this help or am I missing something in your setup?

Link to comment
Share on other sites

Hi a-ok,

i often do this kind of thing for menu with section, sub sections, sub sections, and so on
the simplest way to achieve this is to set your main foreach as it is different from its content ones and for all the sub foreaches, simply write a recursive function, if($foo.hasChildren) or whaterver condition you want your_function() with params or not depending on what you need, done ?

have a nice day

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...