You are here

public function FilterPluginBase::showExposeButton in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/filter/FilterPluginBase.php \Drupal\views\Plugin\views\filter\FilterPluginBase::showExposeButton()
  2. 9 core/modules/views/src/Plugin/views/filter/FilterPluginBase.php \Drupal\views\Plugin\views\filter\FilterPluginBase::showExposeButton()

Shortcut to display the expose/hide button.

1 call to FilterPluginBase::showExposeButton()
FilterPluginBase::buildOptionsForm in core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
Provide the basic form which calls through to subforms.

File

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

Class

FilterPluginBase
Base class for Views filters handler plugins.

Namespace

Drupal\views\Plugin\views\filter

Code

public function showExposeButton(&$form, FormStateInterface $form_state) {
  $form['expose_button'] = [
    '#prefix' => '<div class="views-expose clearfix">',
    '#suffix' => '</div>',
    // Should always come after the description and the relationship.
    '#weight' => -200,
  ];

  // Add a checkbox for JS users, which will have behavior attached to it
  // so it can replace the button.
  $form['expose_button']['checkbox'] = [
    '#theme_wrappers' => [
      'container',
    ],
    '#attributes' => [
      'class' => [
        'js-only',
      ],
    ],
  ];
  $form['expose_button']['checkbox']['checkbox'] = [
    '#title' => $this
      ->t('Expose this filter to visitors, to allow them to change it'),
    '#type' => 'checkbox',
  ];

  // Then add the button itself.
  if (empty($this->options['exposed'])) {
    $form['expose_button']['markup'] = [
      '#markup' => '<div class="description exposed-description">' . $this
        ->t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
    ];
    $form['expose_button']['button'] = [
      '#limit_validation_errors' => [],
      '#type' => 'submit',
      '#value' => $this
        ->t('Expose filter'),
      '#submit' => [
        [
          $this,
          'displayExposedForm',
        ],
      ],
    ];
    $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
  }
  else {
    $form['expose_button']['markup'] = [
      '#markup' => '<div class="description exposed-description">' . $this
        ->t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
    ];
    $form['expose_button']['button'] = [
      '#limit_validation_errors' => [],
      '#type' => 'submit',
      '#value' => $this
        ->t('Hide filter'),
      '#submit' => [
        [
          $this,
          'displayExposedForm',
        ],
      ],
    ];
    $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
  }
}