I don’t know if it’s really usefull but I needed this functionality for pagination I include in my snippets.
The function will keep the original url+querystring and keep all values that are not changed intact. This way the link it produces works in every situation.
For example if your snippet is called together with another snippet that uses some querystring value you can’t possibly now what that value will be unless you preserve the url. When you create a link to a certain ability of your own snippet it will not break the other snippet call.
you will give either a docid or a docalias of the link and put the extra querystring values in an array.
function:
function smartModxUrl($docid, $docalias, $array_values) {
global $modx;
$array_url = $_GET;
$urlstring = array();
unset($array_url["id"]);
unset($array_url["q"]);
$array_url = array_merge($array_url,$array_values);
foreach ($array_url as $name => $value) {
if (!is_null($value)) {
$urlstring[] = $name . '=' . urlencode($value);
}
}
return $modx->makeUrl($docid, $docalias, join('&',$urlstring));
}
example usage:
$link['page'] = 3;
$link['aname'] = 'avalue';
$link['another'] = 'one';
echo smartModxUrl($modx->documentObject["id"],NULL, $link);