function FilterPluginBase::buildExposedForm

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/filter/FilterPluginBase.php \Drupal\views\Plugin\views\filter\FilterPluginBase::buildExposedForm()
  2. 8.9.x core/modules/views/src/Plugin/views/filter/FilterPluginBase.php \Drupal\views\Plugin\views\filter\FilterPluginBase::buildExposedForm()
  3. 10 core/modules/views/src/Plugin/views/filter/FilterPluginBase.php \Drupal\views\Plugin\views\filter\FilterPluginBase::buildExposedForm()

Render our chunk of the exposed filter form when selecting.

You can override this if it doesn't do what you expect.

Overrides HandlerBase::buildExposedForm

File

core/modules/views/src/Plugin/views/filter/FilterPluginBase.php, line 942

Class

FilterPluginBase
Base class for Views filters handler plugins.

Namespace

Drupal\views\Plugin\views\filter

Code

public function buildExposedForm(&$form, FormStateInterface $form_state) {
    if (empty($this->options['exposed'])) {
        return;
    }
    // Build the exposed form, when its based on an operator.
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
        $operator = $this->options['expose']['operator_id'];
        $this->operatorForm($form, $form_state);
        // Limit the exposed operators if needed.
        if (!empty($this->options['expose']['operator_limit_selection']) && !empty($this->options['expose']['operator_list'])) {
            $options = $this->operatorOptions();
            $operator_list = $this->options['expose']['operator_list'];
            $form['operator']['#options'] = array_intersect_key($options, $operator_list);
        }
        $form[$operator] = $form['operator'];
        $this->exposedTranslate($form[$operator], 'operator');
        unset($form['operator']);
        // When the operator and value forms are both in play, enclose them within
        // a wrapper.
        if (!empty($this->options['expose']['identifier'])) {
            $wrapper = $this->options['expose']['identifier'] . '_wrapper';
            $this->buildValueWrapper($form, $wrapper);
            $form[$operator]['#title_display'] = 'invisible';
            $form[$wrapper][$operator] = $form[$operator];
            unset($form[$operator]);
        }
    }
    // Build the form and set the value based on the identifier.
    if (!empty($this->options['expose']['identifier'])) {
        $value = $this->options['expose']['identifier'];
        $this->valueForm($form, $form_state);
        $form[$value] = $form['value'];
        if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
            unset($form[$value]['#title']);
        }
        $this->exposedTranslate($form[$value], 'value');
        if ($value != 'value') {
            unset($form['value']);
        }
        // When the operator and value forms are both in play, enclose them within
        // a wrapper, for usability. Also wrap if the value form is comprised of
        // multiple elements.
        if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) || count(Element::children($form[$value]))) {
            $wrapper = $value . '_wrapper';
            $this->buildValueWrapper($form, $wrapper);
            $form[$wrapper][$value] = $form[$value];
            unset($form[$value]);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.