When the first time I learn MODx, one thing I want to know is making a snippet for displaying resources. Apparently that not easy because I need to learn xPDO and chunk (modChunk).
So here the SimpleMenu snippet with xPDO to query the resources and modChunk to format the output.
Create a new snippet, named it as "SimpleMenu" and paste below code.
<?php
/*
* SimpleMenu Snippet
*
* - parameters: context, parent, bar, tpl, wrapper
*
* - example 1: [[SimpleMenu]]
* - example 2:
[[SimpleMenu?
&context=`web`
&parent=`0`
&bar=` &nbps; `
&tpl=`<a href="[[~[[+id]]]]">[[+menutitle:default=`[[+pagetitle]]`]]</a>`
&wrapper=`<div>[[+wrapper]]</div>`
]]
*
* Features:
* - learn how to create snippet
* - learn how to use xPDO
* - learn how to use modChunk (@INLINE mode)
* - it's works and amazingly fast (compared to heavyweight addon menu)
* - need different menu? duplicate this snippet and modify as you wish
*/
$output = '';
$current_context = $modx->context->get('key');
$context = isset($context) ? $context : $current_context;
$parent = isset($parent) ? intval($parent) : 0;
$wrapper = isset($wrapper) ? $wrapper : "";
$bar = isset($bar) ? $bar : ' ';
$tpl = isset($tpl) ? $tpl : '<a href="[[+link]]" name="[[+alias]]" class="[[+class]]">[[+menutitle:default=`[[+pagetitle]]`]]</a>';
$tpl = str_replace('{~', '[[', $tpl);
$tpl = str_replace('~}', ']]', $tpl);
//xPDO stuff
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
'parent' => $parent,
'context_key' => $context,
'published' => 1,
'hidemenu' => '0'
));
$criteria->sortby('menuindex','ASC');
$resources = $modx->getCollection('modResource', $criteria);
// get all resource ID from results
$resourcesIDs = array_keys($resources);
// get all parent ID from current resource
$parentIDs = $modx->getParentIds($modx->resourceIdentifier);
array_unshift($parentIDs, $modx->resourceIdentifier);
// selected menu
$selected_parent = array_keys(array_intersect($resourcesIDs, $parentIDs));
$selected_menu = !empty($selected_parent) ? intval($selected_parent[0]) : 0;
//DEBUG?
//print_r(array('<pre>DEBUG ON:', $resourcesIDs, $parentIDs, $selected_parent, $selected_menu, 'OFF</pre>'));
$i = 0;
$n = sizeof($resources);
$_row = null;
foreach ($resources as $resourceID => $resource) {
$_row = array();
$_row['id'] = $resourceID;
$_row['alias'] = $resource->get('alias');
$_row['pagetitle'] = $resource->get('pagetitle');
$_row['menutitle'] = $resource->get('menutitle');
$_row['link'] = $modx->makeUrl($resourceID, '', '', 'full');
//set css-class
$_row['class'] = 'menu-'.$i . ' resource-' . $resourceID;
if ($selected_menu == $i) {
$_row['class'] .= ' selected';
}
if ($i < 1) $_row['class'] .= ' first';
if ($i+1 == $n) $_row['class'] .= ' last';
//modChunk
$chunk = $modx->newObject('modChunk');
$chunk->setContent($tpl);
$chunk->setProperties($_row);
$chunk->setCacheable(false);
$output .= $chunk->process();
if ($n-1 > $i) {
$output .= $bar;
}
$i++;
}
//add wrapper
if (!empty($wrapper)) {
$chunk = $modx->newObject('modChunk');
$chunk->setContent($wrapper);
$chunk->setProperties(array('wrapper'=>$output));
//$chunk->setCacheable(false);
$output = $chunk->process();
}
unset($context);
unset($parent);
unset($wrapper);
unset($bar);
unset($tpl);
unset($criteria);
unset($resources);
unset($resourcesIDs);
unset($resourcesIDs);
unset($selected_parent);
unset($selected_menu);
unset($_row);
unset($chunk);
return $output;
[ed. note: lokamaya last edited this post 11 years, 5 months ago.]