You are here

SettingsForm.php in Views Filter Harmonizer 1.0.x

Same filename and directory in other branches
  1. 8 src/Form/SettingsForm.php

File

src/Form/SettingsForm.php
View source
<?php

namespace Drupal\filter_harmonizer\Form;

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

/**
 * Provides the from to edit Views Filter Harmonizer admin config settings.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'filter_harmonizer_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $msg1 = $this
      ->t('The following views have paris of contextual and regular filters on the same fields.');
    $msg2 = $this
      ->t('You may select any of these views for harmonization.');
    $form['fh_views_with_paired_filters'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this
        ->t('Harmonizable views'),
      '#description' => "{$msg1}<br/>{$msg2}",
    ];
    $eligible_view_info = [];
    foreach (Views::getAllViews() as $view) {
      $view_id = $view
        ->id();
      $view_label = $view
        ->get('label');
      if (views_view_is_disabled($view)) {
        $view_label .= ' (' . $this
          ->t('disabled') . ')';
      }
      if ($filter_pairs = filter_harmonizer_harmonize_and_record_filter_pairs($view
        ->getExecutable())) {
        if (self::viewHasFilterPair($view_id, $filter_pairs)) {
          $eligible_view_info[$view_id] = [
            'label' => $view_label,
            'displays' => $filter_pairs[$view_id],
          ];
        }
      }
    }
    $config = $this
      ->config('filter_harmonizer.settings');
    $harmonized_view_ids = $config
      ->get('filter_harmonizer_harmonized_view_ids') ?? [];
    foreach ($eligible_view_info as $view_id => $view_info) {
      $field_labels = [];
      foreach ($view_info['displays'] as $filter_pairs) {
        foreach (array_column($filter_pairs, 'field_label') as $label) {
          if (!in_array($label, $field_labels)) {
            $field_labels[] = $label;
          }
        }
      }
      $form['fh_views_with_paired_filters']['view_' . $view_id] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('View %view_label: @field_labels', [
          '%view_label' => $view_info['label'],
          '@field_labels' => implode(', ', $field_labels),
        ]),
        '#default_value' => in_array($view_id, $harmonized_view_ids),
      ];
    }
    $form['fh_display_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Contextual and regular filter display options'),
      '#description' => '',
      '#open' => TRUE,
    ];
    $form['fh_display_options']['fh_contextual_args_in_exposed_form'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Reflect contextual (URL) arguments on the regular filter form'),
      '#default_value' => $config
        ->get('filter_harmonizer_contextual_args_in_exposed_form'),
      '#description' => $this
        ->t('This helps to avoid confusion when the page was initially loaded with contextual arguments on the URL.<br/>Does not work when "Use AJAX" is ticked.'),
    ];
    $form['fh_display_options']['fh_regular_filter_values_in_address_bar'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Reflect regular filter selections on the browser URL/address bar'),
      '#default_value' => $config
        ->get('filter_harmonizer_regular_filter_values_in_address_bar'),
      '#description' => $this
        ->t('This helps to avoid confusion after the page is re-submitted with new regular filter values.<br/>It also allows for easy sharing of a filtered page URL in emails and on social media, because the URL reflects the filter selection you made.<br/>Does not apply to Views UI admin pages. Does not work when "Use AJAX" is ticked.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $harmonized_view_ids = [];
    foreach ($form_state
      ->getValues() as $key => $value) {
      if ('view_' == substr($key, 0, 5) && $value == 1) {
        $harmonized_view_ids[] = substr($key, 5);
      }
    }
    $this
      ->config('filter_harmonizer.settings')
      ->set('filter_harmonizer_harmonized_view_ids', $harmonized_view_ids)
      ->set('filter_harmonizer_regular_filter_values_in_address_bar', $form_state
      ->getValue('fh_regular_filter_values_in_address_bar'))
      ->set('filter_harmonizer_contextual_args_in_exposed_form', $form_state
      ->getValue('fh_contextual_args_in_exposed_form'))
      ->save();
  }

  /**
   * {@inheritdoc}
   *
   * To allow saving of config settings in above submitForm().
   */
  protected function getEditableConfigNames() {
    return [
      'filter_harmonizer.settings',
    ];
  }

  /**
   * Returns if the View has a contextual and regular filter pair on a field.
   *
   * @param string $view_id
   *   The View id.
   * @param array $filter_pairs
   *   Array of filter pair information, indexed by display ID.
   *
   * @return bool
   *   TRUE if the View has at least one field with a filter pair.
   */
  public static function viewHasFilterPair($view_id, array $filter_pairs) {
    if (!empty($filter_pairs[$view_id])) {
      foreach ($filter_pairs[$view_id] as $field_info) {
        foreach ($field_info as $filters) {
          if (!empty($filters['contextual']) && !empty($filters['regular'])) {
            return TRUE;
          }
        }
      }
    }
    return FALSE;
  }

}

Classes

Namesort descending Description
SettingsForm Provides the from to edit Views Filter Harmonizer admin config settings.