You are here

breadcrumbs_by_path.module in Breadcrumbs by path 7

Creates a breadcrumb trail based on the current aliased path.

File

breadcrumbs_by_path.module
View source
<?php

/**
 * @file
 * Creates a breadcrumb trail based on the current aliased path.
 */

/**
 * Implements hook_help().
 */
function breadcrumbs_by_path_help($path, $arg) {
  switch ($path) {
    case 'admin/help#breadcrumbs_by_path':
      return t("This module alters the page's breadcrumb trail based on the URL by examining each level in the URL and creating a breadcrumb link for each valid page.");
  }
}

/**
 * Implements hook_menu().
 */
function breadcrumbs_by_path_menu() {
  $items['admin/config/user-interface/breadcrumbs-by-path'] = array(
    'title' => 'Breadcrumbs by Path',
    'description' => 'Administration settings for the Breadcrumbs by Path module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'breadcrumbs_by_path_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'includes/breadcrumbs_by_path.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_page_build().
 */
function breadcrumbs_by_path_page_build(&$page) {
  $use_breadcrumbs_by_path = TRUE;

  // Start with getting the current URL
  $path = current_path();
  $uri = drupal_get_path_alias($path);

  // If this path is within the excluded paths, do not show the breadcrumb.
  $to_exclude = variable_get('breadcrumbs_by_path_exclude_paths');
  if ($to_exclude) {
    if (drupal_match_path($uri, $to_exclude)) {
      drupal_set_breadcrumb(array());
      $use_breadcrumbs_by_path = FALSE;
    }
  }

  // Also check that this path isn't within the 'core breadcrumbs' list.
  $core_breadcrumbs = variable_get('breadcrumbs_by_path_core_paths');
  if ($core_breadcrumbs) {
    if (drupal_match_path($uri, $core_breadcrumbs)) {
      $use_breadcrumbs_by_path = FALSE;
    }
  }
  if ($use_breadcrumbs_by_path) {

    // build a breadcrumb trail and set the breadcrumb
    $trail = _breadcrumbs_by_path_build_trail($uri);

    // If the setting is enabled, add the current page title to the end of the
    // trail.
    if (variable_get('breadcrumbs_by_path_current_title')) {
      $trail[] = '<span class="active active-trail">' . drupal_get_title() . '</span>';
    }
    drupal_set_breadcrumb($trail);
  }
}

/**
 * Build a breadcrumb trail based on a URI.
 */
function _breadcrumbs_by_path_build_trail($uri) {
  $trail = array();

  // if we're on the home page, skip everything and return just the home link
  if (!drupal_is_front_page()) {
    $i = 0;

    // strip the current page from the request
    $uri = dirname($uri);

    // recursively trim uri and look for available path
    while ($uri && $uri != '.') {

      // First we see if the uri is an alias.
      $path = drupal_lookup_path('source', $uri);

      // figure out the title of the current item
      if ($path) {
        $item = menu_get_item($path);

        // If this item is a forum, load the appropriate taxonomy term and get
        // it's title.
        if ($item['path'] == 'forum/%') {
          $exploded = explode('/', $path);
          $tid = $exploded[1];
          $trail[$i]['title'] = taxonomy_term_load($tid)->name;
        }
        elseif ($item['path'] == 'taxonomy/term/%') {
          $exploded = explode('/', $path);
          $tid = $exploded[2];
          $trail[$i]['title'] = taxonomy_term_load($tid)->name;
        }
        else {
          $trail[$i]['title'] = $item['title'];
        }
      }
      else {

        // if it's not a valid path, load the menu item for the path
        $item = menu_get_item($uri);

        // if we still didn't get a valid menu item, skip this
        if (!$item) {
          $uri = dirname($uri);
          continue;
        }

        // original_map holds the full list of args passed in to the menu item
        $last_arg = array_pop($item['original_map']);

        // check to see if the end of the path is the same as the menu path, meaning there are no additional args
        if ($item['path'] == $last_arg) {
          $trail[$i]['title'] = $item['title'];
        }
        else {

          // Check if item is a taxonomy term and if so get title from term
          if ($item['path'] == 'taxonomy/term/%') {
            $term = taxonomy_term_load($last_arg);
            $trail[$i]['title'] = $term->name;
          }
          else {
            $trail[$i]['title'] = _breadcrumbs_by_path_clean_string($last_arg);
          }
        }
      }

      // Add in the path if we set something.
      if (isset($trail[$i])) {
        $url = check_url($uri);
        $trail[$i]['path'] = $url;
      }

      // prepare for the next iteration
      $uri = dirname($uri);
      $i++;
    }

    // reverse the trail so that it will be in the correct order when returned
    $trail = array_reverse($trail);
  }

  // Now build the actual breadcrumb trail in the right format.
  $bc = array(
    l(t('Home'), '<front>'),
  );
  foreach ($trail as $c) {
    if (isset($c['title']) && isset($c['path'])) {
      $bc[] = l($c['title'], $c['path']);
    }
  }
  return $bc;
}

/**
 * Clean a string for display as a breadcrumb title.
 */
function _breadcrumbs_by_path_clean_string($string) {

  // replace hyphens and underscores with spaces
  $string = str_replace(array(
    '-',
    '_',
  ), ' ', $string);

  // Make the first character of the title uppercase
  $string = drupal_ucfirst($string);
  return $string;
}

Functions

Namesort descending Description
breadcrumbs_by_path_help Implements hook_help().
breadcrumbs_by_path_menu Implements hook_menu().
breadcrumbs_by_path_page_build Implements hook_page_build().
_breadcrumbs_by_path_build_trail Build a breadcrumb trail based on a URI.
_breadcrumbs_by_path_clean_string Clean a string for display as a breadcrumb title.