I had to resort to PHP to solve the above problem. Here’s how I did it:
First, at the top of the template (probably anywhere outside of the categories tag, actually), I did:
<?php $site_index = '{path=site_index}'; ?>
That’s because inside the categories loop, site_index seems to have a different meaning; it includes the path to the template plus the category link.
Then, to display the related entries with links:
<h3>Multi Relationship Test</h3>
<ul>
{related_entries id="nsite_multi_related_test"}
{categories}
<?php
// this works in part because we know static pages entries
// will be in only one category, so this PHP is executed only once
global $DB;
$cat_tree = '';
$page_cat = '';
$cat_id = {category_id};
$sql = "select cat_description from exp_categories where cat_id=$cat_id";
$query = $DB->query($sql);
$page_cat .= $query->row['cat_description'];
do {
$sql = "select parent.cat_id, parent.cat_description from "
. "exp_categories as parent, exp_categories as child "
. "where child.cat_id=$cat_id and child.parent_id=parent.cat_id";
$query = $DB->query($sql);
if ($query->num_rows == 1) {
$cat_tree = $query->row['cat_description'].'/'.$cat_tree;
$cat_id = $query->row['cat_id'];
}
} while ($query->num_rows == 1);
$cat_tree .= $page_cat;
?><li><a href="<?=$site_index.$cat_tree?>">{title}</a></li>
{/categories}
{/related_entries}
</ul>
It’s important (as noted) that static pages only be assigned to a single category. Since that’s the way Pages works by design, I felt I could rely upon it. So inside the {related_entries} tag, I invoke the {categories} tag pair so that I can get at {category_id}. Then I do a database lookup (actually, a bunch of them) to find its parent, then its parent’s parent, etc., until the query returns no results (at the topmost parent). This gives me a string like “root/directory/subdirectory/”, to which I still need to add the page name itself. Well, this is also a category name, but it’s not a parent, so one final DB->query pulls that in and sticks it on the end. This string, appended to the site_index I fetched back at the beginning of the page load, makes the URL I need.
I’m definitely interested in hearing about ways to do this that don’t require so many database calls. This solution requires (n+1)*m calls, where n is the depth of the linked document and m is the number of related documents. So if I have 10 related links, each of which is 4 levels deep in the category hierarchy, I’m looking at 50 database calls just to build this section. Not cool!
Cheers,
Ben