You are here

function custom_breadcrumbs_taxonomy_views_pre_render in Custom Breadcrumbs 7.2

Same name and namespace in other branches
  1. 6.2 custom_breadcrumbs_taxonomy/custom_breadcrumbs_taxonomy.module \custom_breadcrumbs_taxonomy_views_pre_render()

Implements hook_views_pre_render().

File

custom_breadcrumbs_taxonomy/custom_breadcrumbs_taxonomy.module, line 188
Main file for the Custom breadcrumbs for taxonomy.

Code

function custom_breadcrumbs_taxonomy_views_pre_render(&$view) {
  if (variable_get('custom_breadcrumbs_taxonomy_views', FALSE)) {
    module_load_include('inc', 'custom_breadcrumbs', 'custom_breadcrumbs_common');

    // Set the taxonomy breadcrumb for the view.
    if (isset($view->display) && !empty($view->display)) {
      $curpath = drupal_get_normal_path($_GET['q']);

      // A taxonomy term page is any page with path a component 'taxonomy'
      // followed by 'term'.
      $arg_values = arg(NULL, $curpath);
      $is_term_page = FALSE;
      if (($key = array_search('taxonomy', $arg_values)) !== FALSE) {
        $is_term_page = isset($arg_values[$key + 1]) && $arg_values[$key + 1] == 'term';
      }
      foreach ($view->display as $id => $display) {

        // Identify allowed displays for breadcrumb replacement.
        if (!_custom_breadcrumbs_allowed_display($display)) {
          continue;
        }
        $viewpath = _custom_breadcrumbs_construct_view_path($display);

        // Verify the view path matches the current path.
        if (_custom_breadcrumbs_match_path($curpath, $viewpath)) {

          // Select matching display with the greatest number of explicit
          // arguments.
          $num = substr_count($display->display_options['path'], '%');

          // @codingStandardsIgnoreLine
          if (!isset($max) || isset($max) && $num > $max) {
            $max = $num;
            $max_id = $id;
          }
        }
      }
      if (isset($max_id)) {
        $display = $view->display[$max_id];
        $arguments = _custom_breadcrumbs_views_display_arguments($display);
        if (isset($arguments) && !empty($arguments)) {
          $viewargs = isset($display->handler->view->args) && is_array($display->handler->view->args) ? $display->handler->view->args : array();
          $arg_values = _custom_breadcrumbs_views_parse_args($arguments, $viewargs);
          foreach ($arg_values['types'] as $key => $type) {
            $tid = NULL;
            $vid = NULL;
            switch ($type) {
              case 'tid':
                $tid = $arg_values['values'][$key];
                break;
              case 'vid':
                $vid = $arg_values['values'][$key];
                break;
            }
            if (!is_null($tid) || !is_null($vid)) {
              $terms = array();
              if (!is_null($tid)) {
                $term = taxonomy_term_load($tid);
                $vid = $term->vid;
                if ($term) {
                  $terms[$term->tid] = $term;
                }
              }
              module_load_include('inc', 'custom_breadcrumbs_taxonomy');
              _custom_breadcrumbs_taxonomy_set_breadcrumb($tid, $vid, $is_term_page, array(
                'view' => $view,
              ), $terms);
              if (!is_null($tid)) {
                _custom_breadcrumbs_taxonomy_recent_term($tid);
              }
              return;
            }
          }
        }

        // Otherwise, optionally use the first result node that is of an allowed
        // node type to generate the breadcrumb.
        if (variable_get('custom_breadcrumbs_taxonomy_result_nodes', FALSE) && _custom_breadcrumbs_allowed_display($display) && !empty($view->result)) {
          foreach ($view->result as $result) {
            if (isset($result->nid)) {
              $node = node_load($result->nid);
              if (_custom_breadcrumbs_taxonomy_allowed_node_type($node->type)) {
                module_load_include('inc', 'custom_breadcrumbs_taxonomy');
                $term = custom_breadcrumbs_taxonomy_node_get_term($node);
                if ($term) {
                  $terms = custom_breadcrumbs_taxonomy_node_get_terms($node);
                  _custom_breadcrumbs_taxonomy_set_breadcrumb($term->tid, $term->vid, $is_term_page, array(
                    'node' => $node,
                  ), $terms);
                  _custom_breadcrumbs_taxonomy_recent_term($term->tid);
                  return;
                }
              }
            }
          }
        }
      }
    }
  }
}