Commit ddc2a36d authored by Alan Sherry's avatar Alan Sherry Committed by Daniel Cothran
Browse files

Issue #3159852 by asherry, andileco: Create filter plugin for on/off fields

parent 3138c285
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
views.filter.views_fields_on_off_form:
  type: views.filter.in_operator
  label: 'Views Fields On/Off'
+2 −2
Original line number Diff line number Diff line
@@ -124,10 +124,10 @@ class ViewsFieldsOnOffForm extends FieldPluginBase {
   * {@inheritdoc}
   */
  public function query() {
    // This is not a real field and it only affects the query by excluding
    // This is not a real field, and it only affects the query by excluding
    // fields from the display. But Views won't render if the query()
    // method is not present. This doesn't do anything, but it has to be here.
    // This function is a void so it doesn't return anything.
    // This function is a void, so it doesn't return anything.
  }

}
+79 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\views_fields_on_off\Plugin\views\filter;

use Drupal\views\Plugin\views\filter\InOperator;

/**
 * Provides a handler that adds the form for Fields On/Off.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("views_fields_on_off_form")
 */
class ViewsFieldsOnOffForm extends InOperator {

  /**
   * {@inheritdoc}
   */
  public function canExpose() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function isExposed() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getValueOptions() {
    $all_fields = [];
    foreach ($this->displayHandler->getHandlers('field') as $id => $handler) {
      if ($label = $handler->label()) {
        $all_fields[$id] = $label;
      }
      else {
        $all_fields[$id] = $handler->adminLabel();
      }
    }
    if (isset($this->valueOptions)) {
      return $this->valueOptions;
    }
    $this->valueOptions = $all_fields;

    return $this->valueOptions;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    // This is not a real field, and it only affects the query by excluding
    // fields from the display. But Views won't render if the query()
    // method is not present. This doesn't do anything, but it has to be here.
    // This function is a void, so it doesn't return anything.
  }

  /**
   * Theme preprocess function.
   *
   * @see views_fields_on_off_preprocess_views_view()
   *
   * @param $variables
   *   Theme variables to be rendered.
   */
  public function preprocess(&$variables) {
    $field_options = $this->view->display_handler->getOption('fields');
    foreach ($field_options as $key => &$field) {
      if (isset($this->value[$key])) {
        continue;
      }

      unset($this->view->field[$key]);
    }
  }
}
+12 −0
Original line number Diff line number Diff line
@@ -112,3 +112,15 @@ function views_fields_on_off_views_pre_view(ViewExecutable $view, $display_id, a
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function views_fields_on_off_preprocess_views_view(&$variables) {
  $plugin_id = 'views_fields_on_off_form';
  $view = $variables['view'];

  if ($view->getHandler($view->current_display, 'filter', $plugin_id)) {
    $view->filter[$plugin_id]->preprocess($variables);
  }
}
+3 −0
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@ function views_fields_on_off_views_data() {
    'field' => [
      'id' => 'views_fields_on_off_form',
    ],
    'filter' => [
      'id' => 'views_fields_on_off_form',
    ],
  ];

  return $data;