You are here

function panels_breadcrumbs_build_breadcrumb in Panels Breadcrumbs 7.2

Builds a custom breadcrumb from the provided contexts and configuration.

Parameters

array $conf: An array containing:

  • panels_breadcrumbs_title: A string containing a series of titles separated by a line break.
  • panels_breadcrumbs_paths: A string containing a series of paths separated by a line break.
  • panels_breadcrumbs_home: A boolean that enables the display of an 'Home' root link on the breadcrumb.
  • panels_breadcrumbs_menu_active_trail: A boolean that enables menu active trail as prefix.

array $contexts: A ctools contexts array.

bool $override: A boolean that makes this breadcrumb configuration override the global settings.

Return value

mixed A properly structured array of links as expected by theme_breadcrumb(), or FALSE if the breadcrumb could not be built with the given configuration.

3 calls to panels_breadcrumbs_build_breadcrumb()
panels_breadcrumbs_ctools_render_alter in ./panels_breadcrumbs.module
Implements hook_ctools_render_alter().
panels_breadcrumbs_page_breadcrumb_content_type_render in ./panels_breadcrumbs.module
Render callback override of 'page_breadcrumb' ctools content-type.
panels_breadcrumbs_panelizer_pre_render_alter in ./panels_breadcrumbs.module
Implements hook_panelizer_pre_render_alter().

File

./panels_breadcrumbs.module, line 171
Main file for panels breadcrumbs module.

Code

function panels_breadcrumbs_build_breadcrumb(array $conf, array $contexts, $override = FALSE) {
  $breadcrumbs =& drupal_static(__FUNCTION__);
  if (empty($breadcrumbs)) {

    // If no titles or paths are defined, bail out.
    if (!isset($conf['panels_breadcrumbs_titles']) || !isset($conf['panels_breadcrumbs_paths'])) {

      // Do not store anything in the static $breadcrumbs variable, in order to
      // allow a possible fallback with another breadcrumb configuration.
      return FALSE;
    }

    // The configuration is active and is the first triggered, so we don't allow
    // another breadcrumb configuration to override it by setting the static
    // variable.
    $breadcrumbs = array();

    // Look for placeholder tokens in paths and convert them for this display.
    $titles = $conf['panels_breadcrumbs_titles'];
    $paths = ctools_context_keyword_substitute($conf['panels_breadcrumbs_paths'], array(), $contexts);

    // Break titles and paths into arrays and remove empty keys.
    $titles = array_filter(array_map('trim', explode("\n", $titles)), 'strlen');
    $paths = array_filter(array_map('trim', explode("\n", $paths)), 'strlen');

    // We add in path as although it is not used to render breadcrumbs, it is
    // required by user_menu_breadcrumb_alter().
    $default_breadcrumb_info = array(
      'title' => '',
      'href' => '',
      'localized_options' => array(),
      'path' => '',
    );
    $breadcrumbs_info = array();

    // Set the first crumb to home.
    if (!isset($conf['panels_breadcrumbs_home']) || $conf['panels_breadcrumbs_home'] == TRUE) {
      $breadcrumbs_info[] = array(
        'title' => t('Home'),
        'href' => '<front>',
      ) + $default_breadcrumb_info;
    }

    // Iterate through all crumbs and add them to the breadcrumb.
    foreach ($titles as $key => $title) {

      // Translate the title and convert existing placeholder tokens afterwards.
      $translated_title = t('@title', array(
        '@title' => $title,
      ));
      $translated_title = ctools_context_keyword_substitute($translated_title, array(), $contexts);
      if (empty($translated_title)) {
        continue;
      }
      $translated_title = html_entity_decode(trim($translated_title), ENT_QUOTES, 'UTF-8');
      $path = empty($paths[$key]) ? '<none>' : trim($paths[$key]);
      $breadcrumbs_info[] = array(
        'title' => $translated_title,
        'href' => $path,
      ) + $default_breadcrumb_info;
    }

    // Allow other modules to intercept and operate changes in the breadcrumb.
    $breadcrumb_info_end = end($breadcrumbs_info);
    drupal_alter('menu_breadcrumb', $breadcrumbs_info, $breadcrumb_info_end);

    // Finally, render the breadcrumb.
    foreach ($breadcrumbs_info as $crumb) {
      if (isset($crumb['href']) && $crumb['href'] == '<none>') {
        $crumb['localized_options'] += array(
          'attributes' => array(),
          'html' => TRUE,
        );
        $attributes = drupal_attributes($crumb['localized_options']['attributes']);
        $content = $crumb['localized_options']['html'] ? filter_xss_admin($crumb['title']) : check_plain($crumb['title']);
        $breadcrumbs[] = sprintf('<span %s>%s</span>', $attributes, $content);
      }
      else {
        $breadcrumbs[] = l($crumb['title'], $crumb['href'], $crumb['localized_options']);
      }
    }
  }

  // Optionally override the global breadcrumb configuration.
  if ($override) {
    if (!isset($conf['panels_breadcrumbs_menu_active_trail']) || $conf['panels_breadcrumbs_menu_active_trail'] == TRUE) {
      $prefix = menu_get_active_breadcrumb();
      $breadcrumbs = array_merge($prefix, $breadcrumbs);
    }
    drupal_set_breadcrumb($breadcrumbs);
  }
  return $breadcrumbs;
}