You are here

function theme_views_tree in Views tree 7.2

Same name and namespace in other branches
  1. 6.2 views_tree.module \theme_views_tree()
  2. 6 views_tree.module \theme_views_tree()

Theme function for the tree style plugin.

We need to do some weirdness that makes more sense as a theme function than as a template.

@link http://drupal.org/node/355919

File

./views_tree.module, line 53
Views Tree module.

Code

function theme_views_tree($variables) {
  $view = $variables['view'];
  $options = $variables['options'];
  $rows = $variables['rows'];
  $title = $variables['title'];
  $result = $view->result;
  $fields =& $view->field;
  $parents = array();
  if (!$fields[$options['main_field']] instanceof views_handler_field) {
    drupal_set_message(t('Main field is invalid: %field', array(
      '%field' => $options['main_field'],
    )), 'error');
    return '';
  }
  if (!$fields[$options['parent_field']] instanceof views_handler_field) {
    drupal_set_message(t('Parent field is invalid: %field', array(
      '%field' => $options['parent_field'],
    )), 'error');
    return '';
  }

  // The field structure of Field API fields in a views result object is...
  // ridiculous. To avoid having to deal with it, we'll first iterate over all
  // records and normalize out the main and parent IDs to new properties. That
  // vastly simplifies the code that follows. This particular magic incantation
  // extracts the value from each record for the appropriate field specified by
  // the user. It then normalizes that value down to just an int, even though in
  // some cases it is an array. See views_tree_normalize_key(). Finally, we
  // build up a list of all main keys in the result set so that we can normalize
  // top-level records below.
  foreach ($result as $i => $record) {
    $result[$i]->views_tree_main = views_tree_normalize_key($fields[$options['main_field']]
      ->get_value($record), $fields[$options['main_field']]);
    $result[$i]->views_tree_parent = views_tree_normalize_key($fields[$options['parent_field']]
      ->get_value($record), $fields[$options['parent_field']]);
    $parents[] = $record->views_tree_main;
  }

  // Normalize the top level of records to all point to 0 as their parent
  // We only have to do this once, so we do it here in the wrapping function.
  foreach ($result as $i => $record) {
    if (!in_array($record->views_tree_parent, $parents)) {
      $result[$i]->views_tree_parent = 0;
    }
  }

  // Recursively render each item.
  $tree = theme('views_tree_inner', array(
    'view' => $view,
    'options' => $options,
    'rows' => $rows,
    'title' => $title,
    'result' => $result,
    'parent' => 0,
  ));

  // Add JS and CSS for collapsible tree, if configured.
  if (!empty($options['collapsible_tree'])) {
    drupal_add_js(array(
      'views_tree_setting' => array(
        $view->name => $options['collapsible_tree'],
      ),
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'views_tree') . '/js/collapsible.js');
    drupal_add_css(drupal_get_path('module', 'views_tree') . '/css/collapsible.css');
  }
  return $title . $tree;
}