You are here

function hansel_tokens in Hansel breadcrumbs 7

Same name and namespace in other branches
  1. 8 hansel.module \hansel_tokens()

Implements of hook_tokens().

File

./hansel.module, line 802
Hansel module

Code

function hansel_tokens($type, $tokens, $data = array(), $options = array()) {
  global $_hansel_test_path;
  $hansel_types = array(
    'node' => 'node',
    'term' => 'taxonomy_term',
    'user' => 'user',
  );
  if (!array_key_exists($type, $hansel_types) || !isset($data[$hansel_types[$type]]) || empty($data[$hansel_types[$type]])) {
    return array();
  }
  $object = $data[$hansel_types[$type]];

  // If our token is not provided, we do not have to perform any tasks.
  if (array_search('hansel-path', $tokens) === FALSE) {
    return array();
  }

  // If this is a new node it won't have nid yet.
  if (!isset($object->nid)) {
    return array();
  }
  switch ($type) {
    case 'node':
      $_hansel_test_path = 'node/' . $object->nid;
      $title = $object->title;
      break;
    case 'taxonomy':
      $_hansel_test_path = 'taxonomy/term/' . $object->tid;
      $title = $object->name;
      break;
    case 'user':
      $_hansel_test_path = 'user/' . $object->uid;
      $title = $object->name;
      break;
    default:
      return array();
  }
  $path = hansel_get_breadcrumbs(FALSE, TRUE, TRUE, array(
    'alias',
  ));
  if (is_array($path)) {
    $path = $path['breadcrumb'];
  }
  else {
    $path = array();
  }
  $_hansel_test_path = NULL;
  if (variable_get('hansel_remove_first_token_item', TRUE)) {
    array_shift($path);
  }
  if (empty($path)) {
    $path = array(
      t('content'),
      $title,
    );
  }
  $path = array_map('strtolower', $path);
  if (module_exists('pathauto')) {
    module_load_include('inc', 'pathauto');
    if (function_exists('pathauto_cleanstring')) {
      $path = array_map('pathauto_cleanstring', $path);
    }
  }
  $path = implode('/', $path);
  $replacements = array();
  foreach ($tokens as $name => $original) {
    if ($name == 'hansel-path') {
      $replacements[$original] = $path;
    }
  }
  return $replacements;
}