MenutrailbypathBreadcrumb.inc in Menu Trail By Path 7.3
File
src/MenutrailbypathBreadcrumb.inc
View source
<?php
class MenutrailbypathBreadcrumb {
protected $pathHelper;
protected $menuHelper;
public function __construct(MenutrailbypathPathHelper $pathHelper, MenutrailbypathMenuHelper $menuHelper) {
$this->pathHelper = $pathHelper;
$this->menuHelper = $menuHelper;
}
public function setBreadcrumb() {
$breadcrumb = array();
$this
->setActiveTrail();
if (drupal_is_front_page()) {
return;
}
$active_trail = menu_get_active_trail();
$item = end($active_trail);
if (!empty($item['href'])) {
drupal_alter('menu_breadcrumb', $active_trail, $item);
$end = end($active_trail);
if ($item['href'] == $end['href']) {
$current_page_link = array_pop($active_trail);
}
if (($item['type'] & MENU_LINKS_TO_PARENT) == MENU_LINKS_TO_PARENT) {
array_pop($active_trail);
}
foreach ($active_trail as $parent) {
$breadcrumb[] = l($parent['title'], $parent['href'], $parent['localized_options']);
}
if (!empty($current_page_link)) {
$drupal_page_title = drupal_get_title();
if (!empty($drupal_page_title) && $current_page_link['href'] == current_path()) {
$current_page_link['title'] = $drupal_page_title;
}
if (MenutrailbypathConfig::get('breadcrumb_display_current_page') == 'yes_link') {
$link_options = $current_page_link['localized_options'];
$link_options['attributes']['class'][] = 'breadcrumb__item--current-page';
$link_options['attributes']['class'][] = 'active';
$breadcrumb[] = l($current_page_link['title'], $current_page_link['href'], $link_options);
}
elseif (MenutrailbypathConfig::get('breadcrumb_display_current_page') == 'yes_span') {
$breadcrumb[] = '<span class="breadcrumb__item--current-page">' . $current_page_link['title'] . '</span>';
}
}
}
drupal_set_breadcrumb($breadcrumb);
}
protected function setActiveTrail() {
$active_trail = array();
$trail_paths = $this->pathHelper
->getPaths();
$trail_menu_links = $this->menuHelper
->getMenuLinksByPaths($trail_paths);
$breadcrumb_menu_links = $this
->groupMenuLinksByPath($trail_menu_links);
$active_trail[] = array(
'title' => t('Home'),
'href' => '<front>',
'localized_options' => array(),
'type' => 0,
);
foreach ($trail_paths as $trail_path) {
$menu_link = NULL;
$router_item = menu_get_item($trail_path);
if (!empty($breadcrumb_menu_links[$trail_path]->link_title)) {
$menu_link = (array) $breadcrumb_menu_links[$trail_path];
$menu_link['external'] = 1;
$menu_link['type'] = $router_item['type'];
_menu_link_translate($menu_link);
}
else {
$menu_link = array(
'title' => isset($router_item['title']) ? $router_item['title'] : '',
'href' => $trail_path,
'localized_options' => array(),
'type' => MENU_NORMAL_ITEM,
);
}
if (!empty($menu_link)) {
$active_trail[] = $menu_link;
}
}
menu_set_active_trail($active_trail);
}
protected function groupMenuLinksByPath(array $menu_links) {
$menu_links = array_reverse($menu_links);
$this->menuHelper
->sortMenuLinksByMenuPreference($menu_links);
$breadcrumb_menu_links = array();
foreach ($menu_links as $menu_link) {
if (!isset($breadcrumb_menu_links[$menu_link->link_path]) && !empty($menu_link->link_title)) {
$breadcrumb_menu_links[$menu_link->link_path] = $menu_link;
}
}
return $breadcrumb_menu_links;
}
}