Breadcrumbs, set base page

Feed 13 posts, 4 voices

Aug 18, 2008 15:20
Avatar
3 posts

How can I set the base page for breadcrumbs? I don't want it to reach all the way up to the home page and have the top page be the main page of a particular section.

Essentially I would like to have instead of "Home > About > Sub #1", I would like "About > Sub #1". Using something like $this->breadcrumbs(separator, from_url); would be nice.

 
Aug 18, 2008 16:04
Avatar
458 posts

@subtletech - How can I set the base page for breadcrumbs?

Not currently possible unless I'm mistaken. This would be a feature request. If you have a google account you can add it yourself as an "Enhancement". I can add the request for you if you want.

Or you can write it yourself and submit a patch if you prefer.

 
Aug 21, 2008 11:25
Avatar
6 posts

Hi I would also need something like this but since it's not implemented I thaught of a function that could simulate it but I don't know how to code it since I just started using from yesterday.

My site has the following structure en/page_one/page_two/

I want to get the url and explode it into an array. Then, for each slug, I want to the the breadcrumb associated it.

There's my problem... I don't know how to get the breadcrumb linked to the slug.

After that, all you need to do is concat everything.

Can anyone help? Thanks

 
Aug 21, 2008 11:25
Avatar
6 posts

Sorry I forgot to mention that the whole of this is to not have the "en" in the breadcrumbs :)

 
Aug 21, 2008 11:47
Avatar
3 posts

I have coded up a custom function to do this. It isn't fully tested beyond the removal of the "Home Page" entry. Here's the code for both affected functions in app\frontend\classes\Page.php

You should then be able to use $this->breadcrumbs_from(separator, start_from)

start_from is defined as the top most parent/page to exclude. By default it excludes the home page '/'.

These are just quick hacks, so if anyone has something better or wishes to improve/revise these - please share.

public function breadcrumbs_from($separator='>', $start_from='') { $url = ''; $path = ''; $paths = explode('/', '/'.$this->slug); $nb_path = count($paths);

    $out = '<div class="breadcrumb">'."\n";

    if ($this->parent()->url() != BASE_URL . $start_from)
        $out .= $this->parent->_inversedBreadcrumbs_from($separator, $start_from);

    return $out . '<span class="breadcrumb-current">'.$this->breadcrumb().'</span></div>'."\n";

}

private function _inversedBreadcrumbs_from($separator, $start_from) { $out = '<a href="'.$this->url().'" title="'.$this->breadcrumb.'">'.$this->breadcrumb.'</a> <span class="breadcrumb-separator">'.$separator.'</span> '."\n";

    if ($this->parent()->url() != BASE_URL . $start_from)
        return $this->parent->_inversedBreadcrumbs_from($separator, $start_from) . $out;

    return $out;
}
 
Aug 21, 2008 12:14
Avatar
963 posts

OK. I know this isn't "breadcrumbs", but you can just give a "previous page" link by including this bit of PHP on whatever you are using as your "Normal" template:

<?php if ($this->level() != 0) { echo $this->parent->link('&#8592; to '.$this->parent->title()); } ?>

It just makes sure you're not at the top level, then gives a link to the parent page.

If your "en" page is at level 1 (child of "Home"), then you could always make the test as if ($this->level() > 1) ... if I've got that right!

Maybe that's no use ... just a suggestion!

 
Aug 21, 2008 12:19
Avatar
963 posts

LOL. While I was goofing off, subtletech actually came up with a proper solution!

 
Aug 21, 2008 12:30
Avatar
963 posts

Just for a laugh, I put subtletech's code on Pastebin, if anyone wants to look at it there. You can also emend/adjust and get diff's there, too.

 
Aug 21, 2008 14:50
Avatar
6 posts

Worked like a charm! Thanks

 
Aug 22, 2008 09:14
Avatar
458 posts

Looks nice, but I don't think this will handle "mutiple levels"? (i.e. BASE_URL/something/something/)

Did you test this with multiple levels?

 
Aug 22, 2008 10:20
Avatar
3 posts

I threw this together very quickly. Not much testing has been done. Just wanted to share my hack.

If someone wants to code up a proper plug-in or integrate it into Frog, please do. I think many would benefit.

 
Aug 22, 2008 22:53
Avatar
6 posts

It didn't work with multilevel but I then realised I only needed to take out the home page.

I did create a custom function that excluded the paths you want.

lets say for example you want to exclude the home page and the it's child (in my case called "en")

I wrote this in the page.php class (in frog\app\frontend\classes)

public function breadcrumbs_exclude($separator='>', $excludeSlugs = '') {

    $url = '';
    $path = '';
    $paths = explode('/', '/'.$this->slug);
    $nb_path = count($paths);
    $slugsToExclude = explode(",", $excludeSlugs);

    $out = '<div class="breadcrumb">'."\n";

    if ($this->parent)
        $out .= $this->parent->_inversedBreadcrumbs_exclude($separator, $slugsToExclude);

    return $out . '<span class="breadcrumb-current">'.$this->breadcrumb().'</span></div>'."\n";

  }

  private function _inversedBreadcrumbs_exclude($separator, $slugsToExclude)
  {
    $foundSlug = false;
    for ($i = 0; $i < sizeof($slugsToExclude); $i++) {
        if ($this->slug == $slugsToExclude[$i]) 
            $foundSlug = true;
    }
    if ($foundSlug == false)
        $out = '<a href="'.$this->url().'" title="'.$this->breadcrumb.'">'.$this->breadcrumb.'</a> <span class="breadcrumb-separator">'.$separator.'</span> '."\n";

    if ($this->parent)
        return $this->parent->_inversedBreadcrumbs_exclude($separator, $slugsToExclude) . $out;

    return $out;
}

And to call the function all you need to do is

echo $this->breadcrumbs_exclude(">", ",fr'')

Notice there's nothing before the comma, that's for the homepage which needs to have an empty slug

Then again, I'm just a newbie so if it helped someone, great, if not oh well!!

Cheers

 
Aug 22, 2008 22:55
Avatar
6 posts

Oupd in my last post, the example is wrong to be consistent, it should have been

echo $this->breadcrumbs_exclude(">", ",en')

also, don't forget to copy the first line (public function breadcrumbs_exclude($separator='>', $excludeSlugs = '') {) it didn't get the markdown.

:)

 
Aug 22, 2008 22:58
Avatar
6 posts

LOL... even better, instead of passing the ",en", you could just pass along the url "/en" and then in the function do an explode("/",$excludeSlugs);

That would be way more logical