Skip to content
QueryString.php 4 KiB
Newer Older
Jur de Vries's avatar
Jur de Vries committed
<?php

/**
 * @file
 * Contains Drupal\facets\Plugin\facets\url_processor\QueryString.
Jur de Vries's avatar
Jur de Vries committed
 */

namespace Drupal\facets\Plugin\facets\url_processor;
Jur de Vries's avatar
Jur de Vries committed

use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\UrlProcessor\UrlProcessorPluginBase;
use Symfony\Component\HttpFoundation\Request;
Jur de Vries's avatar
Jur de Vries committed

/**
Jur de Vries's avatar
Jur de Vries committed
 *   id = "query_string",
 *   label = @Translation("Query string"),
 *   description = @Translation("Query string is the default Facets URL processor, and uses GET parameters, e.g. ?f[0]=brand:drupal&f[1]=color:blue")
Jur de Vries's avatar
Jur de Vries committed
 * )
 */
class QueryString extends UrlProcessorPluginBase {
Jur de Vries's avatar
Jur de Vries committed

   * A string that separates the filters in the query string.
  const SEPARATOR = ':';

  /**
   * A string of how to represent the facet in the url.
   *
   * @var string
   */
  protected $url_alias;

   * An array of active filters.
   *
   * @var string[]
  protected $activeFilters = [];
  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $request);
    $this->initializeActiveFilters();
  }

  public function buildUrls(FacetInterface $facet, array $results) {
    // Create links for all the values.
    // First get the current list of get parameters.
    $get_params = $this->request->query;

    // Set the url alias from the the facet object.
    $this->url_alias = $facet->getUrlAlias();

    // No results are found for this facet, so don't try to create urls.
    if (empty($results)) {
    /** @var \Drupal\facets\Result\ResultInterface $result */
      $filter_string = $this->url_alias . self::SEPARATOR . $result->getRawValue();
      $result_get_params = clone $get_params;

      $filter_params = $result_get_params->get($this->filterKey, [], TRUE);
      // If the value is active, remove the filter string from the parameters.
      if ($result->isActive()) {
        foreach ($filter_params as $key => $filter_param) {
          if ($filter_param == $filter_string) {
            unset($filter_params[$key]);
          }
        }
      }
      // If the value is not active, add the filter string.
      else {
        $filter_params[] = $filter_string;
      }

      $result_get_params->set($this->filterKey, $filter_params);
      if ($facet->getFacetSource()->getPath()) {
        $request = Request::create('/' . $facet->getFacetSource()->getPath());
      $url->setOption('query', $result_get_params->all());
      $result->setUrl($url);
    }

  public function setActiveItems(FacetInterface $facet) {
    // Set the url alias from the the facet object.
    $this->url_alias = $facet->getUrlAlias();

    // Get the filter key of the facet.
    if (isset($this->activeFilters[$this->url_alias])) {
      foreach ($this->activeFilters[$this->url_alias] as $value) {
        $facet->setActiveItem(trim($value, '"'));
      }
    }
  }

  /**
   * Initialize the active filters.
   * Get all the filters that are active. This method only get's all the
   * filters but doesn't assign them to facets. In the processFacet method the
   * active values for a specific facet are added to the facet.
   */
  protected function initializeActiveFilters() {
    $url_parameters = $this->request->query;

    // Get the active facet parameters.
    $active_params = $url_parameters->get($this->filterKey, array(), TRUE);

    // Explode the active params on the separator.
    foreach ($active_params as $param) {
      list($key, $value) = explode(self::SEPARATOR, $param);
      if (!isset($this->activeFilters[$key])) {
        $this->activeFilters[$key] = [$value];
        $this->activeFilters[$key][] = $value;
Jur de Vries's avatar
Jur de Vries committed
  }
Joris Vercammen's avatar
Joris Vercammen committed
}