You are here

public function Select2Boxes::exposedFormAlter in Select2 Boxes 8

File

modules/select2_bef/src/Plugin/better_exposed_filters/filter/Select2Boxes.php, line 92

Class

Select2Boxes
Default widget implementation.

Namespace

Drupal\select2_bef\Plugin\better_exposed_filters\filter

Code

public function exposedFormAlter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
  $filter = $this->handler;
  $filter_id = $this
    ->getExposedFilterFieldId();
  parent::exposedFormAlter($form, $form_state);
  $form[$filter_id]['#type'] = 'select';
  $form[$filter_id]['#attributes']['class'][] = 'select2-widget';
  $form[$filter_id]['#attributes']['data-jquery-once-autocomplete'] = 'true';
  $form[$filter_id]['#attributes']['data-select2-autocomplete-list-widget'] = 'true';

  // Data attributes depending on if multiple values are allowed.
  if (empty($filter->options['expose']['multiple'])) {
    $form[$filter_id]['#multiple'] = FALSE;
    if (!empty($this->configuration['advanced']['limited_search'])) {
      $form[$filter_id]['#attributes']['data-minimum-search-length'] = $this->configuration['advanced']['minimum_search_length'];
    }
  }
  else {
    $form[$filter_id]['#attributes']['data-select2-multiple'] = 'true';
    $form[$filter_id]['#multiple'] = TRUE;
  }

  // Include the flags icons if enabled in BEF settings.
  if (!empty($this->configuration['advanced']['include_flags'])) {
    $this
      ->includeIcons($form, $filter->definition, $filter_id);
  }

  // Workaround to add support for merging process and pre-render functions
  // to the render array of an element.
  // @todo remove once core issue is resolved.
  // @see https://www.drupal.org/project/drupal/issues/2070131
  $form[$filter_id]['#process'][] = [
    '\\Drupal\\Core\\Render\\Element\\Select',
    'processSelect',
  ];
  $form[$filter_id]['#process'][] = [
    '\\Drupal\\Core\\Render\\Element\\Select',
    'processAjaxForm',
  ];
  $form[$filter_id]['#pre_render'][] = [
    '\\Drupal\\Core\\Render\\Element\\Select',
    'preRenderSelect',
  ];

  // Attach the JS.
  $form['#attached']['library'][] = 'select2boxes/widget';
}