You are here

ExposedFilterDataBlock.php in Exposed Filter Data 8

File

src/Plugin/Block/ExposedFilterDataBlock.php
View source
<?php

namespace Drupal\exposed_filter_data\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Entity\View;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides an 'Exposed Filters Data' block.
 *
 * @Block(
 *   id = "exposed_filters_data_block",
 *   admin_label = @Translation("Exposed Filters Data"),
 * )
 */
class ExposedFilterDataBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new ExposedFiltersDataBlock.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, ModuleHandlerInterface $module_handler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->request = $request;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('request_stack')
      ->getCurrentRequest(), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    if ($params = $this->request->query
      ->all()) {

      // Allow modules to alter the parameters.
      $this->moduleHandler
        ->alter('exposed_filter_data_params', $params);
      $build = [
        '#theme' => 'exposed_filter_data_block',
        '#filters' => $params,
        '#attached' => [
          'library' => [
            'exposed_filter_data/exposed_filter_data.block',
          ],
        ],
      ];
    }
    return $build;
  }
  public function getCacheMaxAge() {
    return 0;
  }

}

Classes

Namesort descending Description
ExposedFilterDataBlock Provides an 'Exposed Filters Data' block.