You are here

public function SettingsForm::buildForm in Views Filter Harmonizer 1.0.x

Same name and namespace in other branches
  1. 8 src/Form/SettingsForm.php \Drupal\filter_harmonizer\Form\SettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 24

Class

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

Namespace

Drupal\filter_harmonizer\Form

Code

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);
}