Last active
December 16, 2015 20:09
-
-
Save shadcn/5490042 to your computer and use it in GitHub Desktop.
Drupal 7 - Get breadcrumb by node id.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function _get_breadcrumb_by_nid($nid) { | |
$node = node_load($nid); | |
// If a node is not found, return. | |
if (!$node) { | |
return; | |
} | |
// Load the node into a context. | |
ctools_include('context'); | |
ctools_include('context-task-handler'); | |
// Load the node_view task and contexts. | |
$task = page_manager_get_task('node_view'); | |
$contexts = ctools_context_handler_get_task_contexts($task, '', array($node)); | |
// Find a handler for this task | |
$handlers = page_manager_load_sorted_handlers($task, '', TRUE); | |
$id = ctools_context_handler_get_render_handler($task, $subtask, $handlers, $contexts, $args); | |
if ($id) { | |
$handler = $handlers[$id]; | |
$conf = $handler->conf; | |
// If panels_breadcrumbs isn't enabled for this display, bail out. | |
if (!isset($conf['panels_breadcrumbs_state']) || !$conf['panels_breadcrumbs_state']) { | |
return; | |
} | |
// If no titles or paths are defined, also bail out. | |
if (!isset($conf['panels_breadcrumbs_titles']) || !isset($conf['panels_breadcrumbs_paths'])) { | |
return; | |
} | |
$display_context = ctools_context_handler_get_handler_contexts($contexts, $handler); //$handler->conf['display']->context; | |
// Look for Placeholder Tokens in Titles and Paths and Convert them for this Display | |
$titles = ctools_context_keyword_substitute($conf['panels_breadcrumbs_titles'], array(), $display_context); | |
$paths = ctools_context_keyword_substitute($conf['panels_breadcrumbs_paths'], array(), $display_context); | |
// Breaks titles and paths into arrays and removes empty keys. | |
$titles = array_filter(array_map('trim', explode(PHP_EOL, $titles)), 'strlen'); | |
$paths = array_filter(array_map('trim', explode(PHP_EOL, $paths)), 'strlen'); | |
$default = array( | |
'title' => '', | |
'href' => '', | |
'localized_options' => array(), | |
); | |
$breadcrumbs_info = array(); | |
if (!isset($conf['panels_breadcrumbs_home']) || $conf['panels_breadcrumbs_home'] == TRUE) { | |
// Sets the First Crumb to Home | |
$breadcrumbs_info[] = array('title' => t('Home'), 'href' => '<front>') + $default; | |
} | |
// Iterate through all titles and add them to the breadcrumb | |
foreach ($titles as $key => $title) { | |
$title = html_entity_decode(trim($title)); | |
$path = empty($paths[$key]) ? '<none>' : trim($paths[$key]); | |
$breadcrumbs_info[] = array('title' => t($title), 'href' => $path) + $default; | |
} | |
// Allow other modules to intercept and change the breadcrumb | |
drupal_alter('menu_breadcrumb', $breadcrumbs_info, end($breadcrumbs_info)); | |
$breadcrumbs = array(); | |
foreach ($breadcrumbs_info as $crumb) { | |
if (isset($crumb['href']) && $crumb['href'] == '<none>') { | |
$crumb['localized_options'] += array('attributes' => array(), 'html' => true); | |
$breadcrumbs[] = '<span ' . drupal_attributes($crumb['localized_options']['attributes']) . '>' . ($crumb['localized_options']['html'] ? $crumb['title'] : check_plain($crumb['title'])) . '</span>'; | |
} else { | |
$breadcrumbs[] = l($crumb['title'], $crumb['href'], $crumb['localized_options']); | |
} | |
} | |
} | |
return $breadcrumbs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment