You are here

views_flipped_table.module in Views Flipped Table 8

Same filename and directory in other branches
  1. 6 views_flipped_table.module
  2. 7 views_flipped_table.module

Flipped table style plugin, flipping rows and columns.

File

views_flipped_table.module
View source
<?php

/**
 * @file
 * Flipped table style plugin, flipping rows and columns.
 */

/**
 * Prepares variables for views flipped table templates.
 *
 * Default template: views-view-flipped-table.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
 */
function template_preprocess_views_view_flipped_table(&$variables) {
  \Drupal::moduleHandler()
    ->loadInclude('views', 'inc', 'views.theme');

  // Let the base table preprocessor act first
  template_preprocess_views_view_table($variables);
  $view = $variables['view'];
  $options = $view->style_plugin->options;

  // Flip the table.
  $variables['rows_flipped'] = array();
  foreach ($variables['rows'] as $row_index => $row) {
    foreach ($row['columns'] as $field_name => $value) {
      $variables['rows_flipped'][$field_name]['columns'][$row_index] = $value;
    }
  }

  // Determine if the first row is to be shown as a table header.
  $variables['first_row_header'] = (bool) $options['flipped_table_header_first_field'];
  if ($variables['first_row_header']) {
    $variables['flipped_header'] = array();
    $field_names = array_keys($variables['rows_flipped']);
    $field_name = reset($field_names);

    // Flipped header gets the first column of items
    foreach ($variables['rows_flipped'][$field_name]['columns'] as $index => $item) {
      $variables['flipped_header'][] = $item;
    }

    // Set the field name for the header row
    $variables['flipped_header_field_name'] = $field_name;

    // Remove the first row
    array_shift($variables['rows_flipped']);
  }

  // Check if any of the columns have a label set
  $variables['show_labels'] = FALSE;
  foreach ($variables['header'] as $item) {
    if (!empty($item['content'])) {
      $variables['show_labels'] = TRUE;
      break;
    }
  }
}

Functions

Namesort descending Description
template_preprocess_views_view_flipped_table Prepares variables for views flipped table templates.