You are here

function path_breadcrumbs_load_variant in Path Breadcrumbs 7.3

Same name and namespace in other branches
  1. 7.2 path_breadcrumbs.module \path_breadcrumbs_load_variant()

Load and build path breadcrumb variant for page url. Results are cached.

Parameters

string $path: Current page url.

Return value

object Path breadcrumb object that matches page url. Members:

  • variant: raw Path Breadcrumbs variant;
  • build: built Path Breadcrumbs array ready for drupal_set_breadcrumb().
2 calls to path_breadcrumbs_load_variant()
path_breadcrumbs_breadcrumb in ./path_breadcrumbs.module
Override default theme_breadcrumb().
path_breadcrumbs_set_breadcrumb in ./path_breadcrumbs.module
Init Path Breadcrumbs.

File

./path_breadcrumbs.module, line 68

Code

function path_breadcrumbs_load_variant($path) {
  global $language_url;
  if (empty($path)) {
    return FALSE;
  }
  $path_breadcrumbs_data =& drupal_static(__FUNCTION__ . '::' . $path);
  if (!empty($path_breadcrumbs_data)) {
    return $path_breadcrumbs_data;
  }

  // See if user enabled cache for breadcrumbs.
  $cache_enabled = variable_get('path_breadcrumbs_cache_enabled', FALSE);
  $cache_lifetime = variable_get('path_breadcrumbs_cache_lifetime', CACHE_PERMANENT);

  // Apply current time to cache expiration time to set valid cache expire date.
  if ($cache_lifetime > CACHE_PERMANENT) {
    $cache_lifetime += REQUEST_TIME;
  }
  if ($cache_enabled) {

    // Trying to load breadcrumb from cache.
    $cache_id = __FUNCTION__ . ':' . $path . ':' . $language_url->language;
    $cache = cache_get($cache_id, PATH_BREADCRUMBS_CACHE_STORAGE);
    if (!empty($cache->data)) {

      // Empty results are also cached.
      if ($cache->data == 'none') {
        return FALSE;
      }

      // Allow to alter cached items.
      if (count(module_implements('path_breadcrumbs_view_alter')) > 0) {
        $built_breadcrumbs = $cache->data->build;
        $path_breadcrumbs = $cache->data->variant;
        $path_breadcrumbs->from_cache = TRUE;
        $contexts = path_breadcrumbs_get_contexts_from_arguments($path_breadcrumbs->arguments, FALSE, $path);
        drupal_alter('path_breadcrumbs_view', $built_breadcrumbs, $path_breadcrumbs, $contexts);
      }

      // Store data in static cache.
      $path_breadcrumbs_data = $cache->data;
      return $cache->data;
    }
  }
  $path_breadcrumbs_data = new stdClass();

  // Select all variants matching current path.
  $variants = path_breadcrumbs_load_by_path($path);

  // Check if current path maches variant.
  // When first variant is found - return it.
  foreach ($variants as $breadcrumb) {

    // Replace placeholder in path with '*'.
    // Example: 'node/%node/view' -> 'node/*/view'.
    $matched_path = preg_replace("\n        /\\/%    # start with slash-percent\n        [^\\/]+  # all symbols except for the slash\n        /x", '/*', $breadcrumb->path);
    if (drupal_match_path($path, $matched_path)) {

      // Load breadcrumbs' contexts from current path.
      $contexts = path_breadcrumbs_get_contexts_from_arguments($breadcrumb->arguments, FALSE, $path);

      // If breadcrumb contains broken context
      // it means that unable to load context from URL.
      if (isset($contexts['broken_context'])) {
        continue;
      }

      // Check if breadcrumb is accessable for current page.
      if (!empty($breadcrumb->access)) {
        $access = ctools_access($breadcrumb->access, $contexts);
        if (!$access) {
          continue;
        }
      }

      // Appropriate variant found.
      $path_breadcrumbs_data->variant = $breadcrumb;

      // Build suitable breadcrumb variant.
      $path_breadcrumbs_data->build = _path_breadcrumbs_build_breadcrumbs($breadcrumb, $contexts);

      // Save data in cache and return it.
      if ($cache_enabled) {
        cache_set($cache_id, $path_breadcrumbs_data, PATH_BREADCRUMBS_CACHE_STORAGE, $cache_lifetime);
      }
      return $path_breadcrumbs_data;
    }
  }

  // Save empty data in cache and return it.
  if ($cache_enabled) {
    cache_set($cache_id, 'none', PATH_BREADCRUMBS_CACHE_STORAGE, $cache_lifetime);
  }
  return FALSE;
}