Skip to content

Instantly share code, notes, and snippets.

@stefanheimes
Last active August 29, 2015 14:01
Show Gist options
  • Save stefanheimes/2a04e953189401f139ce to your computer and use it in GitHub Desktop.
Save stefanheimes/2a04e953189401f139ce to your computer and use it in GitHub Desktop.
A helper class for building a breadcrumb for a given page alias.
<?php
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2014 Leo Feyer
*
* @package Zz_sg_patches
* @link https://contao.org
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/
/**
* Register the namespaces
*/
ClassLoader::addNamespaces(array
(
'MAW',
));
/**
* Register the classes
*/
ClassLoader::addClasses(array
(
'MAW\Search\Helper' => 'system/modules/maw/MAW/Search/Helper.php',
));
<?php
/**
* Contao Open Source CMS
*
* @copyright MEN AT WORK 2014
* @package Helper
* @license GNU/LGPL
* @filesource
*/
namespace MAW\Search;
class Helper
{
/**
* Get a breadcrumb for a page url.
*
* @param string $strAlias The alias from the page.
*
* @param string $strTargetLanguage The target language e.g. de, en, etc.
*
* @return array List with all page title. First Root last current page.
*/
public static function getBreadcrumbForPage($strAlias, $strTargetLanguage = '')
{
$arrReturn = array();
$objPages = \PageModel::findPublishedByIdOrAlias($strAlias);
// Check if we have a entry.
if ($objPages == null)
{
return $arrReturn;
}
// If we have more than one side, check the language.
if ($objPages->count() > 1 && !empty($strTargetLanguage))
{
while ($objPages->next())
{
// Get the page with all information.
$objPage = $objPages->current()->loadDetails();
// Check the language if hit one with the current.
if ($objPage->language == $strTargetLanguage)
{
// Get the current page with all details.
$objCurrentPage = $objPage;
break;
}
}
}
else
{
// Get the current page with all details.
$objCurrentPage = $objPages
->current()
->loadDetails();
}
// Check if we have a entry.
if ($objCurrentPage == null)
{
return $arrReturn;
}
// Get the title.
$arrReturn[] = $objCurrentPage->title;
// Remove the last entry and reverse the array.
$arrTrail = $objCurrentPage->trail;
array_pop($arrTrail);
$arrTrail = array_reverse($arrTrail);
// Run each tail.
foreach ($arrTrail as $intPageId)
{
// Get the page information
$objTrailPage = \PageModel::findByPk($intPageId);
// Check if we have the site.
if ($objTrailPage == null)
{
continue;
}
// Add the title to the list.
$arrReturn[] = $objTrailPage->title;
}
// Return all in reveres order. Root first current page last.
return array_reverse($arrReturn);
}
/**
* Get a breadcrumb for a page url with spans.
*
* @param string $strAlias Alias of the page
*
* @param string $strTargetLanguage The target language e.g. de, en, etc.
*
* @return string The titles in spans. First Root last current page.
*/
public static function getFormattedBreadcrumbForPage($strAlias, $strTargetLanguage = '')
{
$arrData = self::getBreadcrumbForPage($strAlias, $strTargetLanguage);
$strReturn = '';
foreach ($arrData as $strData)
{
$strReturn .= "<span>" . $strData . "</span>";
}
return $strReturn;
}
}
<div class="<?php echo $this->class; ?>">
<h3><a href="<?php echo $this->href; ?>" title="<?php echo $this->title; ?>"><?php echo $this->link; ?></a></h3>
<?php if ($this->context): ?>
<p class="context"><?php echo $this->context; ?></p>
<?php endif; ?>
<p class="url">
<?php echo MAW\Search\Helper::getFormattedBreadcrumbForPage($this->url, $GLOBALS['TL_LANGUAGE']); ?>
<span class="filesize"><?php echo $this->filesize; ?> kB</span>
</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment