You are here

views_tree.module in Views tree 6

Same filename and directory in other branches
  1. 8.2 views_tree.module
  2. 6.2 views_tree.module
  3. 7.2 views_tree.module

File

views_tree.module
View source
<?php

/**
 * Implementation of hook_views_api().
 */
function views_tree_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'views_tree'),
  );
}

/**
 * Implementation of hook_theme().
 */
function views_tree_theme($existing, $type, $theme, $path) {
  return array(
    'views_tree_inner' => array(
      'arguments' => array(
        'view' => NULL,
        'options' => array(),
        'rows' => array(),
        'title' => NULL,
        'result' => array(),
        'parent' => NULL,
      ),
    ),
  );
}

/**
 * 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.
 *
 * @ingroup themeable
 * @link http://drupal.org/node/355919
 */
function theme_views_tree($view, $options, $rows, $title) {
  $result = $view->result;
  $fields =& $view->field;
  $options['main_field_property'] = $fields[$options['main_field']]->field_alias;
  $options['parent_field_property'] = $fields[$options['parent_field']]->field_alias;

  // 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.
  $parents = array();
  foreach ($result as $record) {
    $parents[] = $record->{$options}['main_field_property'];
  }
  foreach ($result as $record) {
    if (!in_array($record->{$options}['parent_field_property'], $parents)) {
      $record->{$options}['parent_field_property'] = 0;
    }
  }
  return $title . theme('views_tree_inner', $view, $options, $rows, $title, $result, 0);
}

/**
 * Inner recursive theme function for views tree theming.
 *
 * @ingroup themeable
 * @param $view
 * @param $options
 * @param $row
 * @param $title
 * @param $result
 *   An array representing the raw data returned from the query.
 * @param $parent
 *   The id of the parent entry in the call stack.
 */
function theme_views_tree_inner($view, $options, $rows, $title, $result, $parent = NULL) {
  $items = array();
  foreach ($result as $i => $record) {
    if ($record->{$options}['parent_field_property'] == $parent) {
      $items[] = $rows[$i] . call_user_func(__FUNCTION__, $view, $options, $rows, $title, $result, $record->{$options}['main_field_property']);
    }
  }
  return count($items) ? theme('item_list', $items, NULL, $options['type']) : '';
}

Functions

Namesort descending Description
theme_views_tree Theme function for the tree style plugin.
theme_views_tree_inner Inner recursive theme function for views tree theming.
views_tree_theme Implementation of hook_theme().
views_tree_views_api Implementation of hook_views_api().