You are here

function hansel_action_add_parents_get_crumbs in Hansel breadcrumbs 8

Same name and namespace in other branches
  1. 7 hansel.actions.inc \hansel_action_add_parents_get_crumbs()

Callback for the "add single link" breadcrumb action to generate the crumbs.

Parameters

array $arguments:

Return value

array

1 string reference to 'hansel_action_add_parents_get_crumbs'
hansel_hansel_action_types in ./hansel.module
Implements hook_hansel_action_types().

File

./hansel.actions.inc, line 202
Hansel breadcrumb actions

Code

function hansel_action_add_parents_get_crumbs($arguments) {
  $args = array();
  $i = 0;
  while ($arg = hansel_arg($i++)) {
    $args[] = $arg;
  }
  $path = implode('/', $args);
  if ($path == variable_get('site_frontpage', 'node')) {

    // Do not output parents for frontpage.
    return array();
  }
  if ($crumbs = hansel_cache_get("parents:{$path}")) {
    return $crumbs;
  }
  $crumbs = array();
  $parent_hashes = array();
  $modules = module_implements('hansel_get_parent');
  while (TRUE) {
    foreach ($modules as $module) {
      $hook = "{$module}_hansel_get_parent";
      if ($parent = $hook($path)) {

        // Create a hash to check recursion. The reason that we do not simply
        // check the paths is that the site structure may not be hierarchical.
        // There could be a parent item with the same path. This is often used
        // in cases where a top level item item has the same link as its first
        // child item.
        $hash = crc32($parent['path'] . $parent['title']);
        if (in_array($hash, $parent_hashes)) {

          // We are in recursion.
          break 2;
        }
        $parent_hashes[] = $hash;
        if ($parent['path'] == variable_get('site_frontpage', 'node') || $parent['path'] == '<front>') {

          // The frontpage is not added as a parent, to avoid issues having
          // duplicated items in the breadcrumb when the frontpage is created
          // as a menu item.
          break;
        }
        array_unshift($crumbs, array(
          'title' => $parent['title'],
          'href' => $parent['path'],
        ));
        $path = $parent['path'];
        continue 2;
      }
    }

    // We tried all hooks but did not find a parent.
    break;
  }
  hansel_cache_set('parents:' . implode('/', $args), $crumbs);
  return $crumbs;
}