Diem snippets syndication

Customize the breadcrumb

14. Januar 2010 by Thibault D

On the front application, breadcrumbs are generated automatically.

Sometimes we want to modify the displayed links.

The easy way: listen an event

Use the dm.bread_crumb.filter_links event to modify the breadcrumb links before the are rendered.
The event contains a page parameter which is the current page.
The event value is an array of the breadcrumb links to be displayed. We will modify the links array to modify the breadcrumb.

In this example, we assume we have a post module with post/list and post/show pages.
We will rename the post/list link, or remove it when the current page is a post/show page.

apps/front/config/frontConfiguration.class.php

class frontConfiguration extends dmFrontApplicationConfiguration
{  
 
  public function configure()  
  {  
    // connect to the breadcrumb event  
    $this->getEventDispatcher()->connect('dm.bread_crumb.filter_links', array($this, 'listenToBreadCrumbFilterLinksEvent'));  
  }  
 
  public function listenToBreadCrumbFilterLinksEvent(sfEvent $e, array $links)  
  {  
    // check if the breadcrumb contains a link to the post/list page  
    if(isset($links['post/list']))  
    {  
      // if the current page is a post/show page  
      if($e['page']->isModuleAction('post', 'show'))  
      {  
        // remove the post/list link  
        unset($links['post/list']);  
      }  
      else  
      {  
        // Rename the link  
        $links['post/list']->text('Articles');  
      }  
    }  
 
    // return the links to the breadcrumb  
    return $links;  
  }  

The powerful way: override the class

You can also override the breadcrumb view class. First, declare the new view class for breadcrumb widgets:

apps/front/config/dm/widget_types.yml

dmWidgetNavigation:  
  
  breadCrumb:  
    view_class:         myBreadCrumbView  

Then create your class, which inherits dmWidgetNavigationBreadCrumbView.
apps/front/lib/myBreadCrumbView

class myBreadCrumbView extends dmWidgetNavigationBreadCrumbView
{  
  // override the getLinks method  
  protected function getLinks($includeCurrent = true)  
  {  
    // get links from dmWidgetNavigationBreadCrumbView  
    $links = parent::getLinks($includeCurrent);  
 
    // always remove the home link (for example)  
    unset($links['main/root']);  
 
    return $links;  
  }  
}  

This way you can override all the breadcrumb view methods.

With the apps/front/config/dm/widget_types.yml config file, you can replace every form or view class for all widgets, and even create yours.

Add a comment

Comments