Commit ad3e4a4c authored by Daniel Cothran's avatar Daniel Cothran Committed by Daniel Cothran
Browse files

Issue #3042469 by andileco, franksj, ranavaibhav, maxilein: Initial view...

Issue #3042469 by andileco, franksj, ranavaibhav, maxilein: Initial view should properly reflect fields that are on or off
parent d4b46014
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@ class ViewsFieldsOnOffForm extends FieldPluginBase {

    $form[$field_id] = [
      '#type' => $this->options['exposed_select_type'],
      '#plugin_id' => 'views_fields_on_off_form',
      '#default_enabled' => $this->options['default_enabled'],
      '#title' => $this->t('@value', [
        '@value' => $label,
      ]),
@@ -76,6 +78,7 @@ class ViewsFieldsOnOffForm extends FieldPluginBase {

    $options['fields'] = ['default' => []];
    $options['exposed_select_type'] = ['default' => 'checkboxes'];
    $options['default_enabled'] = ['default' => NULL];

    return $options;
  }
@@ -117,6 +120,11 @@ class ViewsFieldsOnOffForm extends FieldPluginBase {
      ],
      '#default_value' => $this->options['exposed_select_type'],
    ];
    $form['default_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable fields by default.'),
      '#default_value' => $this->options['default_enabled'],
    ];

  }

+35 −3
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@
 * Provides a Views Global field that allows users to turn fields on/off.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;

/**
 * Implements hook_views_pre_view().
@@ -39,12 +41,12 @@ function views_fields_on_off_views_pre_view(ViewExecutable $view, $display_id, a
          $select_type = $view->getHandler($display_id, 'field', $views_on_off_field)['exposed_select_type'];
          foreach ($fields[$views_on_off_field]['fields'] as $field_option) {
            if (in_array($select_type, ['select', 'radios'])) {
              if (($field_option !== $params[$views_on_off_field]) && !empty($field_option)) {
              if (!empty($params[$views_on_off_field]) && ($field_option !== $params[$views_on_off_field]) && !empty($field_option)) {
                $fields[$field_option]['exclude'] = 1;
              }
            }
            else {
              if (!in_array($field_option, $params[$views_on_off_field]) && !empty($field_option)) {
              if ((empty($params[$views_on_off_field]) && !empty($field_option)) || (!in_array($field_option, $params[$views_on_off_field]) && !empty($field_option))) {
                $fields[$field_option]['exclude'] = 1;
              }
            }
@@ -55,7 +57,7 @@ function views_fields_on_off_views_pre_view(ViewExecutable $view, $display_id, a
      else {
        foreach ($views_on_off_fields as $views_on_off_field) {
          foreach ($fields[$views_on_off_field]['fields'] as $field_option) {
            if (!empty($field_option)) {
            if (!empty($field_option) && !$fields[$views_on_off_field]['default_enabled']) {
              $fields[$field_option]['exclude'] = 1;
            }
          }
@@ -73,8 +75,38 @@ function views_fields_on_off_views_pre_view(ViewExecutable $view, $display_id, a
function views_fields_on_off_preprocess_views_view(&$variables) {
  $plugin_id = 'views_fields_on_off_form';
  $view = $variables['view'];
  $variables['exposed']['#parent_view'] = $view->id();

  if ($view->getHandler($view->current_display, 'filter', $plugin_id)) {
    $view->filter[$plugin_id]->preprocess($variables);
  }
}

/**
 * Implements template_preprocess_views_exposed_form().
 */
function views_fields_on_off_preprocess_views_exposed_form(&$variables) {
  $view = Views::getView($variables['form']['#parent_view']);
  $submitted = $view->getExposedInput();
  if (empty($submitted)) {
    foreach ($variables['form'] as $form_item) {
      if (!empty($form_item['#plugin_id']) && ($form_item['#plugin_id'] === 'views_fields_on_off_form') && $form_item['#default_enabled']) {
        $select_type = $form_item['#type'];
        if (in_array($select_type, ['checkboxes', 'select'])) {
          if ($select_type === 'checkboxes') {
            foreach ($form_item['#options'] as $key => $option) {
              $field_name = $form_item['#name'];
              $variables['form'][$field_name][$key]['#checked'] = TRUE;
            }
          }
          elseif ($form_item['#multiple']) {
            foreach ($form_item['#options'] as $key => $option) {
              $field_name = $form_item['#name'];
              $variables['form'][$field_name]['#value'][] = $key;
            }
          }
        }
      }
    }
  }
}