I have added a grep callback handler to the wayfinder snippet so that I can programatically remove a document resource from the output. It is very simple: the snippet call is extended to include the grep function, and the grep function returns TRUE or FALSE.
At first I modified the core wayfinder snippet code like this
if (class_exists('Wayfinder')) {
// extend wayfinder here
class WayfinderGrep extends Wayfinder
{
function renderRow(&$resource,$numChildren) {
if ($this->_config['grep']){
if (!call_user_func($this->_config['grep'],$resource))
return '';
}
return parent::renderRow(&$resource,$numChildren);
}
}
$wf = new WayfinderGrep();
} else {
return 'error: Wayfinder class not found';
}
Then I moved my changes into a new snippet called WayfinderGrep. So the snippet call looks something like:
[[WayfinderGrep? &startId=`0` &grep=`myGrepFunction`]]
But I dont see it being called that way. Since the grep function needs a home, it is more convenient to create a specialised snippet and call WayfinderGrep from within
<?php
// myGrepSnippet can include whatever extra params I need
return $modx->runSnippet("wayfindergrep",Array("startId"=>$startId,"grep"=>"grep"));
function grep($resource){
// return TRUE or FALSE
}
?>
It all works nicely and Wayfinder is unchanged. But I had to copy the entire wayfinder snippet to wayfinderGrep which I wasn’t so happy with. But maybe that’s OK. Does anybody think this is a good way to extend core addons, or is there a better way?
BTW: I’m no wayfinder expert so if this functionality already exists, please view this post as a simple query about extending addons.