We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29487
    • 385 Posts
    В продолжение этой, этой и этой темы.
    Периодически встаёт задача выводить уровневое подменю, то есть меню, появляющееся только на страницах определенного уровня в разделе.
    Хотелось чего-то универсального, поэтому решил вопрос так:

    1. Переписал под себя сниппет UltimateParent:

    <?php
    /**
     * @name UltimateParent
     * @version 2.0+ (requires MODx 0.9.5+)
     * @author Temus
     * 
     * @param &id The id of the document whose parent you want to find.
     * @param &top The top node for the search.
     * @param &topLevel The top level node for the search
     * @param &topLimit The bottom level node for the search
     * 
     * @license Public Domain, use as you like.
     * 
     * Based on the original UltimateParent 2.0 snippet by Jason Coward <[email protected]>
     */
    $top= isset ($top) && intval($top) ? $top : 0;
    $id= isset ($id) && intval($id) ? intval($id) : $modx->documentIdentifier;
    $topLevel= isset ($topLevel) && intval($topLevel) ? intval($topLevel) : 0;
    $topLimit= isset ($topLimit) && intval($topLimit) ? intval($topLimit) : 0;
    
    if ($id && $id != $top) {
    	$pid= $id;
    	$crumbCount=0;
    	$crumbArray[]=$pid;
    	while ($pid != $top && $pid != 0) {
    		$id= $pid;
    		$parentIds= $modx->getParentIds($id, 1);
    		$pid= array_pop($parentIds);
    		$crumbCount++;
    		$crumbArray[]=$pid;
    		if ($pid == $top) {
    			if ($topLimit && $topLimit<$crumbCount) {return 0;}
    			if ($topLevel && $topLevel<=$crumbCount) {return $crumbArray[$crumbCount-$topLevel]; }
    			if ($topLevel && $topLevel>$crumbCount) {return 0; }
    			return $id;
    		}
    	}
    }
    return 0;
    ?>
    


    Теперь он по-другому интерпретирует topLevel и возвращает ноль, если ничего не находит согласно параметрам. Также добавил параметр topLimit

    2. В сниппет Wayfinder перед строкой //Process Wayfinder добавил:

    $array_of_parametrs = array();
    if (isset($top)) {$array_of_parametrs['top'] = $top;}
    if (isset($topLevel)) {$array_of_parametrs['topLevel'] = $topLevel;}
    if (isset($topLimit)) {$array_of_parametrs['topLimit'] = $topLimit;}
    if (!empty($array_of_parametrs)) {
        $wf->_config['id'] = $modx->runSnippet('UltimateParent', $array_of_parametrs);
        if ($wf->_config['id']==0) {return;}
    }
    


    Итого в Wayfinder появилось 3 новых параметра: &top, &topLevel и &topLimit
    При указании хотя бы одного из них будет выводиться подменю на страницах с уровня topLevel по topLimit включительно относительно top
    Если top не указан - уровни считаются от корня
    Если top указан, а уровни не указаны - будет выводиться подменю на страницах, имеющих родителя top