You are here

function _path_breadcrumbs_build_breadcrumbs in Path Breadcrumbs 7.3

Same name and namespace in other branches
  1. 7 path_breadcrumbs.module \_path_breadcrumbs_build_breadcrumbs()
  2. 7.2 path_breadcrumbs.module \_path_breadcrumbs_build_breadcrumbs()

Build breadcrumbs navigation from loaded path breadcrumb variant.

Parameters

object $path_breadcrumb: Object with path breadcrumb variant loaded from database.

array $contexts: Ctools contexts from current URL.

Return value

array Array with breadcrumbs navigation.

1 call to _path_breadcrumbs_build_breadcrumbs()
path_breadcrumbs_load_variant in ./path_breadcrumbs.module
Load and build path breadcrumb variant for page url. Results are cached.

File

./path_breadcrumbs.module, line 183

Code

function _path_breadcrumbs_build_breadcrumbs($path_breadcrumb, $contexts = array()) {
  $breadcrumb = array();

  // Add hook_path_breadcrumbs_view() for other developers.
  module_invoke_all('path_breadcrumbs_view', $path_breadcrumb, $contexts);

  // Prepend HOME link to breadcrumbs navigation.
  if ($path_breadcrumb->home == TRUE) {
    $home = variable_get('path_breadcrumbs_home_link_title', 'Home');
    $breadcrumb[] = l($home, '<front>');
  }

  // Convert breadcrumb titles and paths to string.
  $titles = implode("\n", $path_breadcrumb->titles);
  $paths = implode("\n", $path_breadcrumb->paths);

  // Replace %nn sequences to prevent mixing them with contexts.
  $paths = rawurldecode($paths);

  // Replace module placeholders.
  $replace = array();
  $search = array();

  // Replace placeholders by its value from url.
  if (!empty($path_breadcrumb->arguments)) {
    foreach ($path_breadcrumb->arguments as $keyword => $argument) {
      $search[] = '!' . $keyword;
      $replace[] = arg($argument['position']);
    }
  }

  // Replace placeholder for current page title.
  $search[] = '!page_title';
  $replace[] = drupal_get_title();

  // Replace module placeholders.
  $titles = str_replace($search, $replace, $titles);
  $paths = str_replace($search, $replace, $paths);

  // Add custom ctools context for site.
  $contexts['site'] = ctools_context_create('path_breadcrumbs_site');

  // Replace placeholders by current context values.
  $titles = ctools_context_keyword_substitute($titles, array(), $contexts);
  $paths = ctools_context_keyword_substitute($paths, array(), $contexts);

  // Explode titles and paths into array.
  $path_breadcrumb->titles_prepared = explode("\n", $titles);
  $path_breadcrumb->paths_prepared = explode("\n", $paths);

  // Support empty breadcrumbs (disabling).
  if (count($path_breadcrumb->titles_prepared) == 1 && $path_breadcrumb->titles_prepared[0] == '<none>' && $path_breadcrumb->paths_prepared[0] == '<none>') {
    $path_breadcrumb->titles_prepared = array();
    $path_breadcrumb->paths_prepared = array();
  }
  foreach ($path_breadcrumb->titles_prepared as $key => $title) {

    // Remove breadcrumb from navigation if it is empty.
    if (empty($title)) {
      continue;
    }
    $options = array();

    // Decode title if needed.
    $decode_html_entities = variable_get('path_breadcrumbs_decode_entities', TRUE);
    if ($decode_html_entities) {
      $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
    }

    // Truncate to max length, consider ellipsis (+3 symbols).
    $truncate_length = variable_get('path_breadcrumbs_truncate_title_length', '');
    $title_length = drupal_strlen($title);
    if ($truncate_length && $truncate_length < $title_length) {

      // Show full title in tooltip.
      $options['attributes']['title'] = $title;
      $title = truncate_utf8($title, $truncate_length + 3, TRUE, TRUE);
    }

    // Set a breadcrumb as a link or as a plain text.
    if (isset($path_breadcrumb->paths_prepared[$key]) && $path_breadcrumb->paths_prepared[$key] != '<none>') {
      $path = _path_breadcrumbs_clean_url($path_breadcrumb->paths_prepared[$key], $options);
      $breadcrumb[] = l($title, $path, $options);
    }
    elseif (isset($path_breadcrumb->paths_prepared[$key]) && $path_breadcrumb->paths_prepared[$key] == '<none>') {
      $breadcrumb[] = check_plain($title);
    }
  }

  // Inform other modules that this PB item wasn't loaded from cache.
  $path_breadcrumb->from_cache = FALSE;

  // Allow other modules to alter breadcrumbs generated by this module.
  drupal_alter('path_breadcrumbs_view', $breadcrumb, $path_breadcrumb, $contexts);
  return $breadcrumb;
}