You are here

YamlFormFilterFormBase.php in YAML Form 8

File

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

namespace Drupal\yamlform\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides base class for form filter forms.
 */
abstract class YamlFormFilterFormBase extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $search = NULL, $state = NULL, array $state_options = []) {
    $form['#attributes'] = [
      'class' => [
        'yamlform-filter-form',
      ],
    ];
    $form['filter'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Filter submissions'),
      '#open' => TRUE,
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
    ];
    $form['filter']['search'] = [
      '#type' => 'search',
      '#title' => $this
        ->t('Filter by submitted data and/or notes'),
      '#title_display' => 'invisible',
      '#placeholder' => $this
        ->t('Filter by submitted data and/or notes'),
      '#maxlength' => 128,
      '#size' => 40,
      '#default_value' => $search,
    ];
    $form['filter']['state'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('State'),
      '#title_display' => 'invisible',
      '#options' => $state_options,
      '#default_value' => $state,
    ];
    $form['filter']['submit'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this
        ->t('Filter'),
    ];
    if (!empty($search) || !empty($state)) {
      $form['filter']['reset'] = [
        '#type' => 'submit',
        '#submit' => [
          '::resetForm',
        ],
        '#value' => $this
          ->t('Reset'),
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $query = [
      'search' => trim($form_state
        ->getValue('search')),
      'state' => trim($form_state
        ->getValue('state')),
    ];
    $form_state
      ->setRedirect($this
      ->getRouteMatch()
      ->getRouteName(), $this
      ->getRouteMatch()
      ->getRawParameters()
      ->all(), [
      'query' => $query,
    ]);
  }

  /**
   * Resets the filter selection.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function resetForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRedirect($this
      ->getRouteMatch()
      ->getRouteName(), $this
      ->getRouteMatch()
      ->getRawParameters()
      ->all());
  }

}

Classes

Namesort descending Description
YamlFormFilterFormBase Provides base class for form filter forms.