You are here

function hansel_taxonomy_action_add_term_path_get_crumbs in Hansel breadcrumbs 8

Same name and namespace in other branches
  1. 7 taxonomy/hansel_taxonomy.module \hansel_taxonomy_action_add_term_path_get_crumbs()

Callback for "add term path" breadcrumb action

Parameters

array $arguments Values from the configuration form.:

Return value

array

1 string reference to 'hansel_taxonomy_action_add_term_path_get_crumbs'
hansel_taxonomy_hansel_action_types in taxonomy/hansel_taxonomy.module
Implements hook_hansel_action_types().

File

taxonomy/hansel_taxonomy.module, line 39
Hansel taxonomy integration

Code

function hansel_taxonomy_action_add_term_path_get_crumbs($arguments) {
  $links = array();
  if (drupal_strtolower(hansel_arg(0)) == 'taxonomy' && drupal_strtolower(hansel_arg(1)) == 'term' && is_numeric(hansel_arg(2))) {
    $tid = hansel_arg(2);
    if ($data = hansel_cache_get("taxonomy:t{$tid}")) {
      return $data;
    }
    if ($term = taxonomy_term_load($tid)) {
      $parents = taxonomy_get_parents_all($term->tid);
      if (function_exists('i18n_taxonomy_localize_terms')) {
        $parents = i18n_taxonomy_localize_terms($parents);
      }
      foreach ($parents as $term) {
        $links[] = array(
          'title' => $term->name,
          'href' => 'taxonomy/term/' . $term->tid,
        );
      }
      $links = array_reverse($links);
    }
    hansel_cache_set("taxonomy:t{$tid}", $links);
  }
  elseif (drupal_strtolower(hansel_arg(0)) == 'node' && is_numeric(hansel_arg(1))) {
    $nid = hansel_arg(1);
    if ($data = hansel_cache_get("taxonomy:n{$nid}")) {
      return $data;
    }
    if ($node = node_load($nid)) {
      $terms = array();
      foreach (field_info_instances('node', $node->type) as $field => $info) {
        if (isset($info['widget']['module']) && ($info['widget']['module'] == 'taxonomy' || $info['widget']['module'] == 'options')) {
          $term_field = $node->{$field};
          if (isset($term_field[$node->language]) || isset($term_field['und'])) {
            $localized_terms = isset($term_field[$node->language]) ? $term_field[$node->language] : $term_field['und'];
            foreach ($localized_terms as $term) {
              if (!isset($term['tid'])) {

                // Some items may not be taxonomy terms, like Commerce products (see #1365428).
                continue;
              }
              if (!isset($term['taxonomy_term'])) {
                $term['taxonomy_term'] = taxonomy_term_load($term['tid']);
              }
              if (empty($arguments['vid']) || !empty($arguments['vid']) && $term['taxonomy_term']->vid == $arguments['vid']) {
                $terms[$term['tid']] = $term['taxonomy_term'];
              }
            }
          }
        }
      }
      if ($term = reset($terms)) {
        $link = array();
        $link[$term->tid] = array(
          'title' => $term->name,
          'href' => 'taxonomy/term/' . $term->tid,
        );
        $parents = taxonomy_get_parents_all($term->tid);
        if (function_exists('i18n_taxonomy_localize_terms')) {
          $parents = i18n_taxonomy_localize_terms($parents);
        }
        foreach ($parents as $parent) {
          $link[$parent->tid] = array(
            'title' => $parent->name,
            'href' => 'taxonomy/term/' . $parent->tid,
          );
        }
        foreach (array_reverse($link) as $tid => $value) {
          $links[$tid] = $value;
        }
      }
    }
    hansel_cache_set("taxonomy:n{$nid}", $links);
  }
  return $links;
}