function FilterPluginBase::buildExposeForm

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

Options form subform for exposed filter options.

Overrides HandlerBase::buildExposeForm

See also

buildOptionsForm()

4 calls to FilterPluginBase::buildExposeForm()
HistoryUserTimestamp::buildExposeForm in core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php
Options form subform for exposed filter options.
InOperator::buildExposeForm in core/modules/views/src/Plugin/views/filter/InOperator.php
Options form subform for exposed filter options.
NumericFilter::buildExposeForm in core/modules/views/src/Plugin/views/filter/NumericFilter.php
Options form subform for exposed filter options.
StringFilter::buildExposeForm in core/modules/views/src/Plugin/views/filter/StringFilter.php
Options form subform for exposed filter options.
4 methods override FilterPluginBase::buildExposeForm()
HistoryUserTimestamp::buildExposeForm in core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php
Options form subform for exposed filter options.
InOperator::buildExposeForm in core/modules/views/src/Plugin/views/filter/InOperator.php
Options form subform for exposed filter options.
NumericFilter::buildExposeForm in core/modules/views/src/Plugin/views/filter/NumericFilter.php
Options form subform for exposed filter options.
StringFilter::buildExposeForm in core/modules/views/src/Plugin/views/filter/StringFilter.php
Options form subform for exposed filter options.

File

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

Class

FilterPluginBase
Base class for Views filters handler plugins.

Namespace

Drupal\views\Plugin\views\filter

Code

public function buildExposeForm(&$form, FormStateInterface $form_state) {
    $form['#theme'] = 'views_ui_expose_filter_form';
    // #flatten will move everything from $form['expose'][$key] to $form[$key]
    // prior to rendering. That's why the preRender for it needs to run first,
    // so that when the next preRender (the one for fieldsets) runs, it gets
    // the flattened data.
    array_unshift($form['#pre_render'], [
        static::class,
        'preRenderFlattenData',
    ]);
    $form['expose']['#flatten'] = TRUE;
    if (empty($this->always_required)) {
        $form['expose']['required'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Required'),
            '#default_value' => $this->options['expose']['required'],
        ];
    }
    else {
        $form['expose']['required'] = [
            '#type' => 'value',
            '#value' => TRUE,
        ];
    }
    $form['expose']['label'] = [
        '#type' => 'textfield',
        '#default_value' => $this->options['expose']['label'],
        '#title' => $this->t('Label'),
        '#size' => 40,
    ];
    $form['expose']['description'] = [
        '#type' => 'textfield',
        '#default_value' => $this->options['expose']['description'],
        '#title' => $this->t('Description'),
        '#size' => 60,
    ];
    if (!empty($form['operator']['#type'])) {
        // Increase the width of the left (operator) column.
        $form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
        $form['operator']['#suffix'] = '</div>';
        $form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
        $form['value']['#suffix'] = '</div>';
        $form['expose']['use_operator'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Expose operator'),
            '#description' => $this->t('Allow the user to choose the operator.'),
            '#default_value' => !empty($this->options['expose']['use_operator']),
        ];
        $operators = $this->operatorOptions();
        if (!empty($operators) && count($operators) > 1) {
            $form['expose']['operator_limit_selection'] = [
                '#type' => 'checkbox',
                '#title' => $this->t('Limit the available operators'),
                '#description' => $this->t('Limit the available operators to be shown on the exposed filter.'),
                '#default_value' => !empty($this->options['expose']['operator_limit_selection']),
                '#states' => [
                    'visible' => [
                        ':input[name="options[expose][use_operator]"]' => [
                            'checked' => TRUE,
                        ],
                    ],
                ],
            ];
            $form['expose']['operator_list'] = [
                '#type' => 'select',
                '#title' => $this->t('Restrict operators to'),
                '#default_value' => $this->options['expose']['operator_list'],
                '#options' => $operators,
                '#multiple' => TRUE,
                '#description' => $this->t('Selecting none will make all of them available.'),
                '#states' => [
                    'visible' => [
                        ':input[name="options[expose][operator_limit_selection]"]' => [
                            'checked' => TRUE,
                        ],
                        ':input[name="options[expose][use_operator]"]' => [
                            'checked' => TRUE,
                        ],
                    ],
                ],
            ];
        }
        $form['expose']['operator_id'] = [
            '#type' => 'textfield',
            '#default_value' => $this->options['expose']['operator_id'],
            '#title' => $this->t('Operator identifier'),
            '#size' => 40,
            '#description' => $this->t('This will appear in the URL after the ? to identify this operator.'),
            '#states' => [
                'visible' => [
                    ':input[name="options[expose][use_operator]"]' => [
                        'checked' => TRUE,
                    ],
                ],
            ],
        ];
    }
    else {
        $form['expose']['operator_id'] = [
            '#type' => 'value',
            '#value' => '',
        ];
    }
    if (empty($this->alwaysMultiple)) {
        $form['expose']['multiple'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Allow multiple selections'),
            '#description' => $this->t('Enable to allow users to select multiple items.'),
            '#default_value' => $this->options['expose']['multiple'],
        ];
    }
    $form['expose']['remember'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Remember the last selection'),
        '#description' => $this->t('Enable to remember the last selection made by the user.'),
        '#default_value' => $this->options['expose']['remember'],
    ];
    $role_options = array_map(fn(RoleInterface $role) => Html::escape($role->label()), Role::loadMultiple());
    $form['expose']['remember_roles'] = [
        '#type' => 'checkboxes',
        '#title' => $this->t('User roles'),
        '#description' => $this->t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
        '#default_value' => $this->options['expose']['remember_roles'],
        '#options' => $role_options,
        '#states' => [
            'invisible' => [
                ':input[name="options[expose][remember]"]' => [
                    'checked' => FALSE,
                ],
            ],
        ],
    ];
    $form['expose']['identifier'] = [
        '#type' => 'textfield',
        '#default_value' => $this->options['expose']['identifier'],
        '#title' => $this->t('Filter identifier'),
        '#size' => 40,
        '#description' => $this->t('This will appear in the URL after the ? to identify this filter. Cannot be blank. Only letters, digits and the dot ("."), hyphen ("-"), underscore ("_"), and tilde ("~") characters are allowed. @reserved_identifiers are reserved words and cannot be used.', [
            '@reserved_identifiers' => '"' . implode('", "', self::RESTRICTED_IDENTIFIERS) . '"',
        ]),
    ];
}

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