You are here

public function ViewsDependentFilter::buildOptionsForm in Views Dependent Filters 8

Provide the basic form which calls through to subforms. If overridden, it is best to call through to the parent, or to at least make sure all of the functions in this form are called.

Overrides FilterPluginBase::buildOptionsForm

File

src/Plugin/views/filter/ViewsDependentFilter.php, line 126

Class

ViewsDependentFilter
Filters by given list of related content title options.

Namespace

Drupal\views_dependent_filter\Plugin\views\filter

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);

  // Lock the exposed checkbox.
  $form['expose_button']['checkbox']['checkbox']['#disabled'] = TRUE;
  $form['expose_button']['checkbox']['checkbox']['#description'] = t('This filter is always exposed.');

  // Not sure what the 'expose' button is for as there's the checkbox, but
  // it's not wanted here.
  unset($form['expose_button']['markup']);
  unset($form['expose_button']['button']);
  $filters = $this->view->display_handler->handlers['filter'];
  if (isset($this->options['controller_filter'])) {

    // Get the handler for the controller filter.
    $controller_filter = $filters[$this->options['controller_filter']];

    // Take copies of the form arrays to pass to the other handler.
    $form_copy = $form;
    $form_state_copy = $form_state;

    // Fixup the form so the handler is fooled.
    // For some reason we need to add this for non-ajax admin operation.
    $form_copy['operator']['#type'] = '';

    // Get the value form from the filter handler.
    $controller_filter
      ->valueForm($form_copy, $form_state);
    $controller_values_element = $form_copy['value'];

    // Clean up the form element.
    if ($controller_values_element['#type'] == 'checkboxes') {

      // We have to unset the 'select all' option on checkboxes.
      unset($controller_values_element['#options']['all']);

      // Force multiple.
      $controller_values_element['#multiple'] = TRUE;
    }

    // Add it to our own form element in the real form.
    $form['controller_values'] = array(
      '#title' => t('Controller values'),
      '#description' => t('The values on the controller filter that will cause the dependent filters to be visible.'),
      '#default_value' => isset($this->options['controller_values']) ? $this->options['controller_values'] : array(),
    ) + $controller_values_element;
  }
  $options = $this
    ->get_filter_options('dependent');
  $form['dependent_filters'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Dependent filters'),
    '#options' => $options,
    '#default_value' => isset($this->options['dependent_filters']) ? $this->options['dependent_filters'] : array(),
    '#description' => t('The filters which should only be visible and active when the controller filter has the given values.'),
  );
  if (empty($options)) {
    $form['dependent_filters']['#description'] .= ' ' . t('This filter needs other filters to be placed below it in the order to use as dependents.');
  }
}