Skip to content

Instantly share code, notes, and snippets.

@herveguetin
Last active May 18, 2018 01:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save herveguetin/9792632 to your computer and use it in GitHub Desktop.
Add custom or CMS-page-title-based translated breadcrumbs with simple layout update in Magento
<?php
/**
* Retrieve current cms page
*
* @return Mage_Core_Model_Abstract
*/
public function getCurrentCmsPage()
{
$pageId = $this->_getRequest()->getParam('page_id', $this->_getRequest()->getParam('id', false));
$page = Mage::getSingleton('cms/page')->load($pageId);
return $page;
}
/**
* Create item for breadcrumbs
*
* @param $text
* @return array
*/
public function makeCrumb($text)
{
// If there is no text passed through layout update, try to use CMS page title
if(!$text) {
$page = $this->getCurrentCmsPage();
if(!$page->getId()) {
return '';
}
$text = $page->getTitle();
}
return array(
'title' => $this->prepareString($text),
'label' => $this->__($text),
);
}
/**
* Formatting string
*
* @param string $string
* @return string
*/
public function prepareString($string)
{
$string = preg_replace('#[^0-9a-z]+#i', '-', $string);
$string = strtolower($string);
$string = trim($string, '-');
return $string;
}
?>
<!-- To use CMS page title -->
<action method="addCrumb">
<name>crumb_name</name>
<params helper="module_helper/makeCrumb"/>
</action>
<!-- To use custom text -->
<action method="addCrumb">
<name>crumb_name</name>
<params helper="module_helper/makeCrumb">
<text>Crumb Label Text</text>
</params>
</action>
@mblarsen
Copy link

Looks interesting. Do you have a usage example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment