Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@mblarsen mblarsen commented Aug 31, 2016

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