You are here

views_fields_on_off.module in Views Fields On/Off 8

Same filename and directory in other branches
  1. 7 views_fields_on_off.module

Provides a Views Global field that allows users to turn fields on/off.

File

views_fields_on_off.module
View source
<?php

/**
 * @file
 * Provides a Views Global field that allows users to turn fields on/off.
 */
use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_pre_view().
 */
function views_fields_on_off_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  $hasFields = array_key_exists('fields', $view->display_handler->options);
  if ($hasFields) {
    $useDefaultDisplay = !$view->display_handler->options['fields'] && $view->display_handler->default_display->options['fields'];
    if ($useDefaultDisplay) {
      $fields = $view->display_handler->default_display->options['fields'];
    }
    else {
      $fields = $view->display_handler->options['fields'];
    }
    $hasViewsFieldsOnOffHandler = FALSE;
    foreach ($fields as $field) {
      if (array_key_exists('plugin_id', $field)) {
        if ($field['plugin_id'] === 'views_fields_on_off_form') {
          $hasViewsFieldsOnOffHandler = TRUE;
        }
      }
    }
    if ($hasViewsFieldsOnOffHandler) {

      // Grab the fields_on_off values that have been submitted already.
      $params = \Drupal::request()->query
        ->all();

      // This is for a GET request.
      // If the view is submitted through AJAX, like in view preview, it will be
      // a POST request. Merge the parameter arrays and we’ll get our values.
      $postParams = \Drupal::request()->request
        ->all();
      $params = array_merge($params, $postParams);

      // We need $on_off_submitted because if the form is submitted with no
      // checkboxes checked, none of the fields_on_off values will be present,
      // so it thinks this is a fresh view and all the columns should be
      // checked.
      $on_off_submitted = array_key_exists('fields_on_off_hidden_submitted', $params);

      // Get all the fields known to be checked based on the params.
      $checked_fields = [];

      // Get the fields marked as able to be hidden if not checked.
      $hideable_fields = [];
      foreach ($params as $key => $value) {
        if (strpos($key, 'views_fields_on_off_form') === 0) {
          if (!empty($view
            ->getHandler($display_id, 'field', $key)['exposed_select_type']) && ($view
            ->getHandler($display_id, 'field', $key)['exposed_select_type'] === 'radios' || $view
            ->getHandler($display_id, 'field', $key)['exposed_select_type'] === 'select')) {
            $reformatted_value = [];
            $reformatted_value[$value] = $value;
            $checked_fields = array_merge($checked_fields, $reformatted_value);
          }
          elseif (!empty($view
            ->getHandler($display_id, 'field', $key)['exposed_select_type']) && $view
            ->getHandler($display_id, 'field', $key)['exposed_select_type'] === 'multi_select') {
            foreach ($value as $result) {
              $reformatted_value = [];
              $reformatted_value[$result] = $result;
              $checked_fields = array_merge($checked_fields, $reformatted_value);
            }
          }
          else {
            $checked_fields = array_merge($checked_fields, $value);
          }
          $hideable = $fields[$key]['fields'];
          foreach ($hideable as $hideable_key => $hideable_value) {
            if ($hideable[$hideable_key] === 0) {
              unset($hideable[$hideable_key]);
            }
          }
          $hideable_fields = array_merge($hideable_fields, $hideable);
        }
      }

      // Here if there are no checked fields but the form has been submitted,
      // we want to turn off the field.
      if (count($checked_fields) || $on_off_submitted) {
        foreach ($fields as $key => $field) {
          if (!array_key_exists($key, $checked_fields) && array_key_exists($key, $hideable_fields) && $hideable_fields[$key]) {

            // If there are fields specified, and this field is one of them,
            // hide it!
            $fields[$key]['exclude'] = 1;
          }
        }
      }
      foreach ($fields as $key => $value) {
        if (strpos($key, 'views_fields_on_off_form') === 0) {

          // And always hide the on/off field or it'll just show up empty.
          $fields[$key]['exclude'] = 1;
        }
      }
      if ($useDefaultDisplay) {
        $view->display_handler->default_display->options['fields'] = $fields;
      }
      else {
        $view->display_handler->options['fields'] = $fields;
      }
    }
  }
}

Functions