I found a quite flexible solution, using one template variable and a snippet.
Create a template variable "in_menu" of
Input type Checkbox, give it the
Caption "Show in menu:" and these
Input Option Values:
Choose
Delimiter as the
Output type and set the
Delimiter value to , (a comma).
Assign the TV to the template containing the Wayfinder calls.
Create a snippet "to_exclude" with this code:
<?php
//Property $menuNr (possible values: 1 or 2)
//Get the TV "in_menu" as object:
$tv = $modx->getObject('modTemplateVar',array('name'=>'in_menu'));
//Array of the IDs of all the resources in the tree:
$all_ids = $modx->getTree(0);
//A new array to collect the IDs to exclude from the menu:
$to_exclude = array();
foreach ( $all_ids as $res_id ) {
//Get the value of the TV for the resource with ID res_id
//(Possible values are "1" or "2" or "1,2")
$value = $tv->renderOutput($res_id);
//Check if the TV value contains the menuNr
//If not, add the res_id to the array $to_exclude:
if ( strpos($value,"$menuNr") === false ) {
$to_exclude[] = $res_id;
};
};
//Convert the array into a string of comma separated IDs
//Return that string
return implode(",",$to_exclude);
The snippet is called with a value for the property
&menuNr (1 or 2). For each resource of the tree the value of the TV is checked. If it does not contain the
&menuNr, the ID of the resource will be added to the collection of the resources to be excluded from this menu. The snippet returns a comma separated list of the resources to exclude, as a string. This string is needed as the value for the property
&excludeDocs of the Wayfinder call.
In the template, for the Main Menu you have to insert this Wayfinder call:
[[!Wayfinder? &startId=`0` &excludeDocs=`[[!to_exclude? &menuNr=`1`]]`]]
And this one for the Side Menu:
[[!Wayfinder? &startId=`0` &excludeDocs=`[[!to_exclude? &menuNr=`2`]]`]]
Now editing a resource in the manager (or adding a new resource) you just have to set the TV "in_menu" to the wanted value, and Wayfinder will automatically adapt both the menus. (The snippet calls have to be uncached if changes of the TV values should show immediately.)
[ed. note: ottogal last edited this post 14 years ago.]