You are here

function breadcrumb_manager_set_breadcrumb in Breadcrumb Manager 7

Magic function: Evaluates correct breadcrumb from the given path.

Parameters

string $current_path: The path to be checked.

bool $set_breadcrumb: TRUE by default. If set to FALSE, the generated breadcrumb will not be set.

Return value

bool|array The generated breadcrumb array or FALSE if given path is an excluded one.

1 call to breadcrumb_manager_set_breadcrumb()
breadcrumb_manager_page_delivery_callback_alter in ./breadcrumb_manager.module
Implements hook_page_delivery_callback_alter().

File

./breadcrumb_manager.module, line 56
Code for Breadcrumb Manager module.

Code

function breadcrumb_manager_set_breadcrumb($current_path = NULL, $set_breadcrumb = TRUE) {
  if (breadcrumb_manager_is_excluded_path($current_path)) {
    return FALSE;
  }
  if (empty($current_path)) {
    $current_path = drupal_get_path_alias(current_path());
  }
  $front = variable_get('site_frontpage', 'node');
  $show_home = variable_get('breadcrumb_manager_show_home', TRUE);
  $show_current = variable_get('breadcrumb_manager_show_current', TRUE);
  $show_current_as_link = variable_get('breadcrumb_manager_show_current_as_link', TRUE);
  $set_active_trail = variable_get('breadcrumb_manager_set_active_trail', FALSE);
  $breadcrumb = array();
  if ($show_home) {
    $home = variable_get('breadcrumb_manager_home', 'Home');
    $home = trim(check_plain($home));
    if (empty($home)) {
      $home = 'Home';
    }
    $breadcrumb[] = l(t($home), '<front>');
  }
  $segments = explode('/', $current_path);
  $total = count($segments);
  $path = '';
  $preferred_item = NULL;
  $last_title = NULL;
  foreach ($segments as $i => $segment) {
    $path = empty($path) ? $segment : implode('/', array(
      $path,
      $segment,
    ));
    $source = drupal_lookup_path('source', $path);
    $current = $i + 1;
    $is_last = $total == $current;
    if (!$is_last || $show_current) {
      $title = breadcrumb_manager_get_title($path);
      $last_title = $title;
      if (empty($title)) {
        $title = drupal_get_title();
        if (empty($title) || $title == $home && current_path() != $front) {
          $title = ucfirst(str_replace('-', ' ', $segment));
          $title = str_replace('_', ' ', $title);
        }
        $last_title = $title;
        $breadcrumb[$current] = l($title, current_path(), _breadcrumb_manager_get_options($total, $i));
        break;
      }
      else {
        $breadcrumb[$current] = l(t('!title', array(
          '!title' => $title,
        )), $path, _breadcrumb_manager_get_options($total, $i));
      }
    }

    // Get active trail.
    if ($set_active_trail) {
      $link_path = $source ? $source : $path;
      $preferred = menu_link_get_preferred($link_path, 'main-menu');
      if (!empty($preferred) && $preferred['menu_name'] == 'main-menu') {
        $preferred_item = $preferred;
      }
    }
  }
  if ($show_current && !$show_current_as_link) {
    $breadcrumb[$total] = $last_title;
  }

  // Set active trail.
  if ($set_active_trail && !empty($preferred_item)) {
    menu_tree_set_path('main-menu', $preferred_item['href']);
  }

  // Allow other modules to alter the generated breadcrumb.
  drupal_alter('breadcrumb', $breadcrumb, $current_path);

  // Set breadcrumb.
  if ($set_breadcrumb) {
    drupal_set_breadcrumb($breadcrumb);
  }
  return $breadcrumb;
}