You are here

views_tree.module in Views tree 8.2

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

Views tree module.

File

views_tree.module
View source
<?php

/**
 * @file
 * Views tree module.
 */

/**
 * Implements hook_theme().
 */
function views_tree_theme($existing, $type, $theme, $path) {
  return [
    'views_tree' => [
      'variables' => [
        'view' => NULL,
        'options' => [],
        'list_type' => 'ul',
        'rows' => [],
        'title' => NULL,
        'items' => [],
      ],
    ],
    'views_tree_table' => [
      'variables' => [
        'view' => NULL,
        'options' => [],
        'list_type' => 'ul',
        'rows' => [],
        'title' => NULL,
        'items' => [],
      ],
    ],
  ];
}

/**
 * Prepares variables for a views tree display.
 *
 * @param array $variables
 *   The variables to pass on to the template.
 */
function template_preprocess_views_tree(array &$variables) {
  $rows = $variables['rows'];

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $options = $view
    ->getStyle()->options;
  $variables['#attached']['library'][] = 'views_tree/views_tree';

  // Add JS and CSS for collapsible tree, if configured.
  if (!empty($options['collapsible_tree'])) {
    $variables['#attached']['drupalSettings'] = [
      'views_tree_settings' => [
        [
          $view
            ->id(),
          $options['collapsible_tree'],
        ],
      ],
    ];
  }
  $variables['options'] = $options;
  $variables['list_type'] = $options['type'];

  /** @var \Drupal\views_tree\TreeHelper $tree_helper */
  $tree_helper = \Drupal::service('views_tree.tree');
  $variables['items'] = $tree_helper
    ->buildRenderTree($view, $rows);
}

/**
 * Prepares variables for a views tree table display.
 */
function template_preprocess_views_tree_table(array &$variables) {

  // Set variables from core's table display.
  template_preprocess_views_view_table($variables);
  $view = $variables['view'];
  $rows = $variables['rows'];
  $options = $view
    ->getStyle()->options;

  // Add a class to the main field cell.
  foreach ($rows as $key => $row) {
    $row['columns'][$options['display_hierarchy_column']]['attributes']
      ->addClass('views-tree-hierarchy-cell');
    $rows[$key] = $row;
  }
  $tree_service = \Drupal::service('views_tree.tree');
  $variables['items'] = $tree_service
    ->buildRenderTree($view, $rows);

  // Since an HTML table isn't representative of a hierarchy, add data
  // data attributes for this representation.
  $tree_service
    ->addDataAttributes($variables['items']);

  // Attach layout library.
  $variables['#attached']['library'][] = 'views_tree/views_tree_table';
}

Functions

Namesort descending Description
template_preprocess_views_tree Prepares variables for a views tree display.
template_preprocess_views_tree_table Prepares variables for a views tree table display.
views_tree_theme Implements hook_theme().