ViewsExposedFilterBlock.php

Same filename in this branch
  1. 11.x core/modules/views/src/Plugin/Derivative/ViewsExposedFilterBlock.php
Same filename and directory in other branches
  1. 9 core/modules/views/src/Plugin/Derivative/ViewsExposedFilterBlock.php
  2. 9 core/modules/views/src/Plugin/Block/ViewsExposedFilterBlock.php
  3. 8.9.x core/modules/views/src/Plugin/Derivative/ViewsExposedFilterBlock.php
  4. 8.9.x core/modules/views/src/Plugin/Block/ViewsExposedFilterBlock.php
  5. 10 core/modules/views/src/Plugin/Derivative/ViewsExposedFilterBlock.php
  6. 10 core/modules/views/src/Plugin/Block/ViewsExposedFilterBlock.php

Namespace

Drupal\views\Plugin\Block

File

core/modules/views/src/Plugin/Block/ViewsExposedFilterBlock.php

View source
<?php

namespace Drupal\views\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Cache\Cache;
use Drupal\Component\Utility\Xss;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Plugin\Derivative\ViewsExposedFilterBlock as ViewsExposedFilterBlockDeriver;

/**
 * Provides a 'Views Exposed Filter' block.
 */
class ViewsExposedFilterBlock extends ViewsBlockBase {
  
  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    $contexts = $this->view->display_handler
      ->getCacheMetadata()
      ->getCacheContexts();
    return Cache::mergeContexts(parent::getCacheContexts(), $contexts);
  }
  
  /**
   * {@inheritdoc}
   *
   * @return array
   *   A renderable array representing the content of the block with additional
   *   context of current view and display ID.
   */
  public function build() : array {
    $output = $this->view->display_handler
      ->viewExposedFormBlocks() ?? [];
    // Provide the context for block build and block view alter hooks.
    // \Drupal\views\Plugin\Block\ViewsBlock::build() adds the same context in
    // \Drupal\views\ViewExecutable::buildRenderable() using
    // \Drupal\views\Plugin\views\display\DisplayPluginBase::buildRenderable().
    if (!empty($output)) {
      $output += [
        '#view' => $this->view,
        '#display_id' => $this->displayID,
      ];
    }
    // Before returning the block output, convert it to a renderable array with
    // contextual links.
    $this->addContextualLinks($output, 'exposed_filter');
    // Set the blocks title.
    if (!empty($this->configuration['label_display']) && ($this->view
      ->getTitle() || !empty($this->configuration['views_label']))) {
      $output['#title'] = [
        '#markup' => empty($this->configuration['views_label']) ? $this->view
          ->getTitle() : $this->configuration['views_label'],
        '#allowed_tags' => Xss::getHtmlTagList(),
      ];
    }
    return $output;
  }

}

Classes

Title Deprecated Summary
ViewsExposedFilterBlock Provides a 'Views Exposed Filter' block.

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