#1 June 27, 2015 11:23am

florianuhlemann
Member
From: Germany
Registered: June 26, 2015
Posts: 21
Website

Removing "double-slug" (/abc/detail/xyz/ -> /abc/xyz/) from treemodule

One more thing... smile

I'm very happy so far, everything turned out well. (even my self-coded contact form^^ i need that practice, so that should be ok for now)

I'm using the demo layout and modifying as I go. (best way to learn imho)
There's this thing called a "trees"-module that includes multiple "tree"-entities.

The general page of trees is called by /abc/ and the detail-page of the single tree is called by /abc/detail/xyz/ but I'd like to remove the /detail/ out of it.

First fix I tried was this:

custom/inc/modules/trees.php
from:

function get($item) {
            $item = parent::get($item);
            $item["detail_link"] = $this->Link."detail/".$item["route"]."/";
            return $item;
        }

        function getNav($page) {
            $items = $this->getAllPositioned();
            $nav = array();
            foreach ($items as $item) {
                $nav[] = array("title" => $item["title"],"link" => WWW_ROOT.$page["path"]."/detail/".$item["route"]."/");
            }
            return $nav;
        }

to:

function get($item) {
            $item = parent::get($item);
            $item["detail_link"] = $this->Link."/".$item["route"]."/";
            return $item;
        }

        function getNav($page) {
            $items = $this->getAllPositioned();
            $nav = array();
            foreach ($items as $item) {
                $nav[] = array("title" => $item["title"],"link" => WWW_ROOT.$page["path"]."/".$item["route"]."/");
            }
            return $nav;
        }

Which would in turn create the correct links when trying to access the detail-pages of the trees, but they cannot be found via that new link. It would only show the "trees" overview page again that is /abc/ ... (/abc/xyz/ goes to the same content as /abc/)
What am I missing?

Last edited by florianuhlemann (June 27, 2015 11:36am)

Offline

#2 June 27, 2015 6:35pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 978

Re: Removing "double-slug" (/abc/detail/xyz/ -> /abc/xyz/) from treemodule

You've done a great job so far, the missing piece is in the routed template files (I believe the directory is /templates/routed/trees/, I'm away from my normal computer so I don't have access to the BigTree codebase at the moment).

In that directory you'll see default.php and details.php -- default.php is what BigTree will route to if it can't find any other routable files. BigTree uses the URL routes that are not part of the main page path to try to find another file in the routed template directory. You see this in the default setup where "details" is added to the URL followed by the individual tree path. Because there is a details.php file, BigTree loads that instead of default.php and sets $bigtree["commands"][0] to the requested tree's URL route.

To do what you want to do, you need to add a condition in default.php. Something like this:

<?php
    if ($bigtree["commands"][0]) {
        include "details.php";
    } else {

Where the stuff following else is the default contents of default.php.

Offline

#3 June 28, 2015 3:57am

florianuhlemann
Member
From: Germany
Registered: June 26, 2015
Posts: 21
Website

Re: Removing "double-slug" (/abc/detail/xyz/ -> /abc/xyz/) from treemodule

Here's what I've done. It's probably nothing even close to a well developed system, but i've decided to move the original default.php into a more appropriate name "default_overview.php" and replaced the content in the default.php with the following.

<? if ($bigtree["commands"][0]) {
        include "detail.php";
    } else {
        include "default_overview.php";
    }
?>

I could have just placed a lot of echos in the default.php but that looked both confusing and probably would have caused bugs or so. this way should be cleaner as it basically either loads the detail.php OR the default_overview.php.

For better understanding: what exactly does "$bigtree["commands"][0]" do? It checks if the array-element with the name "commands" has 0 elements in it, right? What does "commands" really stand for? Just a passed-through value from the system to know what it should "do"?

Last edited by florianuhlemann (June 28, 2015 3:57am)

Offline

#4 June 29, 2015 9:49am

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 978

Re: Removing "double-slug" (/abc/detail/xyz/ -> /abc/xyz/) from treemodule

That looks fine and we often will do something similar when creating sites!

$bigtree["commands"] is an array containing additional URL routes that are not part of the routed template's routed path (which is contained in $bigtree["routed_path"]. To explain the difference, let's take this example URL:

http://www.website.com/trees/details/willow/

In this instance we have a routed template named "Trees" that lives at the URL http://www.website.com/trees/.

IF we have a "details.php" file in the template's directory, it becomes part of the routed path and $bigtree["routed_path"][0] will be equal to "details" and $bigtree["commands"][0] will be equal to "willow". If we do NOT have a details.php file and default.php loads instead, $bigtree["routed_path"] will be empty and $bigtree["commands"] will be array("details","willow").

This logic only applies to routed templates, however. If your "Trees" template was a basic template and someone went to http://www.website.com/trees/whatever/ they would encounter a 404 as it does not accept additional commands.

Offline

#5 June 29, 2015 11:41am

florianuhlemann
Member
From: Germany
Registered: June 26, 2015
Posts: 21
Website

Re: Removing "double-slug" (/abc/detail/xyz/ -> /abc/xyz/) from treemodule

Thanks! Stuff like that should go in the docs. smile

BTW, you rock! Thanks.

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.