You are here

public function EntityReferenceFilterViewResult::afterBuild in Views Reference Filter 8

After build form callback for exposed form with entity reference filters.

File

src/Plugin/views/filter/EntityReferenceFilterViewResult.php, line 373

Class

EntityReferenceFilterViewResult
Filter by entity id using items got from the another view..

Namespace

Drupal\entityreference_filter\Plugin\views\filter

Code

public function afterBuild(array $element, FormStateInterface $form_state) {
  $identifier = $this->options['expose']['identifier'];
  $form_id = $element['#id'];
  $controlling_filters = $this
    ->getControllingFilters();

  // Prevent Firefox from remembering values between page reloads.
  foreach ($controlling_filters as $filter) {
    if (isset($element[$filter])) {
      if (!isset($element[$filter]['#attributes'])) {
        $element[$filter]['#attributes'] = [];
      }
      $element[$filter]['#attributes']['autocomplete'] = 'off';
      foreach (Element::children($element[$filter]) as $child) {
        if (!isset($element[$filter][$child]['#attributes'])) {
          $element[$filter][$child]['#attributes'] = [];
        }
        $element[$filter][$child]['#attributes']['autocomplete'] = 'off';
      }
    }
  }

  /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_plugin **/
  $exposed_plugin = $this->view->display_handler
    ->getPlugin('exposed_form');
  $exposed_plugin_options = $exposed_plugin->options ?? NULL;
  $autosubmit = $exposed_plugin_options['bef']['general']['autosubmit'] ?? FALSE;

  // Send dependent filters settings into drupalSettings.
  if (!$autosubmit && !empty($controlling_filters)) {
    $element['#attached']['drupalSettings']['entityreference_filter'][$form_id]['view'] = [
      'view_name' => $this->view->storage
        ->id(),
      'view_display_id' => $this->view->current_display,
      'view_args' => Html::escape(implode('/', $this
        ->getViewArgs())),
      'view_context_args' => $this
        ->getViewContextArgs(),
      'view_path' => Html::escape($this->pathCurrent
        ->getPath()),
      'view_base_path' => $this->view
        ->getPath(),
      'view_dom_id' => $this->view->dom_id,
      'ajax_path' => Url::fromRoute('entityreference_filter.ajax')
        ->toString(),
    ];
    $element['#attached']['drupalSettings']['entityreference_filter'][$form_id]['dependent_filters_data'][$identifier] = $controlling_filters;
  }
  return $element;
}