You are here

public function DatePickers::exposedFormAlter in Better Exposed Filters 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/better_exposed_filters/filter/DatePickers.php \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\DatePickers::exposedFormAlter()

Manipulate views exposed from element.

Parameters

array $form: The views configuration form.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

Overrides FilterWidgetBase::exposedFormAlter

File

src/Plugin/better_exposed_filters/filter/DatePickers.php, line 34

Class

DatePickers
JQuery UI date picker widget implementation.

Namespace

Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter

Code

public function exposedFormAlter(array &$form, FormStateInterface $form_state) {
  $field_id = $this
    ->getExposedFilterFieldId();
  parent::exposedFormAlter($form, $form_state);

  // Attach the JS (@see /js/datepickers.js)
  $form['#attached']['library'][] = 'better_exposed_filters/datepickers';

  // Date picker settings.
  $form[$field_id]['#attached']['drupalSettings']['better_exposed_filters']['datepicker'] = TRUE;
  $form[$field_id]['#attached']['drupalSettings']['better_exposed_filters']['datepicker_options'] = [];
  $drupal_settings =& $form[$field_id]['#attached']['drupalSettings']['better_exposed_filters']['datepicker_options'];

  // Single Date API-based input element.
  $is_single_date = isset($form[$field_id]['value']['#type']) && 'date_text' == $form[$field_id]['value']['#type'];

  // Double Date-API-based input elements such as "in-between".
  $is_double_date = isset($form[$field_id]['min']) && isset($form[$field_id]['max']) && 'date_text' == $form[$field_id]['min']['#type'] && 'date_text' == $form[$field_id]['max']['#type'];

  // @todo lots of repetition of code. Let's re-organize and clean up.
  if ($is_single_date || $is_double_date) {
    if (isset($form[$field_id]['value'])) {
      $format = $form[$field_id]['value']['#date_format'];
      $form[$field_id]['value']['#attributes']['class'][] = 'bef-datepicker';
      $form[$field_id]['value']['#attributes']['type'] = 'date';
      $form[$field_id]['value']['#attributes']['autocomplete'] = 'off';
    }
    else {

      // Both min and max share the same format.
      $format = $form[$field_id]['min']['#date_format'];
      $form[$field_id]['min']['#attributes']['class'][] = 'bef-datepicker';
      $form[$field_id]['max']['#attributes']['class'][] = 'bef-datepicker';
      $form[$field_id]['min']['#attributes']['type'] = 'date';
      $form[$field_id]['max']['#attributes']['type'] = 'date';
      $form[$field_id]['min']['#attributes']['autocomplete'] = 'off';
      $form[$field_id]['max']['#attributes']['autocomplete'] = 'off';
    }

    // Convert Date API format to jQuery UI date format.
    $mapping = $this
      ->getjQueryUiDateFormatting();
    $drupal_settings['dateformat'] = str_replace(array_keys($mapping), array_values($mapping), $format);
  }
  else {

    /*
     * Standard Drupal date field.  Depending on the settings, the field
     * can be at $form[$field_id] (single field) or
     * $form[$field_id][subfield] for two-value date fields or filters
     * with exposed operators.
     */
    $fields = [
      'min',
      'max',
      'value',
    ];
    if (count(array_intersect($fields, array_keys($form[$field_id])))) {
      $form[$field_id]['#type'] = 'container';
      foreach ($fields as $field) {
        if (isset($form[$field_id][$field])) {
          $form[$field_id][$field]['#attributes']['class'][] = 'bef-datepicker';
          $form[$field_id][$field]['#attributes']['type'] = 'date';
          $form[$field_id][$field]['#attributes']['autocomplete'] = 'off';
        }
      }
    }
    else {
      $form[$field_id]['#attributes']['class'][] = 'bef-datepicker';
      $form[$field_id]['#attributes']['type'] = 'date';
      $form[$field_id]['#attributes']['autocomplete'] = 'off';
    }
  }
}