MODx TagLinks Problems and Solutions

I just went through the MODx blogging tutorial for real, with the intent of actually creating the exact same blog functionality myself. I ran into some problems with friendly/pretty URLs and this is a description of how I solved them.


First of all make sure you have set the document alias for the category documents in low caps only. I haven’t fixed the problem of different caps although you might be able to take care of that one through the .htaccess file. Actually the whole problem could possibly be solved in the .htaccess file but I’m more comfortable with a PHP solution.

Currently my links are on this form:
http://server/online/template_shop/minimal-base/Categories/typo3.html

I wanted the above link to be generated with the following tagLinks invocation:
[[tagLink? &tv=`tags` &separator=` ` &format=`ul` &element=`span` &fap=`1` &path=`minimal-base/Categories`]]

The problem is that the above invocation produces this URL:
http://server/online/template_shop/minimal-base/Categories&tags=typo3

To get the first link above I made the following changes in the tagLinks snippet:

Original line 123-127:

if ($fap == '1'){
	$doc_path = $modx->config['site_url'].$path.'&tags=';
} else {
	$doc_path='&tags=';
}

New version:

if ($fap == '1'){
	$doc_path = $modx->config['site_url'].$path.'&tags=';
} else if($fap == '2'){
  $doc_path = $modx->config['site_url'].$path.'/';
} else {
	$doc_path='&tags=';
}

So if $fap is 2 we drop the ‘&tags=’ part and add a slash instead.

Lines 163-171 in the original:

$link .= '<'.$format.'>'.$nl;
$link .= '<li class="'.$style.'_label">'.$label.'</li>'.$nl;
for ($x=0;$x<$cnt;$x++) {
  $url = urlencode(trim($tvarray[$x]));
  $cnd_separator = ($x!=($cnt-1)) ? $separator : '';
  $link .= '<li><a href="'.$doc_path.strtolower($url).'">'.trim($tvarray[$x]).'</a>'.$cnd_separator.'</li>'.$nl;
}
$link .= '</'.$format.'>'.$nl;
}
}

New version:

$ext = $fap == '2' ? '.html' : '';
$link .= '<'.$format.'>'.$nl;
$link .= '<li class="'.$style.'_label">'.$label.'</li>'.$nl;
for ($x=0;$x<$cnt;$x++) {
  $url = urlencode(trim($tvarray[$x]));
  $cnd_separator = ($x!=($cnt-1)) ? $separator : '';
  $link .= '<li><a href="'.$doc_path.strtolower($url).$ext.'">'.trim($tvarray[$x]).'</a>'.$cnd_separator.'</li>'.$nl;
}
$link .= '</'.$format.'>'.$nl;

The only change here is a new test for if $fap is 2, if it is we add the .html part, otherwise it will be the empty string. When we build the link we use the $ext variable.

I call tagLinks like this now:
[[tagLink? &tv=`tags` &separator=` ` &format=`ul` &element=`span` &fap=`2` &path=`minimal-base/Categories`]].

The only change is the fap value.


Related Posts

Tags: ,